I want to measure the lead time for each stage of order fulfillment in store and these lead times should be available in an existing view.
The view has several columns and entries, but to simplify consider these two columns and this specific entry:
| ORDER_NR | Id |
| ------------ | ------------ |
| SE2844697 | 3824 |
And on the table [OnlineFulFillmentActionLogs] I have the different status according to each stage of the order fulfillment:
ACTION_DATE | OnlineFulFillmentId | Status |
---|---|---|
2023-04-18 12:27:31.0000000 | 3824 | 0 |
2023-04-18 12:43:20.0000000 | 3824 | 1 |
2023-04-18 12:43:46.0000000 | 3824 | 3 |
2023-04-18 12:46:26.0000000 | 3824 | 3 |
2023-04-18 12:46:26.0000000 | 3824 | 5 |
There are cases with more than one entry for the same status. I can solve this with a min(Action_Date), to get the date of the first occurrence. I can also calculate the lead time for each stage with this query:
ISNULL(DATEDIFF(HOUR,Column1,Column2),0) AS [LEAD_TIME_FIRST_STAGE] where,
- Column1 would be the Action_Date of the status=0;
- Column2 would be the Action_Date of the status=1, if existing;
But I'm struggling to figure out how to join this right table [OnlineFulFillmentActionLogs] to the existing view, since I have multiple entries with the same value OnlineFulFillmentId=3824 and I want to calculate the lead time for each status (from status 0 to status 1, from status 1 to status 2, from status 2 to status 3,...)
Expected result:
| ORDER_NR | Id | LEAD_TIME_FIRST_STAGE | LEAD_TIME_SECOND_STAGE | LEAD_TIME_THIRD_STAGE | ... |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| SE2844697 | 3824 | 16 | ... | ... | ... |
1条答案
按热度按时间xdyibdwo1#
First, the next and current state is specified. If the next record is equal to the specified next state, it is calculated
If the time Status columns are dynamic, you should use sql dynamic calculate the time with window function and then it is used pivot
Base Data: