Q&A for those who base their lives on Jewish law and tradition and anyone interested in learning more
select buckets.name, buckets.low, buckets.high, count(scores_and_hours.score) as N, avg(scores_and_hours.score*1.0) as avg_score from ( select p_ptij.score as score, case when max(p_ptij_prev.creationdate) is null then 99999999 -- Never before else datediff(hh, max(p_ptij_prev.creationdate), p_ptij.creationdate) end as hours_since_last from (select p.creationdate, p.owneruserid, p.score from posts p where p.posttypeid = 1 -- q ) p_ptij left outer join (select p.creationdate, p.owneruserid from posts p where p.posttypeid = 1 -- q ) p_ptij_prev on p_ptij_prev.owneruserid = p_ptij.owneruserid and p_ptij_prev.creationdate < p_ptij.creationdate group by p_ptij.score, p_ptij.creationdate ) scores_and_hours inner join ( select * from ( values ('same hour', 0, 1), ('second hour', 1, 2), ('same day', 2, 24), ('second day', 24, 48), ('same month', 48, 720), -- 30 days = 720 hours ('first of month', 720, 99999), -- ten years = 87600 hours ('first ever', 99999, 999999999) ) as buckets (name, low, high) ) buckets on buckets.low <= scores_and_hours.hours_since_last and buckets.high > scores_and_hours.hours_since_last group by buckets.name, buckets.low, buckets.high order by buckets.low asc