Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit f1b62d1

Browse files
authored
added sql colorizer
1 parent ab32535 commit f1b62d1

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

docs/t-sql/functions/grouping-id-transact-sql.md

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ ms.author: vanto
3030
## Syntax
3131

3232
```syntaxsql
33-
3433
GROUPING_ID ( <column_expression>[ ,...n ] )
3534
```
3635

@@ -69,15 +68,15 @@ GROUPING_ID ( <column_expression>[ ,...n ] )
6968

7069
Statement A:
7170

72-
```
71+
```sql
7372
SELECT GROUPING_ID(A,B)
7473
FROM T
7574
GROUP BY CUBE(A,B)
7675
```
7776

7877
Statement B:
7978

80-
```
79+
```sql
8180
SELECT 3 FROM T GROUP BY ()
8281
UNION ALL
8382
SELECT 1 FROM T GROUP BY A
@@ -92,7 +91,7 @@ SELECT 0 FROM T GROUP BY A,B
9291
### A. Using GROUPING_ID to identify grouping levels
9392
The following example returns the count of employees by `Name` and `Title`, `Name,` and company total in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database. `GROUPING_ID()` is used to create a value for each row in the `Title` column that identifies its level of aggregation.
9493

95-
```
94+
```sql
9695
SELECT D.Name
9796
,CASE
9897
WHEN GROUPING_ID(D.Name, E.JobTitle) = 0 THEN E.JobTitle
@@ -116,7 +115,7 @@ GROUP BY ROLLUP(D.Name, E.JobTitle);
116115
#### Simple Example
117116
In the following code, to return only the rows that have a count of employees by title, remove the comment characters from `HAVING GROUPING_ID(D.Name, E.JobTitle); = 0` in the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database. To return only rows with a count of employees by department, remove the comment characters from `HAVING GROUPING_ID(D.Name, E.JobTitle) = 1;`.
118117

119-
```
118+
```sql
120119
SELECT D.Name
121120
,E.JobTitle
122121
,GROUPING_ID(D.Name, E.JobTitle) AS 'Grouping Level'
@@ -151,9 +150,9 @@ GROUP BY ROLLUP(D.Name, E.JobTitle)
151150
#### Complex Example
152151
The following example uses `GROUPING_ID()` to filter a result set that contains multiple grouping levels by grouping level. Similar code can be used to create a view that has several grouping levels and a stored procedure that calls the view by passing a parameter that filters the view by grouping level. The example uses the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
153152

154-
```
155-
DECLARE @Grouping nvarchar(50);
156-
DECLARE @GroupingLevel smallint;
153+
```sql
154+
DECLARE @Grouping NVARCHAR(50);
155+
DECLARE @GroupingLevel SMALLINT;
157156
SET @Grouping = N'CountryRegionCode Total';
158157

159158
SELECT @GroupingLevel = (
@@ -243,14 +242,14 @@ ORDER BY
243242
#### ROLLUP Example
244243
In this example, all grouping levels do not appear as they do in the following CUBE example. If the order of the columns in the `ROLLUP` list is changed, the level values in the `Grouping Level` column will also have to be changed. The example uses the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database.
245244

246-
```
245+
```sql
247246
SELECT DATEPART(yyyy,OrderDate) AS N'Year'
248247
,DATEPART(mm,OrderDate) AS N'Month'
249248
,DATEPART(dd,OrderDate) AS N'Day'
250249
,SUM(TotalDue) AS N'Total Due'
251-
,CAST(GROUPING(DATEPART(dd,OrderDate))AS char(1)) +
252-
CAST(GROUPING(DATEPART(mm,OrderDate))AS char(1)) +
253-
CAST(GROUPING(DATEPART(yyyy,OrderDate))AS char(1))
250+
,CAST(GROUPING(DATEPART(dd,OrderDate)) AS CHAR(1)) +
251+
CAST(GROUPING(DATEPART(mm,OrderDate)) AS CHAR(1)) +
252+
CAST(GROUPING(DATEPART(yyyy,OrderDate)) AS CHAR(1))
254253
AS N'Bit Vector(base-2)'
255254
,GROUPING_ID(DATEPART(yyyy,OrderDate)
256255
,DATEPART(mm,OrderDate)
@@ -324,14 +323,14 @@ ORDER BY GROUPING_ID(DATEPART(mm,OrderDate)
324323

325324
Unlike `ROLLUP` in the previous example, `CUBE` outputs all grouping levels. If the order of the columns in the `CUBE` list is changed, the level values in the `Grouping Level` column will also have to be changed. The example uses the [!INCLUDE[ssSampleDBnormal](../../includes/sssampledbnormal-md.md)] database
326325

327-
```
326+
```sql
328327
SELECT DATEPART(yyyy,OrderDate) AS N'Year'
329328
,DATEPART(mm,OrderDate) AS N'Month'
330329
,DATEPART(dd,OrderDate) AS N'Day'
331330
,SUM(TotalDue) AS N'Total Due'
332-
,CAST(GROUPING(DATEPART(dd,OrderDate))AS char(1)) +
333-
CAST(GROUPING(DATEPART(mm,OrderDate))AS char(1)) +
334-
CAST(GROUPING(DATEPART(yyyy,OrderDate))AS char(1))
331+
,CAST(GROUPING(DATEPART(dd,OrderDate)) AS CHAR(1)) +
332+
CAST(GROUPING(DATEPART(mm,OrderDate)) AS CHAR(1)) +
333+
CAST(GROUPING(DATEPART(yyyy,OrderDate)) AS CHAR(1))
335334
AS N'Bit Vector(base-2)'
336335
,GROUPING_ID(DATEPART(yyyy,OrderDate)
337336
,DATEPART(mm,OrderDate)

0 commit comments

Comments
 (0)