I have 2 tables. I want to select an Id from one table where a row does not exist in another table based on a date... Only I can't seem to find the correct row in the second table
Table 1
adId info tableX tableY
1 blah, blah Y N
2 blah, blah N Y
3 blah, blah N N
4 blah, blah N Y
5 blah, blah N Y
Table 2
id start_date unitId adId
1 2014-04-01 1 1
2 2014-03-02 2 1
3 2014-04-01 2 2
4 2014-03-01 0 4
The relationship of the unitID in Table2 comes from two tables X and Y (I'm not sure if its relevant but I'll describe it in case it is)
TableX
unitId adid
1 1
2 1
TableY
unitId adId
1 5
2 2
3 4
I want to select adId=1, adId=3, adId=5, from Table 1 as in Table 2, none of them have a date in March (NB adId=2 and 5 does not appear in Table 2 at all)
This will find adId= 2 and 5 but not 1.
When I do SELECT TOP (100) PERCENT Table1.adId
FROM Table1
WHERE (NOT EXISTS
(SELECT id, start_date
FROM Table2
WHERE (Table1.adId = adId) AND (Table2.start_date BETWEEN '2014-03-01'AND '2014-03-05)
)
)
How do you add a clause so that its looking for DISTINCT on adId and UnitID?
Is DISTINCT the best way to approach this or is there another way?