Answer the question
In order to leave comments, you need to log in
How to split result row into two in MS SQL?
Hey!
Tell me the solution to this smart problem: we have the result of the selection
As a result, a line appears where two columns and FixHours and AddHours have non-zero values:
FixHours = 0.5 AddHours = 1.5 CalculatedValue = 16.5
How can it be divided into two to get rows:
FixHours = 0.5 AddHours = 0 CalculatedValue = 16.5
FixHours = 0 AddHours = 1.5 CalculatedValue = 16.5
There was an idea to find only this line, then artificially create the second one using Union. But she failed, because. such rows in the table occur more than once. In general, any ideas? I've already broken my head.
Answer the question
In order to leave comments, you need to log in
What's wrong with union?
For example:
CREATE TABLE #Hours (FixHours INT, AddHours INT, CalCulatedValue INT)
INSERT INTO #Hours VALUES (10,0,14),(5,0,14),(5,1,16),(0,5,17)
SELECT * FROM #Hours h
SELECT FixHours, AddHours, CalCulatedValue FROM #Hours h
WHERE NOT (h.FixHours > 0 AND h.AddHours > 0)
UNION ALL
SELECT FixHours, 0, h.CalCulatedValue FROM #Hours h
WHERE h.FixHours > 0 AND h.AddHours > 0
UNION ALL
SELECT 0, AddHours, h.CalCulatedValue FROM #Hours h
WHERE h.FixHours > 0 AND h.AddHours > 0
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question