如何将日期按5天分组,而不是按周将标准date_trunc分组

时间:2019-02-25 22:08:21

标签: sql postgresql amazon-redshift

我需要按5天的增量将日期分组,而不是标准的date_trunc('week',date),以便对该分组进行count(*)

有人可以帮助我吗?

提前谢谢

1 个答案:

答案 0 :(得分:1)

您将获得最短的日期,取差值并除以5:

select ( datediff(day, min_date, date) / 5 )::int as period_num,
       count(*)
from (select t.*, min(date) over (order by date) as min_date
      from t
     ) t
group by 1;