Here is a table https://paiza.io/projects/Q1VykBxszKlxqmiyqknWFw?language=mysql
title|record_datetime
--------------------------
fgame 2018-07-09 11:41:41
fgame 2018-07-09 11:41:25
fgame 2018-07-09 12:41:41
source 2018-07-09 10:40:18
source 2018-07-09 10:40:33
source 2018-07-09 10:41:10
setup 2018-07-24 09:49:11
setup 2018-07-24 09:49:13
I want the result like this sample
15 columns(title+ 13hours+ sum) and 23 rows(22 different titles + sum)
hours from 8 to 20 group by record_datetime
How can i do to make this? I have tried
SELECT * FROM (SELECT title, count(*)as h8 FROM click_log where HOUR(record_datetime) = 8 GROUP BY title) as t8
Right join (SELECT title, count(*)as h9 FROM click_log where HOUR(record_datetime) = 9 GROUP BY title) as t9 on t8.title = t9.title
Right join (SELECT title, count(*)as h10 FROM click_log where HOUR(record_datetime) = 10 GROUP BY title) as t10 on t9.title = t10.title
Right join (SELECT title, count(*)as h11 FROM click_log where HOUR(record_datetime) = 11 GROUP BY title) as t11 on t10.title = t11.title
Right join (SELECT title, count(*)as h12 FROM click_log where HOUR(record_datetime) = 12 GROUP BY title) as t12 on t11.title = t12.title
Right join (SELECT title, count(*)as h13 FROM click_log where HOUR(record_datetime) = 13 GROUP BY title) as t13 on t12.title = t13.title
Right join (SELECT title, count(*)as h14 FROM click_log where HOUR(record_datetime) = 14 GROUP BY title) as t14 on t13.title = t14.title
Right join (SELECT title, count(*)as h15 FROM click_log where HOUR(record_datetime) = 15 GROUP BY title) as t15 on t14.title = t15.title
Right join (SELECT title, count(*)as h16 FROM click_log where HOUR(record_datetime) = 16 GROUP BY title) as t16 on t15.title = t16.title
Right join (SELECT title, count(*)as h17 FROM click_log where HOUR(record_datetime) = 17 GROUP BY title) as t17 on t16.title = t17.title
Right join (SELECT title, count(*)as h18 FROM click_log where HOUR(record_datetime) = 18 GROUP BY title) as t18 on t17.title = t18.title
Right join (SELECT title, count(*)as h19 FROM click_log where HOUR(record_datetime) = 19 GROUP BY title) as t19 on t18.title = t19.title
Right join (SELECT title, count(*)as h20 FROM click_log where HOUR(record_datetime) = 20 GROUP BY title) as t20 on t19.title = t20.title
Right join (SELECT title, count(*)as allh FROM click_log where 1=1 GROUP BY title) as tallh on t20.title = tallh.title
UNION
SELECT * FROM (SELECT title, count(*)as h8 FROM click_log where HOUR(record_datetime) = 8 GROUP BY title) as t8
LEFT join (SELECT title, count(*)as h9 FROM click_log where HOUR(record_datetime) = 9 GROUP BY title) as t9 on t8.title = t9.title
LEFT join (SELECT title, count(*)as h10 FROM click_log where HOUR(record_datetime) = 10 GROUP BY title) as t10 on t9.title = t10.title
LEFT join (SELECT title, count(*)as h11 FROM click_log where HOUR(record_datetime) = 11 GROUP BY title) as t11 on t10.title = t11.title
LEFT join (SELECT title, count(*)as h12 FROM click_log where HOUR(record_datetime) = 12 GROUP BY title) as t12 on t11.title = t12.title
LEFT join (SELECT title, count(*)as h13 FROM click_log where HOUR(record_datetime) = 13 GROUP BY title) as t13 on t12.title = t13.title
LEFT join (SELECT title, count(*)as h14 FROM click_log where HOUR(record_datetime) = 14 GROUP BY title) as t14 on t13.title = t14.title
LEFT join (SELECT title, count(*)as h15 FROM click_log where HOUR(record_datetime) = 15 GROUP BY title) as t15 on t14.title = t15.title
LEFT join (SELECT title, count(*)as h16 FROM click_log where HOUR(record_datetime) = 16 GROUP BY title) as t16 on t15.title = t16.title
LEFT join (SELECT title, count(*)as h17 FROM click_log where HOUR(record_datetime) = 17 GROUP BY title) as t17 on t16.title = t17.title
LEFT join (SELECT title, count(*)as h18 FROM click_log where HOUR(record_datetime) = 18 GROUP BY title) as t18 on t17.title = t18.title
LEFT join (SELECT title, count(*)as h19 FROM click_log where HOUR(record_datetime) = 19 GROUP BY title) as t19 on t18.title = t19.title
LEFT join (SELECT title, count(*)as h20 FROM click_log where HOUR(record_datetime) = 20 GROUP BY title) as t20 on t19.title = t20.title
LEFT join (SELECT title, count(*)as allh FROM click_log where 1=1 GROUP BY title) as tallh on t20.title = tallh.title