Q&A for professional and enthusiast programmers
with rev(pid, id, bid) as ( select top 40000 ph.postid pid , ph.id id , -1 bid from posthistory ph where ph.posthistorytypeid = 3 -- initial title union all select phr.postid , phr.id , fr.id from posthistory phr inner join rev fr on fr.pid = phr.postid where phr.posthistorytypeid = 6 -- revision titile and phr.id > fr.id ) , revall as ( select pid as postid , bid as baseid , min(id) as nextid from rev group by pid, bid ) select revall.postid as [Post Link] , phnext.text as [raw], phbase.text as [edit] from revall inner join posthistory phbase on revall.baseid = phbase.id inner join posthistory phnext on revall.nextid = phnext.id where baseid <> -1 order by revall.postid, baseid