Q&A for professional and enthusiast programmers
select * from ( SELECT Id -- aggregate , VoteTypeId -- columns FROM Votes ) src PIVOT ( count(Id) -- what we aggregate on FOR VoteTypeId in ([1], [2], [3]) -- accept, upmod, downmod ) pvt select * from ( SELECT Id -- aggregate , postid -- group by , VoteTypeId -- columns FROM Votes ) src PIVOT ( count(Id) -- what we aggregate on FOR VoteTypeId in ([1], [2], [3]) -- accept, upmod, downmod ) pvt order by [2] desc