I am working on one of requirement the raw data is in following format
Requirement - Startdate should be the date when status changed to 1 and enddate should be the 1st date after the record status changed from 1 to any other number.
Customer | Status | Date |
---|---|---|
A123 | 0 | 7/2/2021 |
A123 | 0 | 7/15/2021 |
A123 | 0 | 7/22/2021 |
A123 | 1 | 8/18/2021 |
A123 | 1 | 9/8/2021 |
A123 | 0 | 12/1/2021 |
A123 | 0 | 1/21/2022 |
A123 | 1 | 3/6/2022 |
A123 | 1 | 3/7/2022 |
A123 | 0 | 3/15/2022 |
B123 | 1 | 1/1/2022 |
B123 | 0 | 1/6/2022 |
C123 | 1 | 1/2/2022 |
C123 | 2 | 1/8/2022 |
C123 | 0 | 1/9/2022 |
expected output
Customer | StartDate | EndDate |
---|---|---|
A123 | 8/18/2021 | 12/1/2021 |
A123 | 9/8/2021 | 12/1/2021 |
A123 | 3/6/2022 | 3/15/2022 |
A123 | 3/7/2022 | 3/15/2022 |
B123 | 1/1/2022 | 1/6/2022 |
C123 | 1/2/2022 | 1/8/2022 |
Query I tried to get the output is below, I am getting the output for Customer B123 and C123, but not for A123 as expected.
Query Explanation - In 1st part of query I am taking all the records with status = 1 and in next part taking only those records where status is not equal to 1, and joining these 2 datasets based on Customer and row number generated.
SELECT A.[Customer],A.StartDate,B.EndDate
from
(
SELECT [Customer],MIN(Date) AS STARTDATE,[Status],RANK() OVER (PARTITION BY [STATUS] ORDER BY Date ASC) AS ROWNUM
FROM table1
WHERE [STATUS] = 1
GROUP BY Customer,Date,[Status]
) A
LEFT JOIN
(
SELECT [Customer],MIN(Date) AS ENDDATE,[Status],RANK() OVER (PARTITION BY [STATUS] ORDER BY Date ASC) AS ROWNUM
FROM table1
WHERE [STATUS] != 1
AND Date>(
SELECT MIN(Date) AS STARTDATE
FROM table1
WHERE [STATUS] = 1
)
GROUP BY Customer,Date,[Status]
) B
ON
(
A.[Customer] = B.[Customer]
AND A.RowNum = B.RowNum
)
ORDER BY A.Startdate