我怎样才能从Postgresql中的两个日期之间获取每周的重新记录?

时间:2018-10-31 07:19:41

标签: postgresql

我正在使用此查询逐日获取数据

SELECT 
     (count( server_time::timestamp::date)) , 
     server_time::timestamp::date 
FROM 
     complaint_details_v2 
WHERE 
     server_time between '2018/10/03' and '2018/10/11' 
GROUP BY 
     server_time::timestamp::date 
ORDER BY 
     server_time ASC

但是我想在两个日期之间按周而不是按天更改上述查询。

1 个答案:

答案 0 :(得分:0)

您可以通过执行日期算术将日期分组为七个。

053 016% BRAN       A0 
053 100% BRAN       B1 
053 100% BRAN       C01 
053 100% BRAN       E0 
054 100% NATURAL    A0
054 100% NATURAL    B1 
054 100% NATURAL    C01 
054 100% NATURAL    E0 
054 100% NATURAL    F0 
054 100% NATURAL    G0 

另一种替代方法是对表达式SELECT (count( server_time::timestamp::date)) , min(server_time::timestamp::date) as "week starting" FROM complaint_details_v2 WHERE server_time between '2018/10/03' and '2018/10/11' GROUP BY floor((server_time::timestamp::date - '2018/10/03'::date)/7) ORDER BY 2 ASC 进行分组,但这会将您绑定到ISO周