I have three tables that look like this:
PROD
Prod_ID|Desc ------------ P1|Foo1 P2|Foo2 P3|Foo3 P4|Foo4 ...
RAM
Ram_ID|Desc ------------ R1|Bar1 R2|Bar2 R3|Bar3 R4|Bar4 ...
PROD_RAM
Prod_ID|Ram_ID ------------ P1|R1 P2|R2 P3|R1 P3|R2 P3|R3 P4|R3 P5|R1 P5|R2 ...
Between PROD and RAM there is a Many-To-Many relationship described by the PROD_RAM table .
Given Ram_ID set as (R1,R3) , I would like to find all PROD that have ONLY ONE or ALL RAM this set.
In this case (R1,R3) should be returned, for example, P1 , P4 and P5 ; P3 should not be returned because it has R1 and R3 , but also R2 .
What is the fastest request to get the entire PROD that has exactly one or one of the Ram_ID given RAM set?
EDIT:
The PROD_RAM table may contain a ratio greater than 1-> 3, so hard-coded checks for count = 1 OR = 2 are not a viable solution.
sql sql-server sql-server-2008
systempuntoout
source share