Skip to content
Advertisement

Oracle – Is there a way to rewrite my query to lower the number of times I’m querying a table

My modified query:

Data Model:

Desired Output:

For every ‘ORDER_NO’ I want to display only the row with ‘NEXT_STATUS’ of 620. Then in the same row derive two additional columns (SHIPPED_TIME and TIME_OF_DAY) with ‘DAYTIME’ values originating from rows with NEXT STATUS of 540 and 580 respectively, as the image below indicates.

enter image description here

What I have done:

I am getting the intended results from the sql statements below, but because the table is quite large it’s taking one and a half hours to complete the execution. Any help is appreciated.

Advertisement

Answer

Use analytic functions rather than self-joins:

(Note: PARTITION BY whatever conditions makes uniquely identifies the orders; your LEFT OUTER JOIN are just using ORDER_NO/SLDOCO so that is replicated in this query)

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