Group by (SQL)
Appearance
This article is missing information about what "grouping" means.(September 2020) |
A GROUP BY
statement in SQL specifies that a SQL SELECT
statement returns a list that is grouped by one or more columns, usually in order to apply some sort of aggregate function to certain columns.[1][2]
Examples
Returns a list of Department IDs along with the sum of their sales for the date of January 1, 2000.
SELECT DeptID, SUM(SaleAmount) FROM Sales
WHERE SaleDate = '01-Jan-2000'
GROUP BY DeptID
Returns the data of the example pivot table which answers the question "How many Units did we sell in each Region for every Ship Date?":
SELECT Region, Ship_Date, SUM(Units) AS Sum_of_Units
FROM FlatData
GROUP BY Region, Ship_Date
Common grouping (aggregation) functions include:
- Count(expression) - Quantity of matching records (per group)
- Sum(expression) - Summation of given value (per group)
- Min(expression) - Minimum of given value (per group)
- Max(expression) - Maximum of given value (per group)
- Avg(expression) - Average of given value (per group)
References
- ^ "SQL GROUP BY Statement". www.w3schools.com. Retrieved 2020-09-18.
- ^ shkale-msft. "GROUP BY (Transact-SQL) - SQL Server". docs.microsoft.com. Retrieved 2020-09-18.