Search code examples
sqlsql-serversql-match-allrelational-division

SQL, matching all the values in column b and return column a for those matches


I'm only looking at one table, I have the values I need to match, so.

Col A / Col B
1 / 1
1 / 2
1 / 3
2 / 2
2 / 3
4 / 1
4 / 3

So the values I pass in would be 1 and 3 and I'd want to return 1 and 4.

I've tried a group by with a could, where I've had to use two sub queries, but it didn't work.

EDIT: Ideally I'd like to use a select query as I need to use this query within another query which I'm building up outside of sql server.

EDIT2: Ideally I'd like to pass in my input as a csv string


Solution

  • select ColA 
    from your_table
    where ColB in(1, 3)
    group by ColA
    having count(ColA) > 1