I have a warehouse table in my Access database that looks like this:
product_id
10
20
20
30
30
30
40
40
40
40
Now I need an SQL query to return 10, 30
product_ids which have odd count in the table.
SELECT product_id, Count(product_id) AS cnt
FROM warehouse
GROUP BY product_id;
This query returns the count of each value, but how can I edit it to only return rows with odd cnt?
You could use a having clause with the mod operator:
SELECT product_id, COUNT(product_id) AS cnt
FROM warehouse
GROUP BY product_id
HAVING COUNT(product_id) MOD 2 = 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With