Question

I have data in a table like following

Name indicator
A     1
A     2
A     3
B     1
B     2
C     3

I want to get count of Names, for which both indicator 1,2 exists. In the preeceding example, this number is 2 (A & B both have indicator as 1, and 2).

The data I am dealing with is moderately large, and i need to get the similar information of some other permutations of (pre defined ) indicators (which i can change, once i get base query).

Was it helpful?

Solution

Try this:

SELECT Name
FROM Tablename
WHERE indicator IN(1, 2)
GROUP BY Name
HAVING COUNT(DISTINCT indicator) = 2;

See it in action here:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top