微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

postgresql – Postgres group by quarter

我有列的表:topic,person,published_date.我想创建一个查询,帮助我计算每个人在每个季度在特定主题中写的次数.例:

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,published_date,count(*) from table group by topic,published_date

但是小组如何发表日期?

解决方法

假设published_date是日期类型列,您可以使用这样的提取函数

select 
  topic,extract(quarter from published_date) as quarter,count(*)
from 
  table1
group by 
  topic,extract(quarter from published_date)
order by 
  extract(quarter from published_date) asc

Sample SQL Fiddle

如果日期可能属于不同年份,您可能希望将年份添加到select和group by.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐