Redshift SQL - 高/长到宽格式
Posted
技术标签:
【中文标题】Redshift SQL - 高/长到宽格式【英文标题】:Redshift SQL - Tall/long to Wide Format 【发布时间】:2021-12-07 02:59:33 【问题描述】:我有一个名为“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。
【问题讨论】:
【参考方案1】:您想对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;
【讨论】:
已解决。谢谢。以上是关于Redshift SQL - 高/长到宽格式的主要内容,如果未能解决你的问题,请参考以下文章