Skip to content
Advertisement

Oracle SQL – Selecting first inspection row after delivery row

I am struggling with the following problem.

The data in tblTrans is below:

What I am trying to accomplish is determining the first inspection transaction after each In-Place Delivery, creating a table that looks like:

The Transaction_ID for the forthcoming inspection after a delivery will always be greater than the previous deliveries. It goes in order. However, it may not necessarily be +1 as sometimes the system jumps numbers. However, it will always be greater.

I so far have tried a few variants of the below query:

But I get:

which is clearly incorrect.

I have set up a SQL FIDDLE here, for demo purposes, with the data and the query.

How can I redesign my query to achieve the desired output?

Advertisement

Answer

Simply using LEAD IGNORE NULLS:

See fiddle

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