pdf链接:https://docs.aws.amazon.com/redshift/latest/dg/redshift-dg.pdf
为什么用开窗函数
把经过聚合的列和没有经过聚合的列组合在一起
基本语法
function(expression)
over(partition by column order by column ASC/DESC ROWS[...])
其中,over()是必须有的,其余的不是必须的
例如,计算累积总计工资,表涉及name、department、group、salary、join_date字段
每个部门及小组的
partition by department,group
计算前五个人
order by salary
习题网站:https://www.windowfunctions.com/
例题1:对猫咪按照年龄分组体重求和,返回大于12的记录,按年龄排序
select
age,sum(weight) as total_weight
from cats
group by age
having sum(weight)>12
order by age
例题2:对猫咪按姓名排序进行running total(累计求和)
select
name,
sum(weight) over(order by name)
from cats
例题3:对猫咪进行running total计算,同一个breed里累计,排序先按breed再按name