用sql实现行专列
问题描述:2014年公司组织架构调整,导致部门出现多个绩效,业务及人员不同,无法合并算绩效,源表内容如下:
2014 B 9
2015 A 8
2014 A 10
2015 B 7
2014 B 6
想要的结果如下:
A col_A col_B
2014 10 6,9
2015 8 7
实现如下:
select col_1,max(col_A),max(col_B) from (
select col_1,case when col_2 = 'A' then score
else '0'
end as col_A
,case when col_2 = 'B' then score
else '0'
end as col_B
from (
select col_1,col_2,group_concat(col_3) score from depart group by col_1,col_2
) t1
)t2
group by col_1