排名函数

PostgreSQL 中的时间序列分析

Jasmin Ludolf

Content Developer, DataCamp

ROW_NUMBER 提供顺序编号

SELECT
    station_id,
    year_month,
    t_monthly_min,
    ROW_NUMBER() OVER (
      PARTITION BY station_id
      ORDER BY t_monthly_min) AS rank
FROM temperatures_monthly AS tm
WHERE year_month BETWEEN '2018-01-01'
    AND '2018-12-31'
ORDER BY station_id, t_monthly_min;
|station_id|year_month|t_monthly_min|rank|
|----------|----------|-------------|----|
|         1|2018-12-01|          5.4|   1|
|         1|2018-02-01|          6.2|   2|
|         1|2018-01-01|          7.4|   3|
|         1|2018-11-01|          9.0|   4|
|         1|2018-03-01|         10.6|   5| 
|         1|2018-04-01|         14.7|   6|
|         1|2018-10-01|         16.6|   7|
|         1|2018-05-01|         17.2|   8|
|         1|2018-06-01|         21.9|   9|
|         1|2018-09-01|         24.4|  10|
|         1|2018-07-01|         28.1|  11|
...
PostgreSQL 中的时间序列分析

Rank(排名)

  • RANK():按 ORDER BY 为每个分区内的行分配排名,从 1 开始;相同值重复名次
...
RANK() OVER (
        PARTITION BY station_id 
        ORDER BY t_monthly_min DESC) AS rank
...
...
ROW_NUMBER() OVER (
        PARTITION BY station_id 
        ORDER BY t_monthly_min DESC) AS row
...
PostgreSQL 中的时间序列分析

Rank 与 Row 的区别

RANK()

|year_month|t_monthly_min|rank|
|----------|-------------|----|
|2018-02-01|          1.6|   1|
|2018-01-01|          1.6|   1|
|2018-03-01|            2|   3|
|2018-11-01|          2.5|   4|
|2018-12-01|          2.7|   5|
|2018-04-01|          3.4|   6|
|2018-10-01|          4.1|   7|
|2018-09-01|          4.7|   8|
...

ROW_NUMBER()

|year_month|t_monthly_min|rank|
|----------|-------------|----|
|2018-02-01|          1.6|   1|
|2018-01-01|          1.6|   2|
|2018-03-01|            2|   3|
|2018-11-01|          2.5|   4|
|2018-12-01|          2.7|   5|
|2018-04-01|          3.4|   6|
|2018-10-01|          4.1|   7|
|2018-09-01|          4.7|   8|
...
PostgreSQL 中的时间序列分析

Dense Rank(紧密排名)

  • DENSE_RANK():按 ORDER BY 为每个分区内的行分配排名,从 1 开始;相同值重复名次
    • 不跳过名次
...
DENSE_RANK() OVER (
    PARTITION BY station_id 
    ORDER BY t_monthly_min DESC) AS rank
...
PostgreSQL 中的时间序列分析

Rank 与 Dense Rank 的区别

RANK()

|year_month|t_monthly_min|rank|
|----------|-------------|----|
|2018-02-01|          1.6|   1|
|2018-01-01|          1.6|   1|
|2018-03-01|            2|   3|
|2018-11-01|          2.5|   4|
|2018-12-01|          2.7|   5|
|2018-04-01|          3.4|   6|
|2018-10-01|          4.1|   7|
|2018-09-01|          4.7|   8|
...

DENSE_RANK()

|year_month|t_monthly_min|rank|
|----------|-------------|----|
|2018-02-01|          1.6|   1|
|2018-01-01|          1.6|   1|
|2018-03-01|            2|   2|
|2018-11-01|          2.5|   3|
|2018-12-01|          2.7|   4|
|2018-04-01|          3.4|   5|
|2018-10-01|          4.1|   6|
|2018-09-01|          4.7|   7|
...
PostgreSQL 中的时间序列分析

百分位排名(Percent rank)

  • PERCENT_RANK:按 ORDER BY 为每个分区内的行分配百分位排名
    • (rank - 1) / (分区总行数 - 1)
    • 浮点值范围 01
...
PERCENT_RANK() OVER (
    PARTITION BY station_id 
    ORDER BY t_monthly_min DESC) AS percent_rank
...
PostgreSQL 中的时间序列分析

Percent rank 输出

|year_month|t_monthly_min|percent_rank|
|----------|-------------|------------|
|2018-02-01|          1.6|           0|
|2018-01-01|          1.6|           0|
|2018-03-01|            2|        0.18|
|2018-11-01|          2.5|        0.27|
|2018-12-01|          2.7|        0.36|
|2018-04-01|          3.4|        0.45|
|2018-10-01|          4.1|        0.54|
|2018-09-01|          4.7|        0.63|
...
PostgreSQL 中的时间序列分析

Passons à la pratique !

PostgreSQL 中的时间序列分析

Preparing Video For Download...