我有包含以下列的表格:主题、人员、发布日期。我想创建一个查询来帮助我计算每个人每个季度在特定主题上写了多少次。示例:
topic person published_date
'world' JS 2016-05-05
'world' JS 2016-05-10
'nature' AR 2016-12-01
应该返回类似的东西
topic person quarter how_many_times
'world' JS 2 2
'nature' AR 4 1
我可以按主题和人物对它进行分组
select topic, person, published_date, count(*) from table group by topic, person, published_date
但是如何将 published_date 分成几个季度?
最佳答案
假设 published_date
是一个日期类型的列,您可以像这样使用 extract
函数:
select
topic,
person,
extract(quarter from published_date) as quarter,
count(*)
from
table1
group by
topic,
person,
extract(quarter from published_date)
order by
extract(quarter from published_date) asc
如果日期可以属于不同的年份,您可能希望将年份添加到选择和分组依据中。
关于postgresql - Postgres 按季度分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37071631/