加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – Postgres group by quarter

发布时间:2020-12-13 16:05:12 所属栏目:百科 来源:网络整理
导读:我有列的表: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 quart
我有列的表: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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读