May 2019
Intermediate to advanced
484 pages
11h 23m
English
GROUP BY aggregates the results on the required column names or expressions. Building on the same example from the ORDER BY section, we want to know the overall product quantity per product name and location, from the following code snippet. The Quantity column is using the aggregate function SUM. Therefore, the remaining columns need to be contained in the aggregation GROUP BY clause:
SELECT P.Name AS ProductName, SUM([PI].Quantity) AS Total_Quantity, L.Name AS LocationNameFROM Production.Product AS PINNER JOIN Production.ProductInventory AS [PI] ON P.ProductID = [PI].ProductIDINNER JOIN Production.Location AS L ON [PI].LocationID = L.LocationIDWHERE P.Name LIKE 'Touring%'GROUP BY P.Name, L.NameORDER BY P.Name DESC, L.Name DESC; ...
Read now
Unlock full access