Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Calculate field with the same conditions
Message
 
 
À
02/03/2011 14:23:00
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Web
Divers
Thread ID:
01502373
Message ID:
01502374
Vues:
66
This message has been marked as the solution to the initial question of the thread.
>I have a SQL which is using two calculated fields with the same conditions. It goes like this:
>
>
>SELECT Member.FirstName,Member.LastName,
> (SELECT MAX(Order.AddDate) FROM Order WHERE Order.NoMember=Member.Numero) AS AddDate,
> (SELECT COUNT(*) FROM Order WHERE Order.NoMember=Member.Numero) AS CountRecord
> FROM Member
>
>
>So, basically, as you can see, the two calculated fields are using the same conditions. I was trying to know if that could simplified in order to avoid going into the Order table twice.

Try using derived table - it will be most likely a better performance:
select M.FirstName, M.LastName, O.AddDate, O.CountRecord from Member M
inner join (select max(AddDate) as AddDate, count(*) as CountRecord, NoMember 
from [Order] group by NoMember) O
on M.Numero = O.NoMember
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