简体   繁体   English

按 7 天间隔分组 postgresql

[英]Group by 7 day interval postgresql

I know this is a common question, but I couldn't find something that matches my case.我知道这是一个常见问题,但我找不到与我的情况相匹配的东西。 I have this data:我有这个数据:

  id |    obs     
----+------------
  1 | 2018-01-01
  2 | 2018-01-02
  3 | 2018-01-03
  4 | 2018-01-04
  5 | 2018-01-05
  6 | 2018-01-06
  7 | 2018-01-07
  8 | 2018-01-15
  9 | 2018-01-20
 10 | 2018-02-03
 11 | 2018-02-04
 12 | 2018-02-05
 13 | 2018-02-06
 14 | 2018-02-06

I want this data to be grouped based on a 7 day interval.我希望根据 7 天的时间间隔对这些数据进行分组。 That is, the groups would be:也就是说,这些组将是:

  • Group 1: id 1 to 7第 1 组:id 1 到 7
  • Group 2: id 8 and 9第 2 组:id 8 和 9
  • Group 3: id 10 to 14第 3 组:id 10 到 14

How is this query in PostgreSQL? PostgreSQL 中的这个查询如何?

Thanks in advance提前致谢

I would proceed as follow:我将按照以下方式进行:

  • first, use a subquery to compare the date of the current record to the minimum date of the series;首先,使用子查询将当前记录的日期与系列的最小日期进行比较; the difference in days between the dates divided by 7 gives you a first version of the group the record belong to (but for now group numbers are not necessarily consecutive)日期之间的天数差异除以 7 为您提供记录所属组的第一个版本(但目前组编号不一定是连续的)
  • then, use DENSE_RANK() in an outer query to reassign group numbers as consecutive numbers:然后,在外部查询中使用DENSE_RANK()将组编号重新分配为连续编号:

Query:询问:

SELECT 
    id,
    obs,
    DENSE_RANK() OVER(ORDER BY gr) grp
FROM (
    SELECT 
        id,
        obs,
        MIN(obs) OVER(),
        (obs - MIN(obs) OVER())::int/7 + 1 gr
    FROM mytable
) x
ODER BY id

Demo on DB Fiddle ; DB Fiddle 上的演示

| id  | obs                      | grp |
| --- | ------------------------ | --- |
| 1   | 2018-01-01T00:00:00.000Z | 1   |
| 2   | 2018-01-02T00:00:00.000Z | 1   |
| 3   | 2018-01-03T00:00:00.000Z | 1   |
| 4   | 2018-01-04T00:00:00.000Z | 1   |
| 5   | 2018-01-05T00:00:00.000Z | 1   |
| 6   | 2018-01-06T00:00:00.000Z | 1   |
| 7   | 2018-01-07T00:00:00.000Z | 1   |
| 8   | 2018-01-15T00:00:00.000Z | 2   |
| 9   | 2018-01-20T00:00:00.000Z | 2   |
| 10  | 2018-02-03T00:00:00.000Z | 3   |
| 11  | 2018-02-04T00:00:00.000Z | 3   |
| 12  | 2018-02-05T00:00:00.000Z | 4   |
| 13  | 2018-02-06T00:00:00.000Z | 4   |
| 14  | 2018-02-06T00:00:00.000Z | 4   |

If you want to group things based on a gap of seven days, use lag() and a cumulative sum to define the groups:如果要根据 7 天的间隔对事物进行分组,请使用lag()和累积总和来定义组:

select t.*,
       count(*) filter (where prev_obs is null or prev_obs < obs - interval '7 day') over (order by obs) as grp
from (select t.*,
             lag(obs) over (order by obs) as prev_obs
      from t
     ) t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM