You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/t-sql/queries/select-group-by-transact-sql.md
+29-29Lines changed: 29 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
---
1
+
---
2
2
title: "GROUP BY (Transact-SQL) | Microsoft Docs"
3
3
ms.custom: ""
4
4
ms.date: "03/03/2017"
@@ -102,19 +102,20 @@ The column must appear in the FROM clause of the SELECT statement, but is not re
102
102
103
103
The following statements are allowed:
104
104
105
-
```
106
-
SELECT ColumnA, ColumnB FROM T GROUP BY ColumnA, ColumnB;
107
-
SELECT ColumnA + ColumnB FROM T GROUP BY ColumnA, ColumnB;
108
-
SELECT ColumnA + ColumnB FROM T GROUP BY ColumnA + ColumnB;
109
-
SELECT ColumnA + ColumnB + constant FROM T GROUP BY ColumnA, ColumnB;
110
-
```
105
+
```sql
106
+
SELECT ColumnA, ColumnB FROM T GROUP BY ColumnA, ColumnB;
107
+
SELECT ColumnA + ColumnB FROM T GROUP BY ColumnA, ColumnB;
108
+
SELECT ColumnA + ColumnB FROM T GROUP BY ColumnA + ColumnB;
109
+
SELECT ColumnA + ColumnB + constant FROM T GROUP BY ColumnA, ColumnB;
110
+
```
111
111
112
112
The following statements are not allowed:
113
113
114
-
```
115
-
SELECT ColumnA, ColumnB FROM T GROUP BY ColumnA + ColumnB;
116
-
SELECT ColumnA + constant + ColumnB FROM T GROUP BY ColumnA + ColumnB;
117
-
```
114
+
```sql
115
+
SELECT ColumnA, ColumnB FROM T GROUP BY ColumnA + ColumnB;
116
+
SELECT ColumnA + constant + ColumnB FROM T GROUP BY ColumnA + ColumnB;
117
+
```
118
+
118
119
The column expression cannot contain:
119
120
120
121
- A column alias that is defined in the SELECT list. It can use a column alias for a derived table that is defined in the FROM clause.
@@ -129,7 +130,7 @@ Groups the SELECT statement results according to the values in a list of one or
129
130
130
131
For example, this query creates a Sales table with columns for Country, Region, and Sales. It inserts four rows and two of the rows have matching values for Country and Region.
131
132
132
-
```
133
+
```sql
133
134
CREATETABLESales ( Country varchar(50), Region varchar(50), Sales int );
134
135
135
136
INSERT INTO sales VALUES (N'Canada', N'Alberta', 100);
@@ -148,7 +149,7 @@ The Sales table contains these rows:
148
149
149
150
This next query groups Country and Region and returns the aggregate sum for each combination of values.
150
151
151
-
```
152
+
```sql
152
153
SELECT Country, Region, SUM(sales) AS TotalSales
153
154
FROM Sales
154
155
GROUP BY Country, Region;
@@ -177,7 +178,7 @@ For example, `GROUP BY ROLLUP (col1, col2, col3, col4)` creates groups for each
177
178
178
179
Using the table from the previous example, this code runs a GROUP BY ROLLUP operation instead of a simple GROUP BY.
179
180
180
-
```
181
+
```sql
181
182
SELECT Country, Region, SUM(Sales) AS TotalSales
182
183
FROM Sales
183
184
GROUP BY ROLLUP (Country, Region);
@@ -200,7 +201,7 @@ GROUP BY CUBE creates groups for all possible combinations of columns. For GROUP
200
201
201
202
Using the table from the previous examples, this code runs a GROUP BY CUBE operation on Country and Region.
202
203
203
-
```
204
+
```sql
204
205
SELECT Country, Region, SUM(Sales) AS TotalSales
205
206
FROM Sales
206
207
GROUP BY CUBE (Country, Region);
@@ -228,31 +229,30 @@ For example, `GROUP BY ROLLUP (Country, Region)` and `GROUP BY GROUPING SETS ( R
228
229
229
230
When GROUPING SETS has two or more elements, the results are a union of the elements. This example returns the union of the ROLLUP and CUBE results for Country and Region.
230
231
231
-
```
232
+
```sql
232
233
SELECT Country, Region, SUM(Sales) AS TotalSales
233
234
FROM Sales
234
235
GROUP BY GROUPING SETS ( ROLLUP (Country, Region), CUBE (Country, Region) );
235
236
```
236
237
237
238
The results are the same as this query that returns a union of the two GROUP BY statements.
238
239
239
-
```
240
+
```sql
240
241
SELECT Country, Region, SUM(Sales) AS TotalSales
241
242
FROM Sales
242
243
GROUP BY ROLLUP (Country, Region)
243
244
UNION ALL
244
245
SELECT Country, Region, SUM(Sales) AS TotalSales
245
246
FROM Sales
246
-
GROUP BY CUBE (Country, Region)
247
-
;
247
+
GROUP BY CUBE (Country, Region);
248
248
```
249
249
250
250
SQL does not consolidate duplicate groups generated for a GROUPING SETS list. For example, in `GROUP BY ( (), CUBE (Country, Region) )`, both elements return a row for the grand total and both rows will be listed in the results.
251
251
252
252
### GROUP BY ()
253
253
Specifies the empty group which generates the grand total. This is useful as one of the elements of a GROUPING SET. For example, this statement gives the total sales for each country and then gives the grand-total for all countries.
254
254
255
-
```
255
+
```sql
256
256
SELECT Country, SUM(Sales) AS TotalSales
257
257
FROM Sales
258
258
GROUP BY GROUPING SETS ( Country, () );
@@ -357,7 +357,7 @@ The GROUP BY clause supports all GROUP BY features that are included in the SQL-
357
357
### A. Use a simple GROUP BY clause
358
358
The following example retrieves the total for each `SalesOrderID` from the `SalesOrderDetail` table. This example uses AdventureWorks.
359
359
360
-
```
360
+
```sql
361
361
SELECT SalesOrderID, SUM(LineTotal) AS SubTotal
362
362
FROM Sales.SalesOrderDetail AS sod
363
363
GROUP BY SalesOrderID
@@ -367,7 +367,7 @@ ORDER BY SalesOrderID;
367
367
### B. Use a GROUP BY clause with multiple tables
368
368
The following example retrieves the number of employees for each `City` from the `Address` table joined to the `EmployeeAddress` table. This example uses AdventureWorks.
369
369
370
-
```
370
+
```sql
371
371
SELECTa.City, COUNT(bea.AddressID) EmployeeCount
372
372
FROMPerson.BusinessEntityAddressAS bea
373
373
INNER JOINPerson.AddressAS a
@@ -379,7 +379,7 @@ ORDER BY a.City;
379
379
### C. Use a GROUP BY clause with an expression
380
380
The following example retrieves the total sales for each year by using the `DATEPART` function. The same expression must be present in both the `SELECT` list and `GROUP BY` clause.
381
381
382
-
```
382
+
```sql
383
383
SELECT DATEPART(yyyy,OrderDate) AS N'Year'
384
384
,SUM(TotalDue) AS N'Total Order Amount'
385
385
FROMSales.SalesOrderHeader
@@ -390,7 +390,7 @@ ORDER BY DATEPART(yyyy,OrderDate);
390
390
### D. Use a GROUP BY clause with a HAVING clause
391
391
The following example uses the `HAVING` clause to specify which of the groups generated in the `GROUP BY` clause should be included in the result set.
392
392
393
-
```
393
+
```sql
394
394
SELECT DATEPART(yyyy,OrderDate) AS N'Year'
395
395
,SUM(TotalDue) AS N'Total Order Amount'
396
396
FROMSales.SalesOrderHeader
@@ -404,7 +404,7 @@ ORDER BY DATEPART(yyyy,OrderDate);
404
404
### E. Basic use of the GROUP BY clause
405
405
The following example finds the total amount for all sales on each day. One row containing the sum of all sales is returned for each day.
406
406
407
-
```
407
+
```sql
408
408
-- Uses AdventureWorksDW
409
409
410
410
SELECT OrderDateKey, SUM(SalesAmount) AS TotalSales FROM FactInternetSales
@@ -414,7 +414,7 @@ GROUP BY OrderDateKey ORDER BY OrderDateKey;
414
414
### F. Basic use of the DISTRIBUTED_AGG hint
415
415
This example uses the DISTRIBUTED_AGG query hint to force the appliance to shuffle the table on the `CustomerKey` column before performing the aggregation.
416
416
417
-
```
417
+
```sql
418
418
-- Uses AdventureWorksDW
419
419
420
420
SELECT CustomerKey, SUM(SalesAmount) AS sas
@@ -426,7 +426,7 @@ ORDER BY CustomerKey DESC;
426
426
### G. Syntax Variations for GROUP BY
427
427
When the select list has no aggregations, each column in the select list must be included in the GROUP BY list. Computed columns in the select list can be listed, but are not required, in the GROUP BY list. These are examples of syntactically valid SELECT statements:
428
428
429
-
```
429
+
```sql
430
430
-- Uses AdventureWorks
431
431
432
432
SELECT LastName, FirstName FROM DimCustomer GROUP BY LastName, FirstName;
@@ -439,7 +439,7 @@ SELECT SalesAmount FROM FactInternetSales GROUP BY SalesAmount, SalesAmount*1.10
439
439
### H. Using a GROUP BY with multiple GROUP BY expressions
440
440
The following example groups results using multiple `GROUP BY` criteria. If, within each `OrderDateKey` group, there are subgroups that can be differentiated by `DueDateKey`, a new grouping will be defined for the result set.
441
441
442
-
```
442
+
```sql
443
443
-- Uses AdventureWorks
444
444
445
445
SELECT OrderDateKey, DueDateKey, SUM(SalesAmount) AS TotalSales
@@ -451,7 +451,7 @@ ORDER BY OrderDateKey;
451
451
### I. Using a GROUP BY clause with a HAVING clause
452
452
The following example uses the `HAVING` clause to specify the groups generated in the `GROUP BY` clause that should be included in the result set. Only those groups with order dates in 2004 or later will be included in the results.
453
453
454
-
```
454
+
```sql
455
455
-- Uses AdventureWorks
456
456
457
457
SELECT OrderDateKey, SUM(SalesAmount) AS TotalSales
0 commit comments