@@ -4,18 +4,14 @@ description: Learn about the GROUP BY clause for Azure Cosmos DB.
4
4
author : timsander1
5
5
ms.service : cosmos-db
6
6
ms.topic : conceptual
7
- ms.date : 04/10 /2020
7
+ ms.date : 05/19 /2020
8
8
ms.author : tisande
9
9
10
10
---
11
11
# GROUP BY clause in Azure Cosmos DB
12
12
13
13
The GROUP BY clause divides the query's results according to the values of one or more specified properties.
14
14
15
- > [ !NOTE]
16
- > Azure Cosmos DB currently supports GROUP BY in .NET SDK 3.3 and above as well as JavaScript SDK 3.4 and above.
17
- > Support for other language SDK's is not currently available but is planned.
18
-
19
15
## Syntax
20
16
21
17
``` sql
@@ -51,7 +47,12 @@ The GROUP BY clause divides the query's results according to the values of one o
51
47
Queries with an aggregate system function and a subquery with ` GROUP BY ` are not supported. For example, the following query is not supported:
52
48
53
49
``` sql
54
- SELECT COUNT (UniqueLastNames) FROM (SELECT AVG (f .age ) FROM f GROUP BY f .lastName ) AS UniqueLastNames
50
+ SELECT COUNT (UniqueLastNames)
51
+ FROM (
52
+ SELECT AVG (f .age )
53
+ FROM f
54
+ GROUP BY f .lastName
55
+ ) AS UniqueLastNames
55
56
```
56
57
57
58
## Examples
@@ -69,22 +70,24 @@ GROUP BY f.foodGroup
69
70
Some results are (TOP keyword is used to limit results):
70
71
71
72
``` json
72
- [{
73
- "foodGroup" : " Fast Foods" ,
74
- "foodGroupCount" : 371
75
- },
76
- {
77
- "foodGroup" : " Finfish and Shellfish Products" ,
78
- "foodGroupCount" : 267
79
- },
80
- {
81
- "foodGroup" : " Meals, Entrees, and Side Dishes" ,
82
- "foodGroupCount" : 113
83
- },
84
- {
85
- "foodGroup" : " Sausages and Luncheon Meats" ,
86
- "foodGroupCount" : 244
87
- }]
73
+ [
74
+ {
75
+ "foodGroupCount" : 183 ,
76
+ "foodGroup" : " Cereal Grains and Pasta"
77
+ },
78
+ {
79
+ "foodGroupCount" : 133 ,
80
+ "foodGroup" : " Nut and Seed Products"
81
+ },
82
+ {
83
+ "foodGroupCount" : 113 ,
84
+ "foodGroup" : " Meals, Entrees, and Side Dishes"
85
+ },
86
+ {
87
+ "foodGroupCount" : 64 ,
88
+ "foodGroup" : " Spices and Herbs"
89
+ }
90
+ ]
88
91
```
89
92
90
93
This query has two expressions used to divide results:
@@ -98,26 +101,28 @@ GROUP BY f.foodGroup, f.version
98
101
Some results are:
99
102
100
103
``` json
101
- [{
102
- "version" : 1 ,
103
- "foodGroup" : " Nut and Seed Products" ,
104
- "foodGroupCount" : 133
105
- },
106
- {
107
- "version" : 1 ,
108
- "foodGroup" : " Finfish and Shellfish Products" ,
109
- "foodGroupCount" : 267
110
- },
111
- {
112
- "version" : 1 ,
113
- "foodGroup" : " Fast Foods" ,
114
- "foodGroupCount" : 371
115
- },
116
- {
117
- "version" : 1 ,
118
- "foodGroup" : " Sausages and Luncheon Meats" ,
119
- "foodGroupCount" : 244
120
- }]
104
+ [
105
+ {
106
+ "foodGroupCount" : 183 ,
107
+ "foodGroup" : " Cereal Grains and Pasta" ,
108
+ "version" : 1
109
+ },
110
+ {
111
+ "foodGroupCount" : 133 ,
112
+ "foodGroup" : " Nut and Seed Products" ,
113
+ "version" : 1
114
+ },
115
+ {
116
+ "foodGroupCount" : 113 ,
117
+ "foodGroup" : " Meals, Entrees, and Side Dishes" ,
118
+ "version" : 1
119
+ },
120
+ {
121
+ "foodGroupCount" : 64 ,
122
+ "foodGroup" : " Spices and Herbs" ,
123
+ "version" : 1
124
+ }
125
+ ]
121
126
```
122
127
123
128
This query has a system function in the GROUP BY clause:
@@ -131,22 +136,24 @@ GROUP BY UPPER(f.foodGroup)
131
136
Some results are:
132
137
133
138
``` json
134
- [{
135
- "foodGroupCount" : 371 ,
136
- "upperFoodGroup" : " FAST FOODS"
137
- },
138
- {
139
- "foodGroupCount" : 267 ,
140
- "upperFoodGroup" : " FINFISH AND SHELLFISH PRODUCTS"
141
- },
142
- {
143
- "foodGroupCount" : 389 ,
144
- "upperFoodGroup" : " LEGUMES AND LEGUME PRODUCTS"
145
- },
146
- {
147
- "foodGroupCount" : 113 ,
148
- "upperFoodGroup" : " MEALS, ENTREES, AND SIDE DISHES"
149
- }]
139
+ [
140
+ {
141
+ "foodGroupCount" : 183 ,
142
+ "upperFoodGroup" : " CEREAL GRAINS AND PASTA"
143
+ },
144
+ {
145
+ "foodGroupCount" : 133 ,
146
+ "upperFoodGroup" : " NUT AND SEED PRODUCTS"
147
+ },
148
+ {
149
+ "foodGroupCount" : 113 ,
150
+ "upperFoodGroup" : " MEALS, ENTREES, AND SIDE DISHES"
151
+ },
152
+ {
153
+ "foodGroupCount" : 64 ,
154
+ "upperFoodGroup" : " SPICES AND HERBS"
155
+ }
156
+ ]
150
157
```
151
158
152
159
This query uses both keywords and system functions in the item property expression:
@@ -160,16 +167,18 @@ GROUP BY ARRAY_CONTAINS(f.tags, {name: 'orange'}), f.version BETWEEN 0 AND 2
160
167
The results are:
161
168
162
169
``` json
163
- [{
164
- "correctVersion" : true ,
165
- "containsOrangeTag" : false ,
166
- "foodGroupCount" : 8608
167
- },
168
- {
169
- "correctVersion" : true ,
170
- "containsOrangeTag" : true ,
171
- "foodGroupCount" : 10
172
- }]
170
+ [
171
+ {
172
+ "foodGroupCount" : 10 ,
173
+ "containsOrangeTag" : true ,
174
+ "correctVersion" : true
175
+ },
176
+ {
177
+ "foodGroupCount" : 8608 ,
178
+ "containsOrangeTag" : false ,
179
+ "correctVersion" : true
180
+ }
181
+ ]
173
182
```
174
183
175
184
## Next steps
0 commit comments