背景
数据如上,按id和ssny分组,求最大的开票时间,若为空,取非空的上一期组内最大开票时间
解
SELECT
id,
ssny,
kpsj,
max(kpsj) OVER(PARTITION BY id ORDER BY ssny desc ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) zwkpsj
from a;
即为所得
拓展
sum(…A…) over(partition by …B… order by …C… rows between … D1… and …D2…)
avg(…A…) over(partition by …B… order by …C… rows between … D1… and …D2…)
A:需要被加工的字段名称
B:分组的字段名称
C:排序的字段名称
D:计算的行数范围
rows between unbounded prceding and current row --包括本行和之前所有的行
rows between current row and unbounded following --包括本行和之后所有的行
rows between 3 preceding and current row --包括本行以内和前三行
rows between 3 preceding and 1 following --从前三行到下一行 (5行)