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
### A. Using GROUPING_ID to identify grouping levels
93
92
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.
94
93
95
-
```
94
+
```sql
96
95
SELECTD.Name
97
96
,CASE
98
97
WHEN GROUPING_ID(D.Name, E.JobTitle) =0 THEN E.JobTitle
@@ -116,7 +115,7 @@ GROUP BY ROLLUP(D.Name, E.JobTitle);
116
115
#### Simple Example
117
116
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;`.
@@ -151,9 +150,9 @@ GROUP BY ROLLUP(D.Name, E.JobTitle)
151
150
#### Complex Example
152
151
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.
153
152
154
-
```
155
-
DECLARE @Grouping nvarchar(50);
156
-
DECLARE @GroupingLevel smallint;
153
+
```sql
154
+
DECLARE @Grouping NVARCHAR(50);
155
+
DECLARE @GroupingLevel SMALLINT;
157
156
SET @Grouping = N'CountryRegionCode Total';
158
157
159
158
SELECT @GroupingLevel = (
@@ -243,14 +242,14 @@ ORDER BY
243
242
#### ROLLUP Example
244
243
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.
@@ -324,14 +323,14 @@ ORDER BY GROUPING_ID(DATEPART(mm,OrderDate)
324
323
325
324
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
0 commit comments