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
> 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.
20
20
21
21
## Syntax
22
22
@@ -30,166 +30,47 @@ The GROUP BY clause divides the query's results according to the values of one o
30
30
31
31
## Arguments
32
32
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
34
39
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.
36
41
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.
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
-
SELECTCOUNT(UniqueLastNames)
57
-
FROM (
58
-
SELECTAVG(f.age)
59
-
FROM f
60
-
GROUP BYf.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.
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 4COUNT(1) AS foodGroupCount, f.foodGroup
74
-
FROM Food f
75
-
GROUP BYf.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 4COUNT(1) AS foodGroupCount, f.foodGroup, f.version
105
-
FROM Food f
106
-
GROUP BYf.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 4COUNT(1) AS foodGroupCount, UPPER(f.foodGroup) AS upperFoodGroup
140
-
FROM Food f
141
-
GROUP BYUPPER(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
-
SELECTCOUNT(1) AS foodGroupCount, ARRAY_CONTAINS(f.tags, {name: 'orange'}) AS containsOrangeTag, f.version BETWEEN 0AND2AS correctVersion
171
-
FROM Food f
172
-
GROUP BY ARRAY_CONTAINS(f.tags, {name: 'orange'}), f.version BETWEEN 0AND2
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.
0 commit comments