Redshift SQL - Tall/long 到宽格式

Redshift SQL - Tall/long to Wide Format

我有一个 table 名为“source_table”

|    Date    |  Country  | Subscribers |
----------------------------------------
| 2021-10-01 |    USA    |      5      |
| 2021-10-12 |  Canada   |      10     |
| 2021-10-23 |    USA    |      15     |
--
| 2021-11-01 |    USA    |      10     |
| 2021-11-05 |   Canada  |      20     |

我想将其转换为宽格式

| Country | Oct-21 | Nov-21 | Dec-21 |
--------------------------------------
|   USA   |   53   |   68   |   12   |
|  Canada |   35   |   86   |   21   |

我尝试了以下代码:

SELECT country,
       CASE WHEN date BETWEEN '2021-10-01' AND '2021-10-31' THEN SUM(subscribers)
       END AS "Oct-21",
       CASE WHEN date BETWEEN '2021-11-01' AND '2021-11-30' THEN SUM(subscribers)
       END AS "Nov-21",
       CASE WHEN date BETWEEN '2021-12-01' AND '2021-12-31' THEN SUM(subscribers)
       END AS "Dec-21"
FROM 
     source_table
GROUP BY 
     country

但是上面的代码抛出一个错误说

column "source_table.date" must appear in the GROUP BY clause or be used in an aggregate function

我不确定为什么 GROUP BY 中需要“日期”列;如果我确实在 GROUP BY 中使用它,我会多次重复国家/地区,这不是所需的输出。国家/地区名称应该只出现一次,该月份的订阅者数量合计,列中不应有任何 NULL(source_table 中每个国家/地区每天至少有一个订阅者)。

我是不是漏掉了什么?请帮忙。我在 Redshift 上使用 SQL。

您想对 CASE 个表达式求和或计数,例如

SELECT
    country,
    SUM(CASE WHEN date BETWEEN '2021-10-01' AND '2021-10-31'
             THEN subscribers ELSE 0 END) AS "Oct-21",
    SUM(CASE WHEN date BETWEEN '2021-11-01' AND '2021-11-30'
             THEN subscribers ELSE 0 END) AS "Nov-21",
    SUM(CASE WHEN date BETWEEN '2021-12-01' AND '2021-12-31'
             THEN subscribers ELSE 0 END) AS "Dec-21"
FROM source_table
GROUP BY country;