如何在SQL Server 2008中使用正确的查询解决此问题

时间:2013-05-11 07:38:31

标签: sql sql-server sql-server-2008 pivot-table

这是数据的屏幕截图。

enter image description here

在这里,我想比较两个不同年份(例如2013年和2014年)的列(Sal, TA, DA)和逐月...以便我的输出必须如下......

enter image description here

请给我查询

1 个答案:

答案 0 :(得分:0)

尝试类似

的内容
select EMP_ID, MONTH,
max( case when Year=2013 then Sal else null end) as Sal_2013, 
max( case when Year=2014 then Sal else null end) as Sal_2014, 
max( case when Year=2013 then TA else null end) as TA_2013, 
max( case when Year=2014 then TA else null end) as TA_2014, 
max( case when Year=2013 then DA else null end) as DA_2013, 
max( case when Year=2014 then DA else null end) as DA_2014
from  table1
group by EMP_ID, MONTH