Skip to content

Commit 3112a74

Browse files
authored
Merge pull request #943 from Shah-Nisarg/patch-1
Removed the three backticks in examples of column-expression section
2 parents 0ab652f + 6a6ff5f commit 3112a74

1 file changed

Lines changed: 29 additions & 29 deletions

File tree

docs/t-sql/queries/select-group-by-transact-sql.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
---
22
title: "GROUP BY (Transact-SQL) | Microsoft Docs"
33
ms.custom: ""
44
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
102102

103103
The following statements are allowed:
104104

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+
```
111111

112112
The following statements are not allowed:
113113

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+
118119
The column expression cannot contain:
119120

120121
- 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
129130

130131
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.
131132

132-
```
133+
```sql
133134
CREATE TABLE Sales ( Country varchar(50), Region varchar(50), Sales int );
134135

135136
INSERT INTO sales VALUES (N'Canada', N'Alberta', 100);
@@ -148,7 +149,7 @@ The Sales table contains these rows:
148149

149150
This next query groups Country and Region and returns the aggregate sum for each combination of values.
150151

151-
```
152+
```sql
152153
SELECT Country, Region, SUM(sales) AS TotalSales
153154
FROM Sales
154155
GROUP BY Country, Region;
@@ -177,7 +178,7 @@ For example, `GROUP BY ROLLUP (col1, col2, col3, col4)` creates groups for each
177178

178179
Using the table from the previous example, this code runs a GROUP BY ROLLUP operation instead of a simple GROUP BY.
179180

180-
```
181+
```sql
181182
SELECT Country, Region, SUM(Sales) AS TotalSales
182183
FROM Sales
183184
GROUP BY ROLLUP (Country, Region);
@@ -200,7 +201,7 @@ GROUP BY CUBE creates groups for all possible combinations of columns. For GROUP
200201

201202
Using the table from the previous examples, this code runs a GROUP BY CUBE operation on Country and Region.
202203

203-
```
204+
```sql
204205
SELECT Country, Region, SUM(Sales) AS TotalSales
205206
FROM Sales
206207
GROUP BY CUBE (Country, Region);
@@ -228,31 +229,30 @@ For example, `GROUP BY ROLLUP (Country, Region)` and `GROUP BY GROUPING SETS ( R
228229

229230
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.
230231

231-
```
232+
```sql
232233
SELECT Country, Region, SUM(Sales) AS TotalSales
233234
FROM Sales
234235
GROUP BY GROUPING SETS ( ROLLUP (Country, Region), CUBE (Country, Region) );
235236
```
236237

237238
The results are the same as this query that returns a union of the two GROUP BY statements.
238239

239-
```
240+
```sql
240241
SELECT Country, Region, SUM(Sales) AS TotalSales
241242
FROM Sales
242243
GROUP BY ROLLUP (Country, Region)
243244
UNION ALL
244245
SELECT Country, Region, SUM(Sales) AS TotalSales
245246
FROM Sales
246-
GROUP BY CUBE (Country, Region)
247-
;
247+
GROUP BY CUBE (Country, Region);
248248
```
249249

250250
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.
251251

252252
### GROUP BY ()
253253
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.
254254

255-
```
255+
```sql
256256
SELECT Country, SUM(Sales) AS TotalSales
257257
FROM Sales
258258
GROUP BY GROUPING SETS ( Country, () );
@@ -357,7 +357,7 @@ The GROUP BY clause supports all GROUP BY features that are included in the SQL-
357357
### A. Use a simple GROUP BY clause
358358
The following example retrieves the total for each `SalesOrderID` from the `SalesOrderDetail` table. This example uses AdventureWorks.
359359
360-
```
360+
```sql
361361
SELECT SalesOrderID, SUM(LineTotal) AS SubTotal
362362
FROM Sales.SalesOrderDetail AS sod
363363
GROUP BY SalesOrderID
@@ -367,7 +367,7 @@ ORDER BY SalesOrderID;
367367
### B. Use a GROUP BY clause with multiple tables
368368
The following example retrieves the number of employees for each `City` from the `Address` table joined to the `EmployeeAddress` table. This example uses AdventureWorks.
369369

370-
```
370+
```sql
371371
SELECT a.City, COUNT(bea.AddressID) EmployeeCount
372372
FROM Person.BusinessEntityAddress AS bea
373373
INNER JOIN Person.Address AS a
@@ -379,7 +379,7 @@ ORDER BY a.City;
379379
### C. Use a GROUP BY clause with an expression
380380
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.
381381

382-
```
382+
```sql
383383
SELECT DATEPART(yyyy,OrderDate) AS N'Year'
384384
,SUM(TotalDue) AS N'Total Order Amount'
385385
FROM Sales.SalesOrderHeader
@@ -390,7 +390,7 @@ ORDER BY DATEPART(yyyy,OrderDate);
390390
### D. Use a GROUP BY clause with a HAVING clause
391391
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.
392392

393-
```
393+
```sql
394394
SELECT DATEPART(yyyy,OrderDate) AS N'Year'
395395
,SUM(TotalDue) AS N'Total Order Amount'
396396
FROM Sales.SalesOrderHeader
@@ -404,7 +404,7 @@ ORDER BY DATEPART(yyyy,OrderDate);
404404
### E. Basic use of the GROUP BY clause
405405
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.
406406

407-
```
407+
```sql
408408
-- Uses AdventureWorksDW
409409

410410
SELECT OrderDateKey, SUM(SalesAmount) AS TotalSales FROM FactInternetSales
@@ -414,7 +414,7 @@ GROUP BY OrderDateKey ORDER BY OrderDateKey;
414414
### F. Basic use of the DISTRIBUTED_AGG hint
415415
This example uses the DISTRIBUTED_AGG query hint to force the appliance to shuffle the table on the `CustomerKey` column before performing the aggregation.
416416

417-
```
417+
```sql
418418
-- Uses AdventureWorksDW
419419

420420
SELECT CustomerKey, SUM(SalesAmount) AS sas
@@ -426,7 +426,7 @@ ORDER BY CustomerKey DESC;
426426
### G. Syntax Variations for GROUP BY
427427
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:
428428

429-
```
429+
```sql
430430
-- Uses AdventureWorks
431431

432432
SELECT LastName, FirstName FROM DimCustomer GROUP BY LastName, FirstName;
@@ -439,7 +439,7 @@ SELECT SalesAmount FROM FactInternetSales GROUP BY SalesAmount, SalesAmount*1.10
439439
### H. Using a GROUP BY with multiple GROUP BY expressions
440440
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.
441441

442-
```
442+
```sql
443443
-- Uses AdventureWorks
444444

445445
SELECT OrderDateKey, DueDateKey, SUM(SalesAmount) AS TotalSales
@@ -451,7 +451,7 @@ ORDER BY OrderDateKey;
451451
### I. Using a GROUP BY clause with a HAVING clause
452452
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.
453453

454-
```
454+
```sql
455455
-- Uses AdventureWorks
456456

457457
SELECT OrderDateKey, SUM(SalesAmount) AS TotalSales

0 commit comments

Comments
 (0)