Your example # 2 made me scratch my head a bit - I thought to myself: "You can't create a single column DISTINCT , what would it mean?" - until I understood what was happening.
If you
SELECT DISTINCT(t.ItemNumber)
you are not, in spite of appearance, really requesting excellent t.ItemNumber values! Your example # 2 is actually parsed just like
SELECT DISTINCT (t.ItemNumber) , (SELECT TOP 1 ItemDescription FROM Transactions WHERE ItemNumber = t.ItemNumber ORDER BY DateCreated DESC) AS ItemDescription FROM Transactions t
with syntactically correct but extra parentheses around t.ItemNumber . It is to the result set that DISTINCT is generally applied.
In this case, since your GROUP BY groups are column by column, which actually differ, you get the same results. Actually, I'm a little surprised that SQL Server does not (in the GROUP BY example) insist that the heading of the subclasses is mentioned in the GROUP BY list.
Aakashm
source share