select Name, case when Max(id) < 4 then 'R' when MAX(id) = 4 and min(id) = 4 then 'C' when min(id) < 4 and exists(select * from tblTestId t2 where id = 4 and t2.Name = t.Name) then 'RC'end as [Status]from tblTestId tgroup by Nameorder by Name