I store hits. My table looks like this:
ID | time | Country
--------------------------------
1 | 01:00:00 | France
2 | 01:00:00 | Brazil
3 | 01:00:00 | USA
4 | 02:00:00 | USA
This is my query:
SELECT COUNT(*) as total_hits, HOUR(time) as hour, Country
FROM hits
WHERE time >= CURDATE()
GROUP BY HOUR(time)
It does count how many hits I got and groups it by hours:
01:00:00
Total hits: 3
02:00:00
Total hits: 1
But this is what I need:
01:00:00
Total hits: 3
02:00:00
Total hits: 1
-----------
France: 1
USA: 2
Brazil: 1
I know I could do:
SELECT
COUNT(*)
, HOUR(time)
, COUNT(IF( Country = 'France', Country, null)) AS France
, COUNT(IF( Country = 'USA', Country, null)) AS USA
, COUNT(IF( Country = 'Brazil', Country, null)) AS Brazil
FROM hits
WHERE time >= CURDATE()
GROUP BY HOUR(time)
Or alternatively with CASE
or SUM(Country = 'France') AS France
.
But in the country column there are more than just 3 countries. If I would do this with every country my query would be very long.
Edit
I could do this as Alex Monthy answer:
SELECT COUNT(*), Country, HOUR(time)
FROM hits
WHERE time >= CURDATE()
GROUP BY Country, HOUR(time)
But the ouput will be something like this:
01:00:00
Total hits: 1
Country: France
01:00:00
Total hits: 1
Country: Brazil
01:00:00
Total hits: 1
Country: USA
02:00:00
Total hits: 1
Country: USA
However I need an output like mentioned above.
TL;DR: I want one result grouped by time and another grouped by countries.