Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
A little SQL puzzle
Message
 
 
À
18/01/2010 07:59:05
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01444375
Message ID:
01444542
Vues:
42
>>>Why do you need a union and the next select twice (which can be avoided with a cte I think)
>>>
>>>
>>> select TestScore from (select *, row_number() over (order by TestScore, StudentID)  as Row from @StudentsScore) X 
>>>
>>
>>UNION and BETWEEN produce the same results. I don't think there is a difference in execution plans (will verify). I agree, that single statement is easier to maintain.
>
>(1) twice the same select statement makes the query run twice - second time probably faster - but twice nonetheless
>(2) I doubt that Union and between produce the same result - try to select the same record twice with between

I changed a bit, but still BETWEEN worked better.
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
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform