Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have two columns(month,amount). i had enter the datas of the year. i need to show it in row wise. i show u the format like below. i dont know how to construct sql query.

table:
month Amount
jan 500
feb 510
etc..

i want to display like this
jan feb is act as header.
jan feb
500 510
Posted

VB
SELECT * FROM tablename
PIVOT
(
  sum(amount) FOR month IN ([jan],[feb])
)AS somename
 
Share this answer
 
Comments
skydger 18-Jan-14 11:10am    
+5 :)
Yes you can using pivot or case condition
SQL
SELECT SUM(A.JAN)AS JAN,SUM(A.FEB)AS FEB
FROM (
SELECT CASE WHEN [month]='jan' THEN amount ELSE 0 END AS JAN,
CASE WHEN [month]='feb' THEN amount ELSE 0 END AS FEB
FROM dbo.tbl1)AS A
 
Share this answer
 
v2
Just to add, I've written an article that explains all the possible ways to do this, and how they work: SQL Wizardry Part Seven - PIVOT and arbitrary lists of data[^]
 
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