LightDB支持在窗口函数中使用distinct关键字

功能概述

从24.1版本开始,LightDB oracle兼容模式下支持在窗口函数中使用distinct关键字,即窗口中的非重复值才会参与聚集运算,而那些重复值则不会参与聚集运算。

例如select sum(distinct b) over(partition by a) from t;这样的sql,处理逻辑是:先对t表按字段a分组,再对分组内的b数据去重,之后将分组内去重后的b列进行汇总求和,最后把sum结果返回。

上面的sql中over子句中没有order by子句和frame子句,那么一整个分组就可以认为是同一个frame。事实上Oracle也是这么做的。

案例演示

create table t(a int,b int);
insert into t values(1,1);
insert into t values(1,2);
insert into t values(1,1);
insert into t values(2,2);
insert into t values(2,2);
insert into t values(2,3);

lightdb@oracle=# select sum(distinct b) over(partition by a), t.*,rownum from t;
 sum | a | b | rownum 
-----+---+---+--------
   3 | 1 | 1 |      1
   3 | 1 | 1 |      2
   3 | 1 | 2 |      3
   5 | 2 | 2 |      4
   5 | 2 | 2 |      5
   5 | 2 | 3 |      6
(6 rows)

使用限制

1、只允许在Oracle兼容模式下使用带DISTINCT关键字的窗口函数。
2、只有sum/avg/min/max/count可以使用带DISTINCT关键字的窗口函数。
3、当使用带DISTINCT关键字的窗口函数时,窗口定义中不能使用order by子句。
    即select sum(distinct b) over(partition by a order by b) from t语法上不支持
4、当使用带DISTINCT关键字的窗口函数时,不允许指定窗口属性,也就是说只允许使用默认的窗口属性。
    即select sum(distinct b) over(partition by a order by b range between 1 preceding and 1 following) from t语法上不支持

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值