Reputation: 3439
I have this table
I would like to display products(with their type) that have at least two different types.
Here :
I have tried this :
SELECT PRODUCT, TYPE FROM MYTABLE
group by PRODUCT, TYPE
HAVING count(PRODUCT) > 1;
but it doesn't give me the result I want.
Upvotes: 1
Views: 42
Reputation: 312257
The problem here is you want to count by a different grouping than you want to display. One way around this is two have the counting in a subquery:
SELECT DISTINCT product, type
FROM mytable
WHERE product IN (SELECT product
FROM mytable
GROUP BY product
HAVING COUNT(DISTINCT type) > 1)
Upvotes: 4