I tried to use compound conditions in the subquery, but it didn't return the result expected. Can you look into the example below to see why the query doesn't work out ?
Table : 1
EntityID StartDate EndDate
121 2013-08-01 2013-08-31
122 2013-08-01 2013-08-31
123 2013-08-01 2013-08-31
Table : 2
EntityID AttributeID AttributeValue
121 41 304
122 41 304
123 41 304
123 54 307
Now I'm trying to fetch based on AttributeID and AttribueValue from Table-2 and Stardate and enddate from table1 using following query. (Ex: The 41 and 304 and 54 and 307 got satisfied with 123 only I want fetch that 123 only one record)
SELECT pe.EntityID
FROM table1 pe
WHERE pe.StartDate = '2013-08-01'
AND pe.EndDate = '2013-08-31'
AND pe.EntityID IN (SELECT peaiv.EntityID
FROM table2 peaiv
WHERE peaiv.AttributeID IN (41)
AND peaiv.[Value] IN (304)
AND peaiv.EntityID IN (SELECT peaiv.EntityID
FROM
PT_EntityAttributesIntValues
peaiv
WHERE peaiv.AttributeID IN (54)
AND peaiv.[Value] IN (307))
EntitID
--------
121
122
123
The query returning the above result, but I'm expecting result only 123. Can any one try on this.