Q&A about the site for students, teachers, and linguists wanting to discuss the finer points of the Italian language
set nocount on declare @t table (id int, MaxScore int, SelectedScore int) insert @t select Id, (select max(a.Score) from Posts a where a.ParentId = q.Id and a.ParentId is not null) as MaxScore , (select a.Score from Posts a where a.Id = q.AcceptedAnswerId and a.ParentId is not null) as SelectedScore from Posts q delete @t where SelectedScore > MaxScore or SelectedScore > -2 or SelectedScore is null set nocount off set nocount on select Id as [Post Link] , ( select top 1 DisplayName from Posts p join Users u on u.Id = p.OwnerUserId where p.ParentId = t.Id and p.Score = SelectedScore ) as [Selected Owner] , SelectedScore , MaxScore from @t t order by selectedscore asc