sixtytrees sixtytrees - 4 months ago 7
SQL Question

Select a list of entries in Column A where column B = 'value'. Then select all entries where column A is one of the list

0
down vote
favorite

I have an SQL table that has entries like:

RuleID Symbol
1 OR
1 123
1 363

2 AND
2 847
2 287

3 AND
3 NOT
3 234
3 867

4 NOT
4 995


The result should be:

3 AND
3 NOT
3 234
3 867

4 NOT
4 995


Symbol='NOT' corresponds to RuleID='3' and RuleID='4'. I want to

select * in table where RuleID = '3' or '4'


How can I do this with SQL server?

Answer

To do it using Joins which is generally faster than using the IN clause

SELECT T.* 
from table T INNER JOIN 
( SELECT Distinct RuleID 
  FROM table 
  WHERE SYMBOL = 'not' 
) A
ON A.RuleID = T.RuleID