sql --- 窗口函数

交流qq: 2499496272

row_number() over()——分组TOPN

例子:

select
 id,age,name,sex
 from (
 select id,age,name,sex,
 row_number() over(partition by sex order by age desc) as rankfrom t_rownumber) tmpwhere rank<=2;

sum() over()——级联求和

sum()over()的累加范围指定语法:

  • sum() over(partition by x order by y rows between 8 preceding and
    current row)
  • sum() over(partition by x order by y rows between 8 preceding and 5
    following)
  • sum() over(partition by x order by y rows between unbounded preceding
    and 5 following)
  • sum() over(partition by x order by y rows between unbounded preceding
    and unbounded following)
select
 name,month,amt,
 sum(amt) over(partition by name order by month rows between unbounded preceding and current row) as accumulate
 from t_tmp;

3. 窗口分析函数综合应用案例

数据

select 
distinct shopfrom
 (select shop,count(1) as cnt
 from
 (
 select shop,s_date,amt,date_sub(s_date,rn) as diff
 from(
 select shop,s_date,amt,row_number() over(partition by shop order by s_date) as rnfrom t18
 ) o1
 ) o2 
 group by shop,diff having cnt>=4
 ) o3;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值