如何从开始和结束日期中识别和汇总序列

时间:2017-01-06 14:26:59

标签: sql postgresql window-functions gaps-and-islands

我尝试在每个person的日期中识别连续序列,以及该序列的总和amount。我的records表看起来像这样:

person   start_date   end_date     amount
1        2015-09-10   2015-09-11   500
1        2015-09-11   2015-09-12   100
1        2015-09-13   2015-09-14   200
1        2015-10-05   2015-10-07   2000
2        2015-10-05   2015-10-05   300
2        2015-10-06   2015-10-06   1000
3        2015-04-23   2015-04-23   900

结果查询应为:

person   sequence_start_date   sequence_end_date     amount
1        2015-09-10            2015-09-14            800
1        2015-10-05            2015-10-07            2000
2        2015-10-05            2015-10-06            1400
3        2015-04-23            2015-04-23            900

下面,我可以使用LAG和LEAD来识别序列start_dateend_date,但我没有办法聚合amount。我假设答案将涉及某种ROW_NUMBER()窗口函数,它将按顺序进行分区,我只是无法弄清楚如何使序列可以识别该函数。

SELECT
 person
 ,COALESCE(sequence_start_date, LAG(sequence_start_date, 1) OVER (ORDER BY person, start_date)) AS "sequence_start_date"
 ,COALESCE(sequence_end_date, LEAD(sequence_end_date, 1) OVER (ORDER BY person, start_date)) AS "sequence_end_date"
FROM
(
 SELECT
  person
  ,start_date
  ,end_date
  ,CASE WHEN LAG(end_date, 1) OVER (PARTITION BY person ORDER BY start_date) + interval '1 day' = start_date
   THEN NULL
   ELSE start_date
  END AS "sequence_start_date"
  ,CASE WHEN LEAD(start_date, 1) OVER (PARTITION BY person ORDER BY start_date) - interval '1 day' = end_date
   THEN NULL
   ELSE end_date
  END AS "sequence_end_date"
  ,amount
 FROM records
) sq

2 个答案:

答案 0 :(得分:1)

为什么不:

select a1.person, a1.sequence_start_date, a1.sequence_end_date, 
       sum(rx.amount) 
         as amount
from (EXISTING_QUERY) a1
left join records rx 
  on rx.person = a1.person 
  and rx.start_date >= a1.start_date
  and rx.end_date <= a1.end_date
group by a1.person, a1.sequence_start_date, a1.sequence_end_date

答案 1 :(得分:1)

即使您更新的(子)查询仍然不适合您所呈现的数据,这与序列中第二行和后续行的开始日期是否应该等于其前一行的结尾不一致日期或一天后。如果需要,可以非常轻松地更新查询以适应两者。

在任何情况下,您都不能将COALESCE用作窗口功能。聚合函数可以通过提供OVER子句而不是普通函数用作窗口函数。然而,有一些方法可以将窗口功能应用于此任务。这是一种识别数据中序列的方法(如图所示):

SELECT
  person
  ,MAX(sequence_start_date)
     OVER (
       PARTITION BY person
       ORDER BY start_date
       ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
     AS "sequence_start_date"
  ,MIN(sequence_end_date)
     OVER (
       PARTITION BY person
       ORDER BY start_date
       ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
     AS "sequence_end_date"
  ,amount
FROM
(
 SELECT
  person
  ,start_date
  ,end_date
  ,CASE WHEN LAG(end_date, 1) OVER (PARTITION BY person ORDER BY start_date) + interval '1 day' >= start_date
   THEN date '0001-01-01'
   ELSE start_date
   END AS "sequence_start_date"
  ,CASE WHEN LEAD(start_date, 1) OVER (PARTITION BY person ORDER BY start_date) - interval '1 day' <= end_date
   THEN NULL
   ELSE end_date
   END AS "sequence_end_date"
  ,amount
 FROM records
 order by person, start_date
) sq_part
ORDER BY person, sequence_start_date

这取决于MAX()MIN()而不是COALESCE(),它应用窗口框架为每个分区中的每个分区获取适当的范围。结果:

person  sequence_start_date         sequence_end_date           amount
1       September, 10 2015 00:00:00 September, 12 2015 00:00:00 500
1       September, 10 2015 00:00:00 September, 12 2015 00:00:00 100
1       October, 05 2015 00:00:00   October, 07 2015 00:00:00   2000
2       October, 05 2015 00:00:00   October, 06 2015 00:00:00   300
2       October, 05 2015 00:00:00   October, 06 2015 00:00:00   1000
3       April, 23 2015 00:00:00     April, 23 2015 00:00:00     900

请注意,这不需要结束日期与后续开始日期完全匹配;所有与或重叠相邻的人的行将被分配到相同的序列。但是,如果(personstart_date)不能依赖于唯一,那么您可能还需要按结束日期对分区进行排序。

现在你有办法识别序列:它们的特点是三重person, sequence_start_date, sequence_end_date。 (或者实际上,您只需要这些日期的人和一个用于识别目的,但请继续阅读。)您可以将上述查询包装为外部聚合查询的内联视图,以产生您想要的结果:

SELECT
  person,
  sequence_start_date,
  sequence_end_date,
  SUM(amount) AS "amount"
FROM ( <above query> ) sq
GROUP BY person, sequence_start_date, sequence_end_date

当然,如果您要选择日期,则需要将两个日期作为分组列。