gpt4 book ai didi

postgresql - 我如何在 postgresql 中的两个日期之间获取记录月份(按月份分组日期)?

转载 作者:行者123 更新时间:2023-11-29 13:42:33 26 4
gpt4 key购买 nike

I am using below query for fetching the record month wise but it give wrong data

SELECT 
(count( server_time::timestamp::date)) ,
min(server_time::timestamp::date) as "Month Date"
FROM
complaint_details_v2
WHERE
server_time between '2018/08/01' and '2018/10/30'
GROUP BY
floor((server_time::timestamp::date - '2018/08/01'::date)/30)
ORDER BY
2 ASC

结果

Count Month Date
2774 2018-08-01
5893 2018-08-31
1193 2018-09-30

但是结果会是

Count Month Date
2774 2018-08-01
5893 2018-09-01
1193 2018-10-01

最佳答案

使用date_trunc

demo:db<>fiddle

SELECT
count(*),
date_trunc('month', servertime)::date as month_date
FROM log
GROUP BY date_trunc('month', servertime)
ORDER BY 2

关于postgresql - 我如何在 postgresql 中的两个日期之间获取记录月份(按月份分组日期)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53114264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com