set nocount on declare @t table (id int, MaxScore int, S...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

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
where AcceptedAnswerId is not null and ParentId is null 

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

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results