You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 02:40:36