@@ -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
@@ -69,22 +65,24 @@ GROUP BY f.foodGroup
69
65
Some results are (TOP keyword is used to limit results):
70
66
71
67
``` 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
- }]
68
+ [
69
+ {
70
+ "foodGroupCount" : 183 ,
71
+ "foodGroup" : " Cereal Grains and Pasta"
72
+ },
73
+ {
74
+ "foodGroupCount" : 133 ,
75
+ "foodGroup" : " Nut and Seed Products"
76
+ },
77
+ {
78
+ "foodGroupCount" : 113 ,
79
+ "foodGroup" : " Meals, Entrees, and Side Dishes"
80
+ },
81
+ {
82
+ "foodGroupCount" : 64 ,
83
+ "foodGroup" : " Spices and Herbs"
84
+ }
85
+ ]
88
86
```
89
87
90
88
This query has two expressions used to divide results:
@@ -98,26 +96,28 @@ GROUP BY f.foodGroup, f.version
98
96
Some results are:
99
97
100
98
``` 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
- }]
99
+ [
100
+ {
101
+ "foodGroupCount" : 183 ,
102
+ "foodGroup" : " Cereal Grains and Pasta" ,
103
+ "version" : 1
104
+ },
105
+ {
106
+ "foodGroupCount" : 133 ,
107
+ "foodGroup" : " Nut and Seed Products" ,
108
+ "version" : 1
109
+ },
110
+ {
111
+ "foodGroupCount" : 113 ,
112
+ "foodGroup" : " Meals, Entrees, and Side Dishes" ,
113
+ "version" : 1
114
+ },
115
+ {
116
+ "foodGroupCount" : 64 ,
117
+ "foodGroup" : " Spices and Herbs" ,
118
+ "version" : 1
119
+ }
120
+ ]
121
121
```
122
122
123
123
This query has a system function in the GROUP BY clause:
@@ -131,22 +131,24 @@ GROUP BY UPPER(f.foodGroup)
131
131
Some results are:
132
132
133
133
``` 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
- }]
134
+ [
135
+ {
136
+ "foodGroupCount" : 183 ,
137
+ "upperFoodGroup" : " CEREAL GRAINS AND PASTA"
138
+ },
139
+ {
140
+ "foodGroupCount" : 133 ,
141
+ "upperFoodGroup" : " NUT AND SEED PRODUCTS"
142
+ },
143
+ {
144
+ "foodGroupCount" : 113 ,
145
+ "upperFoodGroup" : " MEALS, ENTREES, AND SIDE DISHES"
146
+ },
147
+ {
148
+ "foodGroupCount" : 64 ,
149
+ "upperFoodGroup" : " SPICES AND HERBS"
150
+ }
151
+ ]
150
152
```
151
153
152
154
This query uses both keywords and system functions in the item property expression:
@@ -160,16 +162,18 @@ GROUP BY ARRAY_CONTAINS(f.tags, {name: 'orange'}), f.version BETWEEN 0 AND 2
160
162
The results are:
161
163
162
164
``` json
163
- [{
164
- "correctVersion" : true ,
165
- "containsOrangeTag" : false ,
166
- "foodGroupCount" : 8608
167
- },
168
- {
169
- "correctVersion" : true ,
170
- "containsOrangeTag" : true ,
171
- "foodGroupCount" : 10
172
- }]
165
+ [
166
+ {
167
+ "foodGroupCount" : 10 ,
168
+ "containsOrangeTag" : true ,
169
+ "correctVersion" : true
170
+ },
171
+ {
172
+ "foodGroupCount" : 8608 ,
173
+ "containsOrangeTag" : false ,
174
+ "correctVersion" : true
175
+ }
176
+ ]
173
177
```
174
178
175
179
## Next steps
0 commit comments