功能概述
从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语法上不支持


被折叠的 条评论
为什么被折叠?



