>>set statistics time on >>set statistics profile on >>set statistics io on >>print 'Creating cte' >>;with cte as (select *, row_number() >>over (order by TestScore, StudentID) as Row from @StudentsScore) >> >> select avg(TestScore*1.0) from (select TestScore from cte where Row = (@TotalStudents+1)/2 >> union all select TestScore from cte >> where Row = (@TotalStudents+2)/2) Y >>print 'Finished with the UNION select' >>print 'Creating cte' >>;with cte as (select *, row_number() >>over (order by TestScore, StudentID) as Row from @StudentsScore) >> >> select avg(TestScore*1.0) from cte where Row between (@TotalStudents+1)/2 and (@TotalStudents+2)/2 >>print 'Finished with the BETWEEN select' >>set statistics io off >>set statistics profile off >>set statistics time off >>>
>TotalStudents = 3 >(@TotalStudents+1)/2 = (3+1)/2 = 2 >(@TotalStudents+2)/2 = (3+2)/2 = 2 > > > >TotalStudents = 4 >(@TotalStudents+1)/2 = (4+1)/2 = 2 >(@TotalStudents+2)/2 = (4+2)/2 =3 >You're right. For this problem between is clearly a winner.