I need to compose a MySQL query, which will count the number of email occurrences per each country
I have following mysql table (shop_orders)
order_id | country | |
---|---|---|
1 | [email protected] | _it |
2 | [email protected] | _hu |
3 | [email protected] | _de |
4 | [email protected] | _pl |
5 | [email protected] | _it |
Desired output
count | _it | _hu | _de | _pl |
---|---|---|---|---|
1 | 0 | 1 | 1 | 1 |
2 | 2 | 0 | 0 | 0 |
So far I have managed to do is fetch the count, how many time email appears.
SELECT
COUNT(*) AS count, email
FROM
pinkpanda.shop_orders
GROUP BY email
ORDER BY count DESC;
But now I need to count off all the counts of emails and separate the data for each country
If you need any additional informations, please let me know and I will provide.
2
Answers
Please confirm that everything is right for you, and let me know if more adjustment is necessary.
Output:
You can use conditional aggregation to group data by the number of emails per country:
This is a valid solution for old mysql :
Demo here