sql - Are these two queries the same - GROUP BY vs. DISTINCT? -


These two questions return the same results. Is it a coincidence or is they really the same?

1.

  Select t.ItemNumber, (where the details of the selected Top 1 item from the transaction is ItemNumber = t.ItemNumber ORDER BY DateCreated DESC), as per T-Group by the details of the goods. Item Number  

2.

  Choose DISTINCT (t.ItemNumber), (Choose Top 1 Item Dictionary from Transaction) as long as ItemNumber = t.ItemNumber ORDER dated DESC)  

A little detail: For each item I'm trying to get a separate list of items from a table filled with transactions, I have ItemNumber (identifying fields) and most recent I'm looking for item diction of itself.

Your example # 2 had started scratched me for a while - I thought to myself : "You can not do a column , what does that mean?" - Until I did not realize what was happening.

When you

  select the difference (t.ItemNumber)  

you not Despite the appearances, actually asking for different values ​​ of t.ItemNumber ! Your example # 2 is actually parsed as the

  select difference (t.ItemNumber), (where the item number of the top 1 item is given from the transaction ItemNumber = t.ItemNumber ORDER DateCreated DESC) with the description of the items in the transaction T  

syntax with correct but highly sensitive brackets with t.ItemNumber . This is set as a whole result - DISTINCT is applicable.

In this situation, when your Group BY is actually different from the group, you get the same results. I'm actually a little surprised that the SQL Server ( GROUP BY in the example) does not insist that the column mentioned in the GROUP BY list is mentioned.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -