题解 | #行列互换#

行列互换

https://www.nowcoder.com/practice/019836324d544324a63432dc205f8502

比较简单的换行,就是把年的数据合并起来了,再加上判断是几月的数据,一相加即可.聚合+判断
方法一:group by +sum()+case when 做法
select 
年,
sum(case when 月=1 then 值 else 0 end) as m1,
sum(case when 月=2 then 值 else 0 end) as m2,
sum(case when 月=3 then 值 else 0 end) as m3,
sum(case when 月=4 then 值 else 0 end) as m4
from cook
group by 年
order by 年

方法二:group by +sum()+if()
select 
年,
sum(if(月=1,值,0)) as m1,
sum(if(月=2,值,0)) as m2,
sum(if(月=3,值,0)) as m3,
sum(if(月=4,值,0)) as m4
from cook
group by 年
order by 年

全部评论

相关推荐

08-05 18:14
门头沟学院 Java
小花的沉默:是学历厂没错啊,学历太高了不要
投递小鹏汽车等公司10个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务