简体   繁体   English

DB2 SQL配对日期

[英]DB2 SQL Pairing Dates

I am trying to pair up dates that I am getting from my SQL. 我正在尝试配对从SQL获取的日期。 The output at the moment looks something like this: 目前的输出如下所示:

 start_date   end_date 
 2015-02-02  2015-02-02  
 2015-02-02  2015-02-03    
 2015-02-03  2015-02-03  
 2015-04-12  2015-02-12

I would like the ouput to be paired up so that the smallest and the biggest date of a date group is chosen, so that the output would look like this: 我希望将输出配对,以便选择日期组的最小日期和最大日期,以便输出看起来像这样:

 start_date   end_date 
 2015-02-02  2015-02-03    
 2015-04-12  2015-02-12

Using the first response I get something like this, I believe I have formatted this wrong, I am getting the same date pairs as before, but it does run. 使用第一个响应,我得到这样的信息,我相信我已经设置了错误的格式,我得到了与以前相同的日期对,但是它确实可以运行。

select min(date), max(date)
from (select date,
         sum(case when sum(inc) = 0 then 1 else 0 end) over (order by   date desc) as grp
  from (select t1.datev as date, 1 as inc
        from  table2 t1, 
              table3 c, 
              table4 cr

where t1.datev between date(c.e_start_date) and date(c.e_end_date)
and   t1.datev not in (select date(temp.datev) from mdmins11.temp temp where temp.number < 4000 and temp.organisation_id = 11111)
and   c.tp_cd in (1,6)
and   cr.from_id = c.id
and   cr.organisation_id = 11111

        union all
        select t.datev as date, -1 as inc
        from  table1 t,
              table3 c, 
              table4 cr

where t.datev between date(c.e_start_date) and date(c.e_end_date)
and   t.datev not in (select date(temp.datev) from mdmins11.temp temp where temp.number < 4000 and temp.organisation_id = 11111)
and   c.tp_cd in (1,6)
and   cr.from_id = c.id
and   cr.organisation_id = 11111
       ) t
  group by date
 ) t
group by grp;

One method is to determine where groups of non-overlapping dates start. 一种方法是确定非重叠日期组的开始位置。 For this, you can use not exists . 为此,可以使用not exists Then count up this flag over all records. 然后将该标志加到所有记录上。 This uses window functions. 这使用窗口功能。 However, this poses problems because you have multiple starts on the same date. 但是,这会带来问题,因为您在同一日期有多个起点。

Another method is to keep track of starts and stops and note where the sum is zero. 另一种方法是跟踪开始和停止,并记下总和为零。 These represent boundaries between groups. 这些代表了群体之间的界限。 The following should work on your data: 以下应适用于您的数据:

select min(date), max(date)
from (select date,
             sum(case when sum(inc) = 0 then 1 else 0 end) over (order by date desc) as grp
      from (select start_date as date, 1 as inc
            from table
            union all
            select end_date as date, -1 as inc
            from table
           ) t
      group by date
     ) t
group by grp;

This type of problem is made more complicated when duplicate values are allowed on a given date. 在给定日期允许重复值时,这种类型的问题会变得更加复杂。 Given only the dates, this is challenging. 仅给出日期,这是具有挑战性的。 With a separate unique id for each row, then there are more robust solutions. 每行都有一个唯一的唯一ID,那么就会有更强大的解决方案。

EDIT: 编辑:

A more robust solution: 一个更强大的解决方案:

select min(start_date), max(end_date)
from (select t.*, sum(StartGroupFlag) over (order by start_date) as grp
      from (select t.*,
                   (case when not exists (select 1
                                          from table t2
                                          where t2.start_date < t.start_date and
                                                t2.end_date >= t.start_date
                                         )
                         then 1 else 0
                    end) as StartGroupFlag
            from table t
           ) t
      ) t
group by grp;

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

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