RATIO_TO_REPORT是一個分析函數,用于計算某一列的值在指定分組中的所占的比率。

語法

RATIO_TO_REPORT(col) over ([partition by xxx]) ; 
  • col:需要查詢的列,如果該值為空,則比率的值也為空。
  • [partition by xxx]:指定的分組,如果省略該子句,則會計算當前值占指定分組中所有值的比率。

示例

select *, ratio_to_report(b) over () from rtp;
 a | b | c  |    ratio_to_report     
---+---+----+------------------------
 1 | 5 |  4 | 0.25000000000000000000
 1 | 5 |  6 | 0.25000000000000000000
 2 | 3 | 10 | 0.15000000000000000000
 2 | 7 |    | 0.35000000000000000000
(4 rows)

select *, ratio_to_report(b) over (partition by a) from rtp;
 a | b | c  |    ratio_to_report     
---+---+----+------------------------
 1 | 5 |  4 | 0.50000000000000000000
 1 | 5 |  6 | 0.50000000000000000000
 2 | 3 | 10 | 0.30000000000000000000
 2 | 7 |    | 0.70000000000000000000
(4 rows)