题解 | #行列互换#

行列互换

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 年

全部评论

相关推荐

程序员小白条:找的太晚,别人都是大三实习,然后大四秋招春招的,你大四下了才去实习,晚1年
点赞 评论 收藏
分享
06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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