Group rows with that are less than 15 days apart and assign min/max date

不羁的心 提交于 2021-02-10 20:38:40

问题


If the protocol_opening_date of different protocols are within 15 days of each other, i need to show them as one protocol in another column named expected start date.

I don't know how to copy my table here as it looks but i'll try to explain as much as i can.

So let's say if one protocol has the start_date of 24.01.2018 and end_date of 30.01.2018 and an other one has start_date of 25.01.2018 and end_date of 10.02.2018

I need to display them as a different protocol with a start_date of 24.01 and end_date of 10.02 because they are 15 days within each other.

Another big issue is that i don't have a clue how to compare these two protocols with a third or fourth one. Because even if there is more than 2 protocols in the same date interval, i don't know how to compare them to each other.

Edit: I added a portion of the table, same colors are the protocols that's been combined into one protocol. Beklenen_baslangıc == expected_start_date


回答1:


As mentioned in comments, you could use the LAG function for this. The basic idea is to assign a 0/1 value to each row: if it is within 15 days of previous row then 0 else 1. Then use SUM() OVER () to convert the 1s and 0s to numbers which could be used for grouping.

Note that this could group much longer date ranges together e.g. 01-01, 01-11, 01-21, 02-01 and 02-11 will be grouped together although the first and last dates are more than 15 days apart.

DECLARE @T TABLE (HASTA_ID INT, PROTOKOL_ID INT, STARTDATE DATE, ENDDATE DATE);
INSERT INTO @T VALUES
(273065, 11, '2018-01-24', '2018-01-30'),
(273065, 12, '2018-01-25', '2018-02-10'),
(273065, 13, '2018-01-30', '2018-01-30'),
(273065, 14, '2018-02-23', '2018-02-28'),
(273065, 15, '2018-03-21', '2018-03-29'),
(273065, 16, '2018-05-03', '2018-05-04'),
(273065, 17, '2018-05-03', '2018-05-08'),
(273065, 18, '2018-05-14', '2018-05-22'),
(273065, 19, '2018-05-22', '2018-05-23'),
(273065, 20, '2018-09-20', '2018-09-30');

WITH CTE1 AS (
    SELECT *, CASE WHEN LAG(STARTDATE) OVER (PARTITION BY HASTA_ID ORDER BY STARTDATE) >= DATEADD(DAY, -14, STARTDATE) THEN 0 ELSE 1 END AS CHG
    FROM @T
), CTE2 AS (
    SELECT *, SUM(CHG) OVER (PARTITION BY HASTA_ID ORDER BY STARTDATE) AS GRP
    FROM CTE1
)
SELECT *,
    MIN(STARTDATE) OVER (PARTITION BY HASTA_ID, GRP) AS EX_STARTDATE,
    MAX(ENDDATE) OVER (PARTITION BY HASTA_ID, GRP) AS EX_ENDDATE
FROM CTE2
ORDER BY HASTA_ID, STARTDATE

Demo on DB Fiddle



来源:https://stackoverflow.com/questions/53519702/group-rows-with-that-are-less-than-15-days-apart-and-assign-min-max-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!