Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jazzyb
Starting Member
20 Posts |
Posted - 2011-03-22 : 09:20:23
|
| I have table named as "paytable" with following structure and dataempid , month , salary1 , January , 10002 , January , 10003 , January , 1000...1 , February , 15002 , February , 15003 , February , 2000...1 , March , 20002 , March , 20003 , March 2500... What SELECT QUERY should I use to get the output in following format:empid , January_Salary , February_Salary , March_Salary1 , 1000 , 1500 , 20002 , 1000 , 1500 , 20003 , 1000 , 2000 , 2500...Thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
jonassi
Starting Member
2 Posts |
Posted - 2011-03-22 : 11:45:26
|
| You can also use CASE.SELECTempid,CASE WHEN month ='January' THEN salary END AS [January_salary],CASE WHEN month ='February' THEN salary END AS [February_salary]...FROM paytable |
 |
|
|
jazzyb
Starting Member
20 Posts |
Posted - 2011-03-22 : 12:24:11
|
| Respected Friends,Thanks for replies!I am using MS Sql Express 2005But my problem is not solved. Pivot seems to be very difficult when I used this gives syntax error. I tried may different syntax by searching form google.Secondaly Select Case is also problem it gives null for one column when showing data for other column.May you suggest correct syntax Pivot query for my table. I am irritated by searching solution from internet.Thanks |
 |
|
|
jonassi
Starting Member
2 Posts |
Posted - 2011-03-22 : 12:52:23
|
| You need to group on empid, try this.SELECTempid,SUM(CASE WHEN month ='January' THEN salary END) AS [January_salary],SUM(CASE WHEN month ='February' THEN salary END) AS [February_salary]...FROM paytableGROUP BY emp_id |
 |
|
|
|
|
|
|
|