Q&A for professionals and students of engineering
-- compute for each question how many votes it has --(question means posttypeid = 1) with a as ( Select p.Id, Count(v.PostId) n_votes from Votes v join PostsWithDeleted p on v.PostId = p.Id where p.PostTypeId in (1) and v.VoteTypeId in (2,3) group by p.Id ), --- add posts with zero counts which are not being counted by the scan on the votes table b as ( select pp.Id, Case When n_votes > 0 then n_votes else 0 end n_votes from a full outer join PostsWithDeleted pp on a.Id = pp.id where pp.PostTypeId in (1) ) --- create histogram select n_votes, count(n_votes) from b where n_votes <10 group by n_votes order by n_votes