I have two tables, a header and a detail table:
header
id
name
detail
headerID
dueDate
extensionDate
I’m trying to make a query that will pull the ID and Name of a header who’s detail dueDate is 5 days from now (if extensionDate is null), or if there is an extensionDate, pull the header who has an extensionDate that is 5 days from now. Can this be done in a single query? I hope this makes sense.
I’m having trouble with determining if there is an extensionDate, and checking that, or if not, check the original dueDate. Thank you for your help!
Notes:
DATEDIFF(d, -5, getdate()gives you a date exactly 5 days in the future, without the time portion.EDIT (also contains alternative query)
Why EXISTS? First create these test tables. About 8% of details will match the conditions, which represents 5% of header records (unique).
Check the execution plans for the following two, using Ctrl-M
The first one is just slightly faster