1

I am trying to come up with a Oracle SQL query to list all rows in "External_Order" table in which the order_dt value does not fall within start_dt and end_dt values in orders table for the same cust_id. Is there a way to achieve this using SQL without having to write PL/SQL?

**ORDERS**

CUST_ID  , START_DT,     END_DT
12345     , 01-01-2022,   01-15-2022
12345     , 02-01-2022,   02-28-2022

**EXTERNAL_ORDER**

CUST_ID, ORDER_DT,   AMOUNT
12345,   01-10-2022,  100
12345,   01-16-2022,  200
12345,   01-27-2022,  150
12345,   02-03-2022,  300

**EXPECTED OUTPUT**

CUST_ID  ORDER_ID ,   AMOUNT    
12345,   01-16-2022,  200
12345,   01-27-2022,  150

1 Answer 1

2

Perhaps EXISTS might do the trick?

SELECT eo.*
FROM   EXTERNAL_ORDER eo 
WHERE  NOT EXISTS ( 
       SELECT 1
       FROM   ORDERS o 
       WHERE  eo.CUST_ID = o.CUST_ID
       AND    o.START_DT <= eo.ORDER_DT 
       AND    o.END_DT >= eo.ORDER_DT

)

demo db<>fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

This works great. Thanks @SOS for your time and effort to help with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.