Hi,
I have a table ShipperStatusHistory that tracks orders, Shipping status and relay points (for customers where to collect packages) . .
I want to calculate the number of package That weren''t collected by customers for each a relay point.
To identify a left package (not collected by customer) the status need to be ("Dropoff" or "preparation") ***OR ***("Dropoff" or "preparation") and after the status becomes "collected" or "shipper".
But if i have other status ("shipper Delivery","Relay point Delivery","pickup","announcement") after "Dropoff" or "preparation" the collecatable packages is canceled.
We have other status like "end of time" or "Relay point Collection" but we can ignore them.
AS Bellow a sample of data with The result should look like
Create table #ShipperStatusHistory( id Int primary key identity, OrderNumber varchar(50), RelayPoint INT, Status varchar(50), DateShipper Datetime )
insert into #ShipperStatusHistory values('XN75',NULL,'Shipment','2022-01-27 15:29:22.000') ,('XN75',123,'Dropoff','2022-02-01 07:15:53.000') ,('XN75',123,'Relay point Collection','2022-02-02 07:50:29.000') ,('XN75',123,'collected','2022-02-02 07:50:45.000') ,('XN75',45678,'shipper','2022-02-02 16:02:37.000') ,('XN76',NULL,'Shipment','2022-03-25 20:38:19.000') ,('XN76',234,'Dropoff','2022-04-01 09:16:51.000') ,('XN76',234,'Relay point Collection','2022-04-03 10:14:06.000') ,('XN76',16734,'shipper','2022-04-04 15:05:30.000') ,('XN76',456,'shipper delivery','2022-04-09 10:42:01.000') ,('XN76',456,'Relay point Delivery','2022-04-09 10:43:46.000') ,('XN76',456,'Pickup','2022-04-09 14:08:50.000') ,('XN77',NULL,'Shipment','2022-09-05 18:44:19.000') ,('XN77',555,'Dropoff','2022-09-09 08:38:39.000') ,('XN77',555,'Relay point Collection','2022-09-09 09:33:07.000') ,('XN77',555,'collected','2022-09-09 09:33:09.000') ,('XN77',198721,'shipper','2022-09-11 11:17:48.000') ,('XN77',980,'Relay point Delivery','2022-09-22 09:23:35.000') ,('XN77',980,'End of time','2022-09-29 23:00:06.000') ,('XN77',980,'Preparation','2022-10-01 10:48:04.000') ,('XN77',980,'Pickup','2022-10-11 16:48:30.000') ,('XN78',NULL,'Shipment','2022-09-05 18:44:19.000') ,('XN78',555,'Dropoff','2022-09-09 08:38:39.000') ,('XN78',555,'Relay point Collection','2022-09-09 09:33:07.000') ,('XN78',555,'collected','2022-09-09 09:33:09.000') ,('XN78',198721,'shipper','2022-09-11 11:17:48.000') ,('XN78',980,'Relay point Delivery','2022-09-22 09:23:35.000') ,('XN78',980,'End of time','2022-09-29 23:00:06.000') ,('XN78',980,'Preparation','2022-10-01 10:48:04.000') ,('XN79',NULL,'Shipment','2022-09-25 20:38:19.000') ,('XN79',979,'Dropoff','2022-10-01 09:16:51.000') ,('XN79',979,'Relay point Collection','2022-10-03 10:14:06.000') ,('XN79',198271,'shipper','2022-10-04 15:05:30.000') ,('XN79',154,'shipper Delivery','2022-10-09 10:42:01.000') ,('XN79',154,'Relay point Delivery','2022-10-09 10:43:46.000') ,('XN79',154,'Pickup','2022-10-09 14:08:50.000') ,('XN79',711,'Dropoff','2022-10-10 13:45:59.000') ,('XN79',108223,'shipper','2022-10-11 13:45:59.000')
The result expected :

Any idea how can i do that ?
Thanks for help !