I'm a little confused about this, which means that I may not understand it. If this is the case, please ignore this post.
I'm looking the the code and I think it can be a lot more efficient. I say this, knowing full well, that you may not notice a difference in performance because fast is fast.
The first derived table looks like this:
Code:
SELECT TOP 1
T0.[ItemCode] AS [Item Code],
T1.[Price] AS [Highest Price]
FROM OITM T0
INNER JOIN INV1 T1
ON T0.ItemCode = T1.ItemCode
WHERE T0.[ItemCode] = '12345'
ORDER BY T1.[Price] DESC
Now, here's where the confusion comes in.... This code is obviously using 2 tables (OITM and INV1). Both tables have a column named ItemCode. The OITM table (aliased T0) is used to return the item code, used in the join and used in the filter. Since this is an inner join, every row returned (ignoring the TOP 1 and where condition for a moment) will have the same value from either table. This means that you can return the T1.ItemCode instead of T0.ItemCode and get exactly the same results. Similarly, you can use T1.ItemCode in the where clause to get the same results. Making the replacements, the code would look like this:
Code:
SELECT TOP 1
T1.[ItemCode] AS [Item Code],
T1.[Price] AS [Highest Price]
FROM OITM T0
INNER JOIN INV1 T1
ON T0.ItemCode = T1.ItemCode
WHERE T1.[ItemCode] = '12345'
ORDER BY T1.[Price] DESC
Ok.... so you're thinking.... so what. Ordinarily I would agree with you. However, in this situation, T0 isn't used at all, so you can remove it from the query, like this:
Code:
SELECT TOP 1
T1.[ItemCode] AS [Item Code],
T1.[Price] AS [Highest Price]
FROM INV1 T1
WHERE T1.[ItemCode] = '12345'
ORDER BY T1.[Price] DESC
Similarly, the 2nd derived table can be replaced with this:
Code:
SELECT TOP 1
T1.[ItemCode] AS [Item Code],
T1.[Price] AS [Last Price]
FROM INV1 T1
WHERE T1.[ItemCode] = '12345'
ORDER BY T1.[DocDate] DESC
Taking another look... It's obvious that ItemCode is returned so that we can join the results. Furthermore, since both derived tables have a TOP 1 in them, the entire query can only ever return 1 row with item code, highest price, and last price. The first derived table returns the highest price and the second returns the last price. The key here is that we can treat these as scalar values if we wanted to, so the whole query would look like this.
Code:
SELECT '12345' As [Item Code],
(
SELECT TOP 1
T1.[Price] AS [Highest Price]
FROM INV1 T1
WHERE T1.[ItemCode] = '12345'
ORDER BY T1.[Price] DESC
) As [Highest Price],
(
SELECT TOP 1
T1.[Price] AS [Last Price]
FROM INV1 T1
WHERE T1.[ItemCode] = '12345'
ORDER BY T1.[DocDate] DESC) As [Last Price]
Basically, the derived tables are replaced by subqueries in the select clause. This is really just a simpler (and therefore more efficient) method of combining the 3 scalar values to a single row with 3 columns.
-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom