Click here to Skip to main content
15,742,120 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi dudes,
i've a simple query which gives me my results horizontally.
SQL
SELECT
 ROUND(SUM(TotalAmount),2) AS [Total Amount],
 ROUND(SUM(Amount1),2) AS [A],
 ROUND(SUM(Amount1),2) AS [B],
 ROUND(SUM(Amount1),2) AS [C],
 ROUND(SUM(Amount1),2) AS [D]

FROM tbl1

I then have one row and five columns.
Total Amount  A    B    C    D
   100       150  200  250  300  


How can i change the query so that i get two columns.Something like this:
Total Amount    100
 A              150
 B              200
 C              250
 D              300
Posted
Comments
King Fisher 23-Mar-15 9:14am    
Have you tried with unpivot?

1 solution

Try:
SQL
SELECT Value, V
FROM
(SELECT
 ROUND(SUM(TotalAmount),2) AS [Total Amount],
 ROUND(SUM(Amount1),2) AS [A],
 ROUND(SUM(Amount2),2) AS [B],
 ROUND(SUM(Amount3),2) AS [C],
 ROUND(SUM(Amount4),2) AS [D]

FROM tbl1 ) AS t
UNPIVOT
(
V FOR Value IN ([Total Amount], A, B, C, D)
) AS UP
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900