I have a table with product ID, category ID and status of the product.
+---------+-------------+-------------+
| id | cat_id | status |
+---------+-------------+-------------+
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 2 | 1 |
| 4 | 2 | 0 |
| 5 | 1 | 0 |
| 6 | 2 | 0 |
+---------+-------------+-------------+
How do I group based on its status, then count its product and the total based on its category ID? I'm expecting like this:
+---------+-------------+-------------+-----------+
| cat_id | published | unpublished | totals |
+---------+-------------+-------------+-----------+
| 1 | 2 | 1 | 3 |
| 2 | 1 | 2 | 3 |
+---------+-------------+-------------+-----------+
Where status=1
is published
and status=0
is unpublished
.
Best Answer
You need conditional aggregation using sum(case):
Edit:
My crystal ball was apparently broken, but now it's fixed :-)
If you need to count each product_id only once you can use this: