Skip to content

Commit 62dcfc9

Browse files
committed
Add GROUP BY
1 parent 3d780c3 commit 62dcfc9

File tree

1 file changed

+49
-168
lines changed

1 file changed

+49
-168
lines changed
Lines changed: 49 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
---
2-
title: GROUP BY clause in Azure Cosmos DB
3-
description: Learn about the GROUP BY clause for Azure Cosmos DB.
4-
author: seesharprun
2+
title: GROUP BY
3+
titleSuffix: Azure Cosmos DB for NoSQL
4+
description: An Azure Cosmos DB for NoSQL clause that splits the results according to specified properties.
5+
author: jcodella
6+
ms.author: jacodel
7+
ms.reviewer: sidandrews
58
ms.service: cosmos-db
69
ms.subservice: nosql
7-
ms.custom: ignite-2022
8-
ms.topic: conceptual
9-
ms.date: 05/12/2022
10-
ms.author: sidandrews
11-
ms.reviewer: jucocchi
10+
ms.topic: reference
11+
ms.date: 07/31/2023
12+
ms.custom: query-reference
1213
---
13-
# GROUP BY clause in Azure Cosmos DB
14-
[!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)]
1514

16-
The GROUP BY clause divides the query's results according to the values of one or more specified properties.
15+
# GROUP BY (NoSQL query)
16+
17+
[!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)]
1718

18-
> [!NOTE]
19-
> The GROUP BY clause is not supported in the Azure Cosmos DB Python SDK.
19+
The ``GROUP BY`` clause divides the query's results according to the values of one or more specified properties.
2020

2121
## Syntax
2222

@@ -30,166 +30,47 @@ The GROUP BY clause divides the query's results according to the values of one o
3030

3131
## Arguments
3232

33-
- `<scalar_expression_list>`
33+
| | Description |
34+
| --- | --- |
35+
| **``<scalar_expression_list>``** | Specifies the expressions that are used to group (or divide) query results. |
36+
| **``<scalar_expression>``** | Any scalar expression is allowed except for scalar subqueries and scalar aggregates. Each scalar expression must contain at least one property reference. There's no limit to the number of individual expressions or the cardinality of each expression. |
37+
38+
## Examples
3439

35-
Specifies the expressions that will be used to divide query results.
40+
For the examples in this section, this reference set of items is used.
3641

37-
- `<scalar_expression>`
38-
39-
Any scalar expression is allowed except for scalar subqueries and scalar aggregates. Each scalar expression must contain at least one property reference. There is no limit to the number of individual expressions or the cardinality of each expression.
42+
:::code language="json" source="~/cosmos-db-nosql-query-samples/scripts/group-by/seed.json" range="1-2,4-11,13-20,22-29,31-38,40-46,48-55,57-64" highlight="4-7,12-15,20-23,28-31,36-38,43-46,51-54":::
4043

41-
## Remarks
42-
43-
When a query uses a GROUP BY clause, the SELECT clause can only contain the subset of properties and system functions included in the GROUP BY clause. One exception is aggregate functions, which can appear in the SELECT clause without being included in the GROUP BY clause. You can also always include literal values in the SELECT clause.
44-
45-
The GROUP BY clause must be after the SELECT, FROM, and WHERE clause and before the OFFSET LIMIT clause. You cannot use GROUP BY with an ORDER BY clause.
46-
47-
The GROUP BY clause does not allow any of the following:
48-
49-
- Aliasing properties or aliasing system functions (aliasing is still allowed within the SELECT clause)
50-
- Subqueries
51-
- Aggregate system functions (these are only allowed in the SELECT clause)
52-
53-
Queries with an aggregate system function and a subquery with `GROUP BY` are not supported. For example, the following query is not supported:
54-
55-
```sql
56-
SELECT COUNT(UniqueLastNames)
57-
FROM (
58-
SELECT AVG(f.age)
59-
FROM f
60-
GROUP BY f.lastName
61-
) AS UniqueLastNames
62-
```
63-
64-
Additionally, cross-partition `GROUP BY` queries can have a maximum of 21 aggregate system functions.
44+
In this first example, the ``GROUP BY`` clause is used to create groups of items using the value of a specified property.
6545

66-
## Examples
46+
:::code language="sql" source="~/cosmos-db-nosql-query-samples/scripts/group-by/query.sql" range="1-4,7-8" highlight="5-6":::
47+
48+
:::code language="json" source="~/cosmos-db-nosql-query-samples/scripts/group-by/result.json":::
49+
50+
In this next example, an aggregate system function ([``COUNT``](count.md)) is used with the groupings to provide a total number of items per group.
51+
52+
:::code language="sql" source="~/cosmos-db-nosql-query-samples/scripts/group-by-aggregate/query.sql" range="1-5,8-9" highlight="6-7":::
53+
54+
:::code language="json" source="~/cosmos-db-nosql-query-samples/scripts/group-by-aggregate/result.json":::
55+
56+
In this final example, the items are grouped using multiple properties.
57+
58+
:::code language="sql" source="~/cosmos-db-nosql-query-samples/scripts/group-by-multiple/query.sql" range="1-6,9-11" highlight="7-9":::
59+
60+
:::code language="json" source="~/cosmos-db-nosql-query-samples/scripts/group-by-multiple/result.json":::
61+
62+
## Remarks
6763

68-
These examples use a sample [nutrition data set](https://github.com/AzureCosmosDB/labs/blob/master/dotnet/setup/NutritionData.json).
69-
70-
Here's a query which returns the total count of items in each foodGroup:
71-
72-
```sql
73-
SELECT TOP 4 COUNT(1) AS foodGroupCount, f.foodGroup
74-
FROM Food f
75-
GROUP BY f.foodGroup
76-
```
77-
78-
Some results are (TOP keyword is used to limit results):
79-
80-
```json
81-
[
82-
{
83-
"foodGroupCount": 183,
84-
"foodGroup": "Cereal Grains and Pasta"
85-
},
86-
{
87-
"foodGroupCount": 133,
88-
"foodGroup": "Nut and Seed Products"
89-
},
90-
{
91-
"foodGroupCount": 113,
92-
"foodGroup": "Meals, Entrees, and Side Dishes"
93-
},
94-
{
95-
"foodGroupCount": 64,
96-
"foodGroup": "Spices and Herbs"
97-
}
98-
]
99-
```
100-
101-
This query has two expressions used to divide results:
102-
103-
```sql
104-
SELECT TOP 4 COUNT(1) AS foodGroupCount, f.foodGroup, f.version
105-
FROM Food f
106-
GROUP BY f.foodGroup, f.version
107-
```
108-
109-
Some results are:
110-
111-
```json
112-
[
113-
{
114-
"foodGroupCount": 183,
115-
"foodGroup": "Cereal Grains and Pasta",
116-
"version": 1
117-
},
118-
{
119-
"foodGroupCount": 133,
120-
"foodGroup": "Nut and Seed Products",
121-
"version": 1
122-
},
123-
{
124-
"foodGroupCount": 113,
125-
"foodGroup": "Meals, Entrees, and Side Dishes",
126-
"version": 1
127-
},
128-
{
129-
"foodGroupCount": 64,
130-
"foodGroup": "Spices and Herbs",
131-
"version": 1
132-
}
133-
]
134-
```
135-
136-
This query has a system function in the GROUP BY clause:
137-
138-
```sql
139-
SELECT TOP 4 COUNT(1) AS foodGroupCount, UPPER(f.foodGroup) AS upperFoodGroup
140-
FROM Food f
141-
GROUP BY UPPER(f.foodGroup)
142-
```
143-
144-
Some results are:
145-
146-
```json
147-
[
148-
{
149-
"foodGroupCount": 183,
150-
"upperFoodGroup": "CEREAL GRAINS AND PASTA"
151-
},
152-
{
153-
"foodGroupCount": 133,
154-
"upperFoodGroup": "NUT AND SEED PRODUCTS"
155-
},
156-
{
157-
"foodGroupCount": 113,
158-
"upperFoodGroup": "MEALS, ENTREES, AND SIDE DISHES"
159-
},
160-
{
161-
"foodGroupCount": 64,
162-
"upperFoodGroup": "SPICES AND HERBS"
163-
}
164-
]
165-
```
166-
167-
This query uses both keywords and system functions in the item property expression:
168-
169-
```sql
170-
SELECT COUNT(1) AS foodGroupCount, ARRAY_CONTAINS(f.tags, {name: 'orange'}) AS containsOrangeTag, f.version BETWEEN 0 AND 2 AS correctVersion
171-
FROM Food f
172-
GROUP BY ARRAY_CONTAINS(f.tags, {name: 'orange'}), f.version BETWEEN 0 AND 2
173-
```
174-
175-
The results are:
176-
177-
```json
178-
[
179-
{
180-
"foodGroupCount": 10,
181-
"containsOrangeTag": true,
182-
"correctVersion": true
183-
},
184-
{
185-
"foodGroupCount": 8608,
186-
"containsOrangeTag": false,
187-
"correctVersion": true
188-
}
189-
]
190-
```
64+
- When a query uses a ``GROUP BY`` clause, the ``SELECT`` clause can only contain the subset of properties and system functions included in the ``GROUP BY`` clause. One exception is aggregate functions, which can appear in the ``SELECT`` clause without being included in the ``GROUP BY`` clause. You can also always include literal values in the ``SELECT`` clause.
65+
- The ``GROUP BY`` clause must be after the ``SELECT``, ``FROM``, and ``WHERE`` clause and before the ``OFFSET LIMIT`` clause. You can't use ``GROUP BY`` with an ``ORDER BY`` clause.
66+
- The ``GROUP BY`` clause doesn't allow any of the following features, properties, or functions:
67+
- Aliasing properties or aliasing system functions (aliasing is still allowed within the ``SELECT`` clause)
68+
- Subqueries
69+
- Aggregate system functions (these functions are only allowed in the ``SELECT`` clause)
70+
- Queries with an aggregate system function and a subquery with ``GROUP BY`` aren't supported.
71+
- Cross-partition ``GROUP BY`` queries can have a maximum of **21** aggregate system functions.
19172

19273
## Next steps
19374

194-
- [Getting started](getting-started.md)
195-
- [SELECT clause](select.md)
75+
- [``ORDER BY`` clause](order-by.md)
76+
- [``OFFSET LIMIT`` clause](offset-limit.md)

0 commit comments

Comments
 (0)