ORDER BY

PostgreSQL 統計摘要與視窗函式

Michel Semaan

Data Scientist

列號(Row numbers)

查詢

SELECT
  Year, Event, Country,
  ROW_NUMBER() OVER () AS Row_N
FROM Summer_Medals
WHERE
  Medal = 'Gold';

結果*

| Year | Event                      | Country | Row_N |
|------|----------------------------|---------|-------|
| 1896 | 100M Freestyle             | HUN     | 1     |
| 1896 | 100M Freestyle For Sailors | GRE     | 2     |
| 1896 | 1200M Freestyle            | HUN     | 3     |
| ...  | ...                        | ...     | ...   |
PostgreSQL 統計摘要與視窗函式

加入 ORDER BY

  • OVER 中的 ORDER BY 會為與當前列相關的列排序。
    • :在 ROW_NUMBEROVER 子句中以年份遞減排序,最新年份的列會被指定為 1。
PostgreSQL 統計摘要與視窗函式

以年份遞減排序

查詢

SELECT
  Year, Event, Country,
  ROW_NUMBER() OVER (ORDER BY Year DESC) AS Row_N
FROM Summer_Medals
WHERE
  Medal = 'Gold';

結果

| Year | Event         | Country | Row_N |
|------|---------------|---------|-------|
| 2012 | Wg 96 KG      | IRI     | 1     |
| 2012 | 4X100M Medley | USA     | 2     |
| 2012 | Wg 84 KG      | RUS     | 3     |
| ...  | ...           | ...     | ...   |
| 2008 | 50M Freestyle | BRA     | 637   |
| 2008 | 96 - 120KG    | CUB     | 638   |
| ...  | ...           | ...     | ...   |
PostgreSQL 統計摘要與視窗函式

多欄位排序

查詢

SELECT
  Year, Event, Country,
  ROW_NUMBER() OVER
    (ORDER BY Year DESC, Event ASC) AS Row_N
FROM Summer_Medals
WHERE
  Medal = 'Gold';

結果

| Year | Event   | Country | Row_N |
|------|---------|---------|-------|
| 2012 | + 100KG | FRA     | 1     |
| 2012 | + 67 KG | SRB     | 2     |
| 2012 | + 78KG  | CUB     | 3     |
| ...  | ...     | ...     | ...   |
PostgreSQL 統計摘要與視窗函式

OVER 內外的排序

查詢

SELECT
  Year, Event, Country,
  ROW_NUMBER() OVER
    (ORDER BY Year DESC, Event ASC) AS Row_N
FROM Summer_Medals
WHERE
  Medal = 'Gold'
ORDER BY Country ASC, Row_N ASC;

結果

| Year | Event   | Country | Row_N |
|------|---------|---------|-------|
| 2012 | 1500M   | ALG     | 36    |
| 2000 | 1500M   | ALG     | 1998  |
| 1996 | 1500M   | ALG     | 2662  |
| ...  | ...     | ...     | ...   |
  • OVER 裡的 ORDER BY 會先於 OVER 外的 ORDER BY 生效。
PostgreSQL 統計摘要與視窗函式

衛冕冠軍

  • 「衛冕冠軍」指同時贏得上一屆與本屆比賽的冠軍。
  • 需要把上一屆與本屆的冠軍放在同一列(不同欄)。

使用 LAG

  • LAG(column, n) OVER (...) 會回傳當前列往前第 n 列的 column 值。
    • LAG(column, 1) OVER (...) 會回傳前一列的值。
PostgreSQL 統計摘要與視窗函式

當屆冠軍

查詢

SELECT
  Year, Country AS Champion
FROM Summer_Medals
WHERE
  Year IN (1996, 2000, 2004, 2008, 2012)
  AND Gender = 'Men' AND Medal = 'Gold'
  AND Event = 'Discus Throw';

結果

| Year | Champion |
|------|----------|
| 1996 | GER      |
| 2000 | LTU      |
| 2004 | LTU      |
| 2008 | EST      |
| 2012 | GER      |
PostgreSQL 統計摘要與視窗函式

本屆與上屆冠軍

查詢

WITH Discus_Gold AS (
  SELECT
    Year, Country AS Champion
  FROM Summer_Medals
  WHERE
    Year IN (1996, 2000, 2004, 2008, 2012)
    AND Gender = 'Men' AND Medal = 'Gold'
    AND Event = 'Discus Throw')

SELECT
  Year, Champion,
  LAG(Champion, 1) OVER
    (ORDER BY Year ASC) AS Last_Champion
FROM Discus_Gold
ORDER BY Year ASC;

結果

| Year | Champion | Last_Champion |
|------|----------|---------------|
| 1996 | GER      | null          |
| 2000 | LTU      | GER           |
| 2004 | LTU      | LTU           |
| 2008 | EST      | LTU           |
| 2012 | GER      | EST           |
PostgreSQL 統計摘要與視窗函式

一起來練習吧!

PostgreSQL 統計摘要與視窗函式

Preparing Video For Download...