I'm working on an Oracle SQL query of an asset management system. My query updates a data warehouse with information from the AMS's equipment table. Unfortunately the AMS is a bit rubbish, and the Equipment table doesn't include a last modified date, nor is there any way to get one using audits.
The Equipment table has 2,000,000 records, so I don't want to update the whole thing during office hours. Instead I want to limit it to Equipment linked to Work Orders which were updated recently. But I also need to update the whole table overnight to pick up any new records which haven't been attached to Work Orders. I'd like to avoid having two imports so I don't get in the situation where one import gets changed but the other doesn't.
What I'm after is a way that, based on the time of day, I can add an extra join.
So during office hours it would be:
SELECT
*
FROM
Equipment
INNER JOIN
( SELECT DISTINCT Equipment_ID FROM Work_Orders
WHERE Work_Orders.Last_Update > SYSDATE - 2
) WO
ON
Equipment.Equipment_ID = WO.Equipment_ID
But after say 9pm it would be
SELECT
*
FROM
Equipment
I've seen examples elsewhere which used joins where the ON criteria were conditional, however I haven't found anywhere where even having the join is conditional. Is this even possible?