在CockroachDB中按本地日期的月日分组统计请求数据
在CockroachDB中按本地日期统计指定月份每日数据
我需要在CockroachDB中针对指定月份的每一天执行统计查询,初始查询语句如下:
select count(*), sum(amount) from request where code = 'code_string' and created_at >= '2022-07-31T20:30:00Z' and created_at < '2022-08-31T20:30:00Z'
需求是基于本地日期进行统计,最终要得到包含month、day、count、sum字段的月度统计结果。
解决方案
已找到可行的查询语句,通过提取本地日期的月和日进行分组统计:
select count(amount), sum(amount), extract(month from created_at) as monthTime, extract(day from created_at) as dayTime from request where code = 'code_string' and created_at >= '2022-07-31T20:30:00Z' and created_at < '2022-08-31T20:30:00Z' group by dayTime, monthTime
简化写法
感谢@histocrat提供的简化方案,可将提取日期部分的语句:
extract(month from created_at) as monthTime, extract(day from created_at) as dayTime
替换为:
date_part('month', created_at) as monthTime, date_part('day', created_at) as dayTime
内容的提问来源于stack exchange,提问作者Ali Hosseinzadeh
相关产品推荐
相关产品推荐

