Posted by: Charlie Maitland | Monday 12 March, 2007

Correct Grouping

Nice reminder from Brian Sherwin on the correct way to do grouping without having every field in the Group By  clause.

SELECT C.CustomerID, C.CustomerName, C.City, C.State, S.TotalSales
FROM Customers C
INNER JOIN
(SELECT CustomerID, SUM(Sales) as TotalSales
FROM Sales 
GROUP BY CustomerID) S
ON C.CustomerID = S.CustomerID


Leave a response

Your response:

Categories