这谁家的面试题,03都没有2月份,怎么结果会有2月份,而且table2就是摆设
sql可以下面这样,就是一个行列转置,用case when
以下是sqlserver的
select dep ,
sum(case mon when '一月份' then score else null end) as 一月份 ,
sum(case mon when '二月份' then score else null end) as 二月份,
sum(case mon when '三月份' then score else null end) as 三月份
from table1
group by dep
select dep,
max(case mon when '一月' then score else null end) 一月,
max(case mon when '二月' then score else null end) 二月,
max(case mon when '三月' then score else null end) 三月
from t1
group by dep
select dep,sum(case mon when '一月份' then score end) '一月份',sum(case mon when '二月份' then score end) '二月份' ,
sum(case mon when '三月份' then score end) '三月份'
from Table1 group by dep
SELECT DEP AS 部门,SUM(CASE MON WHEN '一月份' THEN ISNULL(SCORT,NULL) ) AS 一月份,SUM(CASE MON WHEN '二月份' THEN ISNULL(SCORT,NULL) ) 二月份,SUM(CASE MON WHEN 三月份' THEN ISNULL(SCORT,NULL) ) 三月份 FROM TABLE1