题解 | #行列互换#

行列互换

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 年

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 11:27
明天又是董事长面,啥时候是个头啊
积极向上的林同学:董事长亲自面试
点赞 评论 收藏
分享
鬼迹人途:你去投一投尚游游戏,服务器一面,第一个图算法,做完了给你一个策略题,你给出方案他就提出低概率问题,答不上当场给你挂
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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