1

I have a SQL query "Select A,B,C,(A+B+C) as Total from Test" and the result isenter image description here

How to get the same result in only two columns like

enter image description here

0

2 Answers 2

2

You can "unpivot". My preferred method is apply:

Select v.name, v.value
from Test t cross apply
     (values ('A', A), ('B', B), ('C', C)) v(name, value);

The total is not in your result set, but that can also easily be added in.

Sign up to request clarification or add additional context in comments.

Comments

1

You can unpivot using UNION ALL. I still prefer Gordon's solution though.

SELECT 'A' AS name, A AS [value] FROM Test
UNION ALL
SELECT 'B', B FROM Test
UNION ALL
SELECT 'C', C FROM Test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.