SQL-如果日期连接则合并行

时间:2019-01-22 18:05:29

标签: sql oracle gaps-and-islands

我有带有行的表:clientid,startdate和enddate。同一客户ID的日期不能重叠。 如果日期连接,我想为每个客户端合并行。

表如下:

clientid  startdate      enddate
1         10.10.2017     12.10.2017
1         12.10.2017     13.10.2017
1         13.10.2017     17.10.2017
1         10.11.2017     17.11.2017
1         17.11.2017     23.11.2017
1         12.12.2017     14.12.2017
2         10.11.2017     15.11.2017
2         01.12.2017     02.12.2017
2         02.12.2017     05.12.2017

最终表应如下所示:

clientid  startdate      enddate
    1     10.10.2017     17.10.2017
    1     10.11.2017     23.11.2017
    1     12.12.2017     14.12.2017
    2     10.11.2017     15.11.2017
    2     01.12.2017     05.12.2017

谢谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您可以对sum进行聚合并使用lag窗口函数,如下所示:

select clientid, min(startdate) as startdate, max(enddate) as enddate
  from
(
select tt.*, sum(grp) over (order by clientid, startdate) sm 
  from
(
  with t(clientid, startdate, enddate) as
  (
   select 1, date'2017-10-10', date'2017-10-12' from dual union all
   select 1, date'2017-10-12', date'2017-10-13' from dual union all
   select 1, date'2017-10-13', date'2017-10-17' from dual union all  
   select 1, date'2017-11-10', date'2017-11-17' from dual union all  
   select 1, date'2017-11-17', date'2017-11-23' from dual union all  
   select 1, date'2017-12-12', date'2017-12-14' from dual union all
   select 2, date'2017-11-10', date'2017-11-15' from dual union all  
   select 2, date'2017-12-01', date'2017-12-02' from dual union all  
   select 2, date'2017-12-02', date'2017-12-05' from dual
  )
 select clientid, 
        decode(nvl(lag(enddate) over 
                   (order by enddate),startdate),startdate,0,1) 
                   as grp, --> means prev. value equals or not 
        row_number() over (order by clientid, enddate) as rn, startdate, enddate
    from t
) tt
order by rn
) 
group by clientid, sm 
order by clientid, enddate;

CLIENTID    STARTDATE   ENDDATE
----------  ----------  ----------
1           10.10.2017  17.10.2017
1           10.11.2017  23.11.2017
1           12.12.2017  14.12.2017
2           10.11.2017  15.11.2017
2           01.12.2017  05.12.2017

Rextester Demo

Step by Step Query Execution for better understanding

答案 1 :(得分:0)

这是SQL Server语法,与Barbaros的方法相同。为了尽量保持纯正,我尝试进行自我联接而不是使用LAG,但这无疑使查询更难以阅读。

SELECT clientid, MIN(startdate) AS startdate, MAX(enddate) AS enddate
FROM (SELECT *, SUM(CASE WHEN a.enddate_prev = a.startdate THEN 0 ELSE 1 END) OVER (ORDER BY clientid, startdate) sm
      FROM (SELECT clientid, startdate, enddate,  
                   LAG(enddate, 1, NULL) OVER (PARTITION BY clientid ORDER BY clientid, enddate) enddate_prev
            FROM client_dates) a) b
GROUP BY clientid, sm

像这样设置表格:

CREATE TABLE client_dates (clientid INT NOT NULL, startdate DATE NOT NULL, enddate DATE NOT NULL);

INSERT INTO client_dates VALUES (1, TRY_PARSE('10.10.2017' AS datetime USING 'en-GB'), TRY_PARSE('12.10.2017' AS datetime USING 'en-GB'));
INSERT INTO client_dates VALUES (1, TRY_PARSE('12.10.2017' AS datetime USING 'en-GB'), TRY_PARSE('13.10.2017' AS datetime USING 'en-GB'));
INSERT INTO client_dates VALUES (1, TRY_PARSE('13.10.2017' AS datetime USING 'en-GB'), TRY_PARSE('17.10.2017' AS datetime USING 'en-GB'));
INSERT INTO client_dates VALUES (1, TRY_PARSE('10.11.2017' AS datetime USING 'en-GB'), TRY_PARSE('17.11.2017' AS datetime USING 'en-GB'));
INSERT INTO client_dates VALUES (1, TRY_PARSE('17.11.2017' AS datetime USING 'en-GB'), TRY_PARSE('23.11.2017' AS datetime USING 'en-GB'));
INSERT INTO client_dates VALUES (1, TRY_PARSE('12.12.2017' AS datetime USING 'en-GB'), TRY_PARSE('14.12.2017' AS datetime USING 'en-GB'));
INSERT INTO client_dates VALUES (2, TRY_PARSE('10.11.2017' AS datetime USING 'en-GB'), TRY_PARSE('15.11.2017' AS datetime USING 'en-GB'));
INSERT INTO client_dates VALUES (2, TRY_PARSE('01.12.2017' AS datetime USING 'en-GB'), TRY_PARSE('02.12.2017' AS datetime USING 'en-GB'));
INSERT INTO client_dates VALUES (2, TRY_PARSE('02.12.2017' AS datetime USING 'en-GB'), TRY_PARSE('05.12.2017' AS datetime USING 'en-GB'));