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

时间:2021-01-19 08:33:26

Here's a screenshot of the data.

这是数据的屏幕截图。

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

Here, I want to compare columns (Sal, TA, DA) in the basis of two different year (e.g. 2013 and 2014) with order by month...so that my output must be like following...

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

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

Please give me query for this

请给我查询

1 个解决方案

#1


0  

Try something like

尝试类似的东西

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

#1


0  

Try something like

尝试类似的东西

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