Need Help with DB2 Query Modification
I’m working on a data processing task where I need to pull information from multiple database tables and send it to another system. My current query works but returns too many records at once.
Current Setup:
- Orders table: contains order_id, item_seq, process_date, description, notes
- Items table: has order_id, category, ship_date
- Status table: includes order_id, step_num, update_date
Right now I’m using this query structure:
select order_id, item_seq, process_date, description, notes, category, ship_date, step_num, update_date
from orders
left outer join items
on (orders.order_id = items.order_id) and (orders.item_seq = items.item_seq)
left outer join status
on (orders.order_id = status.order_id)
where process_date = '2019-01-01'
Except
select order_id, item_seq, process_date, description, notes, category, ship_date, step_num, update_date
from orders
left outer join items
on (orders.order_id = items.order_id) and (orders.item_seq = items.item_seq)
left outer join status
on (orders.order_id = status.order_id)
where process_date = '2019-01-01'
and orders.notes = 'COMPLETE'
This returns hundreds of rows across many different order numbers. But I want to process one order at a time. How can I modify this to only get records for the first order_id, then after I process those and update their status, the next query run would get the next order_id?
For example, if my result set has orders 1001, 1002, 1003, I want just the 1001 records first time, then 1002 records next time, etc.