Skip to content

Commit 1932a35

Browse files
authored
Merge pull request #115725 from timsander1/master
fix group by alias order to match change in behavior
2 parents f58fe86 + 27b4274 commit 1932a35

File tree

1 file changed

+77
-68
lines changed

1 file changed

+77
-68
lines changed

articles/cosmos-db/sql-query-group-by.md

Lines changed: 77 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@ description: Learn about the GROUP BY clause for Azure Cosmos DB.
44
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 04/10/2020
7+
ms.date: 05/19/2020
88
ms.author: tisande
99

1010
---
1111
# GROUP BY clause in Azure Cosmos DB
1212

1313
The GROUP BY clause divides the query's results according to the values of one or more specified properties.
1414

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-
1915
## Syntax
2016

2117
```sql
@@ -51,7 +47,12 @@ The GROUP BY clause divides the query's results according to the values of one o
5147
Queries with an aggregate system function and a subquery with `GROUP BY` are not supported. For example, the following query is not supported:
5248

5349
```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
5556
```
5657

5758
## Examples
@@ -69,22 +70,24 @@ GROUP BY f.foodGroup
6970
Some results are (TOP keyword is used to limit results):
7071

7172
```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+
]
8891
```
8992

9093
This query has two expressions used to divide results:
@@ -98,26 +101,28 @@ GROUP BY f.foodGroup, f.version
98101
Some results are:
99102

100103
```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+
]
121126
```
122127

123128
This query has a system function in the GROUP BY clause:
@@ -131,22 +136,24 @@ GROUP BY UPPER(f.foodGroup)
131136
Some results are:
132137

133138
```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+
]
150157
```
151158

152159
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
160167
The results are:
161168

162169
```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+
]
173182
```
174183

175184
## Next steps

0 commit comments

Comments
 (0)