What will be the output of

declare @tbl table(id int identity, refid int)
insert into @tbl
select 0 union all select 1 union all
select 2 union all select 1 union all
select 1

select t.id,t.refid,case when x.cnt is null then 0 else x.cnt end as total
from @tbl t
left join
(
select refid,count(refid) as cnt
from @tbl
where refid <> 0
group by refid
having (count(refid)>0)) x
on t.id = x.refid

 Posted by Rajnilari2015 on 1/24/2016 | Category: Sql Server Interview questions | Views: 1582 | Points: 40
Select from following answers:
  1. id refid total 1 0 3 2 1 1 3 2 0 4 1 0 5 1 0
  2. id refid total 1 4 3 2 1 1 3 2 0 4 1 0 5 1 0
  3. id refid total 1 0 3 2 7 1 3 2 0 4 1 0 5 1 0
  4. id refid total 1 0 3 2 1 1 3 8 0 4 1 0 5 1 0
  5. All Above

Show Correct Answer


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response