DC (noreply@fakeaddress.com) writes:[color=blue]
> Thanks for everyone's replies. I guess I'll just have to use the nested
> subquery.[/color]
No, no one recommended you to use a nested subquery. The recommendation
was to use a derived table, which is something different.
A derived table is a logical temp table within the query. It may or may
not be materialized during query computation, that is up to the optimizer.
The optimizer may in fact evalutate the entire query in pass if that is
feasible. This last is important to know, because you can use derived
tables that if they were computed on their own would be prohibitely
expensive, but may run very well in the actual query.
A subquery is a query which appear where you also could have a column
expression:
SELECT C.CustomerID,
OrderCnt = (SELECT COUNT(*)
FROM Orders O
WHERE O.CustomerID = O.OrderId)
FROM Customers
In SQL 2000, such queries often has considerably worse performance than
the corresponding query with a derived table instead.
--
Erland Sommarskog, SQL Server MVP,
esquel@sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp