After multiple searches I couldn't find a fit solution that would work in my case.
I have the following table:
id category entry_date
1 Suggestion 01/01/2019
2 Suggestion 05/01/2019
3 Compliment 05/01/2019
4 Complaint 12/02/2019
5 Suggestion 09/10/2019
6 Compliment 23/11/2019
I need to show the number of each category and the percentage of each (based on the total entries). The 'where' will limit a date range - but I'm assuming that here it doesn't matter. This is my expected result:
Category Totals % of Total Entries
Compliment 2 ˜34%
Complaint 1 16%
Suggestion 3 60%
Here is the query I'm currently using:
SELECT category,
COUNT(*) AS total,
ROUND(COUNT(category)*100 / (SELECT COUNT(*))) AS pct
FROM (Mytable)
WHERE `entry_date` >= '2018-01-01' AND `entry_date` <= '2019-12-31'
GROUP BY category ORDER BY category ASC
With this the pct is relative to each category (so: 200,100,300), and not the total.
I'm using MySQL 5.7. Thank you all!