I would like to combine the counts of the stats by different hit_types into one query. Is that possible?
MariaDB [db]> select allurls.id, count(s1.id) from allurls inner join stats s1 on allurls.id = s1.allurl_id and s1.hit_type = 0 where s1.hit_date >= '2018-01-15' group by allurls.id;
+-----+--------------+
| id | count(s1.id) |
+-----+--------------+
| aaa | 1 |
| cnn | 16 |
+-----+--------------+
MariaDB [db]> select allurls.id, count(s1.id) from allurls inner join stats s1 on allurls.id = s1.allurl_id and s1.hit_type = 1 where s1.hit_date >= '2018-01-15' group by allurls.id;
+-----+--------------+
| id | count(s1.id) |
+-----+--------------+
| cnn | 1 |
+-----+--------------+
MariaDB [db]> select allurls.id, count(s1.id) from allurls inner join stats s1 on allurls.id = s1.allurl_id and s1.hit_type = 2 where s1.hit_date >= '2018-01-15' group by allurls.id;
+-----+--------------+
| id | count(s1.id) |
+-----+--------------+
| cnn | 4 |
+-----+--------------+
I tried to combine the first two but the numbers are all messed up and it eliminated the first result 'aaa'.
MariaDB [db]> select allurls.id, count(s1.id), count(s2.id) from allurls inner join stats s1 on allurls.id = s1.allurl_id and s1.hit_type = 0 inner join stats s2 on allurls.id = s2.allurl_id and s2.hit_type = 1 where s1.hit_date >= '2018-01-15' and s2.hit_date >= '2018-01-15' group by allurls.id;
+-----+--------------+--------------+
| id | count(s1.id) | count(s2.id) |
+-----+--------------+--------------+
| cnn | 16 | 16 |
+-----+--------------+--------------+
I expected to see
+-----+--------------+--------------+
| id | count(s1.id) | count(s2.id) |
+-----+--------------+--------------+
| aaa | 1 | 0 |
| cnn | 16 | 1 |
+-----+--------------+--------------+
Ultimately I want to also include count(distinct(s4.source_id))
.
Here is a fiddle: https://www.db-fiddle.com/f/tGP5SbC2AdGgeEwWTAgobf/0