Question

The simplified version of the DB:

fkey|count1|count2|count3|feature
100|0|1|0|feature1
100|0|1|0|feature1
100|0|1|0|feature2

101|0|1|0|feature3
101|0|1|0|feature4

102|1|0|0|feature3
102|1|0|0|feature4

103|0|0|1|feature1
103|0|0|1|feature2

104|0|0|1|feature1
104|0|0|1|feature1
104|0|0|1|feature2
104|0|0|1|feature3

I want to know which fkeys are associated with feature1 AND feature2 (on real life the list of features can reach 10 or 20). I also want the sum of the counters for both feature1 AND feature2. The expected result is:

100|0|3|0
103|0|0|2
104|0|0|3

Note that the count on the last line of the result is 3 and not 4 as the result should not include information from the line with feature3.

I tried to combine GROUP BY and HAVING, but no lucky as it has the effect of OR, instead of AND. The challenge seem to be having AND behavior for different rows with same fkey.

SELECT ...
...
GROUP BY fkey
HAVING feature in ('feature1', 'feature2')

Any ideas?

Was it helpful?

Solution

This is easiest done with subqueries, on which you can then use AND:

SELECT fkey,
       SUM(count1),
       SUM(count2),
       SUM(count3)
FROM MyTable
WHERE fkey IN (SELECT DISTINCT fkey
               FROM MyTable
               WHERE feature = 'feature1')
  AND fkey IN (SELECT DISTINCT fkey
               FROM MyTable
               WHERE feature = 'feature2')
GROUP BY fkey

Alternatively, use a single compound subquery:

SELECT fkey,
       SUM(count1),
       SUM(count2),
       SUM(count3)
FROM MyTable
WHERE fkey IN (SELECT fkey
               FROM MyTable
               WHERE feature = 'feature1'
               INTERSECT
               SELECT fkey
               FROM MyTable
               WHERE feature = 'feature2')
GROUP BY fkey
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top