This is a bit tricky to explain, but I will do my best.
Say I have two tables:
Dates (DateColumn datetime)
SomeData (ID1 int, ID2 int, SomeDate datetime)
Dates ALWAYS contains the following dates:
1/1/2012
1/2/2012
1/3/2012
SomeData contains data similar to this:
ID ID1 ID2 SomeDate
1 1 4 1/1/2012
2 1 4 1/2/2012
3 2 3 1/1/2012
4 2 3 1/3/2012
6 5 1 1/2/2012
Now, notice how in SomeData, some of the rows have the same ID1 and ID2 combinations. Also notice how some of them are missing certain dates found in the Dates table. I need to somehow join each of those combinations with EVERY date in the Dates table, to find the missing dates.
So on joining, the data would end up looking like this:
ID ID1 ID2 SomeDate
1 1 4 1/1/2012
2 1 4 1/2/2012
NULL 1 4 1/3/2012 <-- missing from SomeData table
3 2 3 1/1/2012
NULL 2 3 1/2/2012 <-- missing from SomeData table
4 2 3 1/3/2012
NULL 5 1 1/1/2012 <-- missing from SomeData table
6 5 1 1/2/2012
NULL 5 1 1/3/2012 <-- missing from SomeData table
Notice how each ID1+ID2 combination each returns 3 records, using all the dates in the Dates table, and also notice how each “missing” record maintains the ID1+ID2 combination. Any idea how I can pull this off?
Thanks in advance!
CROSS JOIN is your friend here. Here’s a full worked example
So to break it down: