在 Presto 的时间戳字段中按月计算出现次数

Counting occurences by month in a timestamp field in Presto

我有一个时间戳类型的字段,我想按月统计记录,如下所示:

Date
2021-01-23
2021-01-12
2021-03-12

应该return

Jan Mar
2    1

如何在 prestodb 中执行此操作?

如果你想旋转你的数据,你可以这样做:

-- sample data
WITH dataset (date) AS (
    VALUES (date '2021-01-23'),
        (date '2021-01-12'),
        (date '2021-03-12')
) 

--query
select count_if(month(date) = 1) as Jan,
    count_if(month(date) = 2) as Feb,
    count_if(month(date) = 3) as Mar 
    -- ...
from dataset

输出:

Jan Feb March
2 0 1

对于“常规”柱状格式,只需使用格式化的短月份名称分组:

--query
select date_format(date, '%b') month, count(*) count
from dataset
group by date_format(date, '%b')

输出:

month count
Mar 1
Jan 2