1.行转列
case 需要转换字段 when(判断相等关系) 待判断的值 then 相等时填充的值 else 不相等时填充的值 end
转换结束
使用场景:通常跟分组,聚合函数连用
表:stu_score
stu_id subject score
sql:select stu_id,sum(case subject when ‘数学’ then score else 0 end) math from stu_score group by stu_id
2.分组生成排序编号字段
row_number()over(partition by 分组依据列表 order by 排序依据列表 倒序/顺序)
使用场景:分组后需得到每组的前几项
注意:生成的编号不能直接用于where子句 需转临时表
表:stu_score
stu_id subject score
sql:select t.* from
(select stu_score.*,row_number()over(partition by subject order by subject,score desc) id) t
where id<=2