I
I
Ilya Trifonov2015-10-02 12:20:29
SQL
Ilya Trifonov, 2015-10-02 12:20:29

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
27d4850542384fe7b112f54fe1517550.png
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

2 answer(s)
A
Artur Polozov, 2015-10-02
@ilyatrifonov

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

A
app25, 2015-10-05
@app25

case to help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question