I have the following data and I want to be able to put all the rows into one line based on the stop type id. So a stop type is in order which means a 0 or 2 will come before a 3. I believe Lead is what I want to use, but doesn't seem to be working like I want and haven't been able to figure out why.
This is what the raw data looks like based on the GMT Date time.
OrderId GmtDeliveryDateTime StopTypeId
3650 2019-01-11 13:04:44.000 0
3650 2019-01-11 14:22:09.000 3
3650 2019-01-11 15:13:35.000 2
3650 2019-01-11 16:05:14.000 3
And I want to get it to look like this:
OrderId GmtDeliveryDateTime StopTypeId GmtDeliveryDateTime StopTypeId
3650 2019-01-11 13:04:44.000 0 2019-01-11 14:22:09.000 3
3650 2019-01-11 15:13:35.000 2 2019-01-11 16:05:14.000 3
Here is the query I am using:
SELECT *
FROM (
SELECT OrderId,
GmtDeliveryDateTime,
StopTypeId,
LEAD(StopTypeId) OVER (ORDER BY GmtDeliveryDateTime, StopTypeId) NxtStop
FROM table
)
Here are the result the above produces:
OrderId GmtDeliveryDateTime StopTypeId NxtStop
3650 2019-01-11 13:04:44.000 0 2
3650 2019-01-11 15:13:35.000 2 2
3650 2019-01-11 14:22:09.000 3 3
3650 2019-01-11 16:05:14.000 3 2
What is wrong with my query?