1. 程式人生 > >OVER中OVER(PARTITION BY... ORDER BY...)的使用

OVER中OVER(PARTITION BY... ORDER BY...)的使用

select PGM_ID,PGM_SH_CFM_CD,row_number() over(partition by PGM_ID order by PGM_SH_CFM_CD)rn,
count(*) over(partition by PGM_ID) cnt from MED_PGM_SH_CFM_H

查出的資料如下

 

最近使用到了ORACLE的分析函式隨手記錄一下。

over(partition by ... order by ...)這個函式,即把一組資料按照制定的欄位進行分割成各種組,然後組內按照某個欄位排序。其他常用的還有:
count() over(partition by ... order by ...)
max() over(partition by ... order by ...)
min() over(partition by ... order by ...)
sum() over(partition by ... order by ...)
avg() over(partition by ... order by ...)