Skip to content
Advertisement

SQL Server query for calculating time duration for column containing multiple datetime entries against order number and order state

I need a T-SQL query for calculating time duration for column containing multiple datetime entries against order number and order state.

Example:

enter image description here

I need the query to find out total time duration for each order number. Please note there can be multiple entries against order numbers.

For multiple Order number Entries (e.g.C8P0070), Order by event_time_t DESC & Then..

(Duration = (RESUME – HOLD) + (RESUME – HOLD) + … )

Expected Result:

enter image description here

Advertisement

Answer

Assuming the “resume” and “hold” have no duplicates, you can use lead() to get the next value and then aggregate:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement