I want to flag packages that weren't collected by customers for each relay point.
To identify a left package (not collected by customer) the status need to be ("Dropoff" or "preparation")
Requirements :
| Flag Cases | Example |
| ------------ | ------------ |
| The row needs to be the first row with status ID in (20,71) | Rows 7,34 |
| The row needs to be the first row with status ID in (20,71) Following a row with status in (50,51,90,91,30,60,10) | Rows 3,10,27.. |
The expected result :
Line Number | OrderNumber | RelayPoint | Status | DateShipper | F | ID Status |
---|---|---|---|---|---|---|
2 | XN75 | NULL | Shipment | 1/27/2022 15:29 | 0 | 10 |
3 | XN75 | 123 | Dropoff | 2/1/2022 7:15 | 1 | 20 |
4 | XN75 | 123 | Relay point Collection | 2/2/2022 7:50 | 0 | 90 |
5 | XN75 | 123 | Collected | 2/2/2022 7:50 | 0 | 91 |
6 | XN75 | 45678 | Shipper | 2/2/2022 16:02 | 0 | 30 |
7 | XN76 | 234 | Dropoff | 4/1/2022 9:16 | 1 | 20 |
8 | XN76 | 234 | Dropoff | 4/1/2022 9:17 | 0 | 20 |
9 | XN77 | 980 | Shipper Delivery | 9/22/2022 9:23 | 0 | 50 |
10 | XN77 | 980 | Preparation | 9/29/2022 23:00 | 1 | 71 |
11 | XN77 | 980 | Shipper Delivery | 10/1/2022 10:48 | 0 | 50 |
12 | XN77 | 980 | Preparation | 10/11/2022 16:48 | 1 | 71 |
13 | XN81 | 1314 | Dropoff | 10/10/2022 13:45 | 1 | 20 |
14 | XN81 | 1314 | Relay point Delivery | 10/11/2022 13:46 | 0 | 51 |
15 | XN81 | 1314 | Preparation | 10/11/2022 15:45 | 1 | 71 |
16 | XN81 | 1314 | Pickup | 10/12/2022 8:46 | 0 | 60 |
17 | XN79 | NULL | Shipment | 9/25/2022 20:38 | 0 | 10 |
18 | XN79 | 979 | Dropoff | 10/1/2022 9:16 | 1 | 20 |
19 | XN79 | 979 | Relay point Collection | 10/3/2022 10:14 | 0 | 90 |
20 | XN79 | 198271 | Shipper | 10/4/2022 15:05 | 0 | 30 |
21 | XN79 | 154 | Shipper Delivery | 10/9/2022 10:42 | 0 | 50 |
22 | XN79 | 154 | Relay point Delivery | 10/9/2022 10:43 | 0 | 51 |
23 | XN79 | 154 | Pickup | 10/9/2022 14:08 | 0 | 60 |
24 | XN79 | 711 | Dropoff | 10/10/2022 13:45 | 1 | 20 |
25 | XN79 | 108223 | Shipper | 10/11/2022 13:45 | 0 | 30 |
26 | XN80 | 980 | Shipper Delivery | 9/22/2022 9:23 | 0 | 50 |
27 | XN80 | 980 | Preparation | 9/29/2022 23:00 | 1 | 71 |
28 | XN80 | 980 | Shipper Delivery | 10/1/2022 10:48 | 0 | 50 |
29 | XN80 | 980 | Preparation | 10/11/2022 16:48 | 1 | 71 |
30 | XN80 | 980 | Shipper Delivery | 10/12/2022 10:48 | 0 | 50 |
31 | XN80 | 980 | Preparation | 10/13/2022 16:48 | 1 | 71 |
32 | XD30 | 340 | Dropoff | 11/17/2022 12:29 | 1 | 20 |
33 | XD30 | 340 | Pickup | 11/17/2022 12:48 | 0 | 60 |
34 | XD30 | 340 | Dropoff | 11/20/2022 18:24 | 1 | 20 |
35 | XD30 | 340 | Preparation | 11/20/2022 18:25 | 0 | 71 |
36 | XD30 | 340 | Collected | 11/20/2022 18:26 | 0 | 91 |
I tried the query as below but it doesn't give the right result.
;WITH cte
AS (SELECT *,Row_number() OVER (partition BY ordernumber, relaypoint ORDER BY dateshipper) AS RN
FROM #shipperstatushistory t1
WHERE status IN( 'Dropoff', 'preparation' )
AND EXISTS (SELECT 1
FROM #shipperstatushistory t2
WHERE status IN( 'shipper Delivery','Relay point Delivery','pickup','announcement','Shipment', 'collected', 'shipper','Relay point Collection')
AND t1.ordernumber = t2.ordernumber
AND t1.relaypoint = t2.relaypoint
AND t2.dateshipper > t1.dateshipper)
UNION
SELECT*,Row_number() OVER (partition BY ordernumber, relaypoint ORDER BY dateshipper) AS RN
FROM #shipperstatushistory t1
WHERE status IN( 'Dropoff', 'preparation' )
AND NOT EXISTS (SELECT 1
FROM #shipperstatushistory t2
WHERE status IN( 'shipper Delivery','Relay point Delivery','pickup','announcement','Shipment', 'collected', 'shipper','Relay point Collection')
AND t1.ordernumber = t2.ordernumber
AND t1.relaypoint = t2.relaypoint
AND t2.dateshipper > t1.dateshipper))
SELECT t.*,
CASE WHEN t2.id IS NOT NULL THEN 1 ELSE 0 END AS f
FROM #shipperstatushistory t
LEFT JOIN cte t2
ON t.id = t2.id
AND rn = 1
My query returns wrong values on OrderNumber XN81 and XN80 and XD30
Here is a sqlFiddle
Thanks for help !
1条答案
按热度按时间3xiyfsfu1#
Consider the following:
I've included your expected output as a
LEFT OUTER JOIN
for comparison purposes. We didn't know which IDs you intended for the second criteria, so all 8 are in there, but you can easily adjust that.