Missing SQL row
This is the sample table I'm using and I'm trying to find non duplicate rows in the table:
ID status_old status_new assign Pre_status S_TIMESTAMP S_TIMESTAMP_next
------------------------------------------------------------------------------------------
1234 NULL A NULL B 7/13/2021 12:19:06 PM 7/13/2021 12:19:22 PM
1234 A B NULL C 7/13/2021 12:19:22 PM 7/13/2021 12:19:22 PM
1234 B C NULL C 7/13/2021 12:19:22 PM 7/13/2021 12:19:23 PM
1234 C C NULL C 7/13/2021 12:19:23 PM 7/13/2021 12:20:24 PM
1234 C C NULL D 7/13/2021 12:20:24 PM 7/13/2021 12:37:03 PM
1234 C D NULL D 7/13/2021 12:37:03 PM 7/13/2021 12:37:09 PM
1234 D D NULL D 7/13/2021 12:37:09 PM 7/13/2021 1:02:01 PM
1234 D D 555 D 7/13/2021 1:02:01 PM 7/13/2021 1:02:07 PM
1234 D D 555 E 7/13/2021 1:02:07 PM 7/13/2021 2:59:40 PM
1234 D E 555 E 7/13/2021 2:59:40 PM 7/13/2021 3:00:26 PM
1234 E E 555 E 7/13/2021 3:00:26 PM 7/18/2021 5:57:38 PM
1234 E E 555 F 7/18/2021 5:57:38 PM 7/18/2021 5:59:55 PM
1234 E F 555 NULL 7/18/2021 5:59:55 PM null
I wrote these SQL queries to filter the rows and display the non duplicated row
INSERT INTO #sub_tableY (ID, status_old, status_new, assign, pre_status, S_TIMESTAMP, S_TIMESTAMP_next)
SELECT
ID, status_old, status_new, assig,
LEAD(status_new, 1, NULL) OVER (PARTITION BY ID ORDER BY S_TIMESTAMP) pre_status,
S_TIMESTAMP,
LEAD(S_TIMESTAMP, 1, NULL) OVER (PARTITION BY ID ORDER BY S_TIMESTAMP) S_TIMESTAMP_next
FROM
TABLE
WHERE
(status_old != status_new OR status_old IS NULL)
AND (assign IS NULL OR assign IS NOT NULL)
GROUP BY
ID, status_old, status_new, S_TIMESTAMP, assign, pre_status
This is the result I get:
ID status_old status_new assign Pre_status S_TIMESTAMP S_TIMESTAMP_next
----------------------------------------------------------------------------------------------
1234 NULL A NULL B 7/13/2021 12:19:06 PM 7/13/2021 12:19:22 PM
1234 A B NULL C 7/13/2021 12:19:22 PM 7/13/2021 12:19:22 PM
1234 B C NULL D 7/13/2021 12:19:22 PM 7/13/2021 12:37:03 PM
1234 C D NULL E 7/13/2021 12:37:03 PM 7/13/2021 2:59:40 PM
1234 D E 555 F 7/13/2021 2:59:40 PM 7/18/2021 5:57:38 PM
1234 E F 555 NULL 7/18/2021 5:57:38 PM NULL
but I expecting the this as result:
ID status_old status_new assign Pre_status S_TIMESTAMP S_TIMESTAMP_next
----------------------------------------------------------------------------------------------
1234 NULL A NULL B 7/13/2021 12:19:06 PM 7/13/2021 12:19:22 PM
1234 A B NULL C 7/13/2021 12:19:22 PM 7/13/2021 12:19:22 PM
1234 B C NULL D 7/13/2021 12:19:22 PM 7/13/2021 12:37:03 PM
1234 C D NULL D 7/13/2021 12:37:03 PM 7/13/2021 1:02:01 PM
>1234 D D 555 E 7/13/2021 1:02:01 PM 7/13/2021 2:59:40 PM
1234 D E 555 F 7/13/2021 2:59:40 PM 7/18/2021 5:57:38 PM
1234 E F 555 NULL 7/18/2021 5:57:38 PM NULL
I'm expecting the query to include the row that contain the assign value for first time
1234 D D 555 E 7/13/2021 1:02:01 PM 7/13/2021 2:59:40 PM
but it's not displayed with my current query
from Recent Questions - Stack Overflow https://ift.tt/3kIOKbQ
https://ift.tt/eA8V8J
Comments
Post a Comment