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
Copy file name to clipboardexpand all lines: docs/user/dql/aggregations.rst
+108-108
Original file line number
Diff line number
Diff line change
@@ -34,12 +34,12 @@ The group by expression could be identifier::
34
34
35
35
os> SELECT gender, sum(age) FROM accounts GROUP BY gender;
36
36
fetched rows / total rows = 2/2
37
-
+----------+------------+
38
-
| gender | sum(age) |
39
-
|----------+------------|
40
-
| F | 28 |
41
-
| M | 101 |
42
-
+----------+------------+
37
+
+--------+----------+
38
+
| gender | sum(age) |
39
+
|--------+----------|
40
+
| F | 28 |
41
+
| M | 101 |
42
+
+--------+----------+
43
43
44
44
45
45
Ordinal
@@ -49,12 +49,12 @@ The group by expression could be ordinal::
49
49
50
50
os> SELECT gender, sum(age) FROM accounts GROUP BY 1;
51
51
fetched rows / total rows = 2/2
52
-
+----------+------------+
53
-
| gender | sum(age) |
54
-
|----------+------------|
55
-
| F | 28 |
56
-
| M | 101 |
57
-
+----------+------------+
52
+
+--------+----------+
53
+
| gender | sum(age) |
54
+
|--------+----------|
55
+
| F | 28 |
56
+
| M | 101 |
57
+
+--------+----------+
58
58
59
59
60
60
Expression
@@ -64,14 +64,14 @@ The group by expression could be expression::
64
64
65
65
os> SELECT abs(account_number), sum(age) FROM accounts GROUP BY abs(account_number);
66
66
fetched rows / total rows = 4/4
67
-
+-----------------------+------------+
68
-
| abs(account_number) | sum(age) |
69
-
|-----------------------+------------|
70
-
| 1 | 32 |
71
-
| 13 | 28 |
72
-
| 18 | 33 |
73
-
| 6 | 36 |
74
-
+-----------------------+------------+
67
+
+---------------------+----------+
68
+
| abs(account_number) | sum(age) |
69
+
|---------------------+----------|
70
+
| 1 | 32 |
71
+
| 13 | 28 |
72
+
| 18 | 33 |
73
+
| 6 | 36 |
74
+
+---------------------+----------+
75
75
76
76
77
77
Aggregation
@@ -91,12 +91,12 @@ The aggregation could be used select::
91
91
92
92
os> SELECT gender, sum(age) FROM accounts GROUP BY gender;
93
93
fetched rows / total rows = 2/2
94
-
+----------+------------+
95
-
| gender | sum(age) |
96
-
|----------+------------|
97
-
| F | 28 |
98
-
| M | 101 |
99
-
+----------+------------+
94
+
+--------+----------+
95
+
| gender | sum(age) |
96
+
|--------+----------|
97
+
| F | 28 |
98
+
| M | 101 |
99
+
+--------+----------+
100
100
101
101
Expression over Aggregation
102
102
---------------------------
@@ -105,12 +105,12 @@ The aggregation could be used as arguments of expression::
105
105
106
106
os> SELECT gender, sum(age) * 2 as sum2 FROM accounts GROUP BY gender;
107
107
fetched rows / total rows = 2/2
108
-
+----------+--------+
109
-
| gender | sum2 |
110
-
|----------+--------|
111
-
| F | 56 |
112
-
| M | 202 |
113
-
+----------+--------+
108
+
+--------+------+
109
+
| gender | sum2 |
110
+
|--------+------|
111
+
| F | 56 |
112
+
| M | 202 |
113
+
+--------+------+
114
114
115
115
Expression as argument of Aggregation
116
116
-------------------------------------
@@ -119,12 +119,12 @@ The aggregation could has expression as arguments::
119
119
120
120
os> SELECT gender, sum(age * 2) as sum2 FROM accounts GROUP BY gender;
121
121
fetched rows / total rows = 2/2
122
-
+----------+--------+
123
-
| gender | sum2 |
124
-
|----------+--------|
125
-
| F | 56 |
126
-
| M | 202 |
127
-
+----------+--------+
122
+
+--------+------+
123
+
| gender | sum2 |
124
+
|--------+------|
125
+
| F | 56 |
126
+
| M | 202 |
127
+
+--------+------+
128
128
129
129
COUNT Aggregations
130
130
------------------
@@ -150,12 +150,12 @@ Example::
150
150
151
151
os> SELECT gender, count(*) as countV FROM accounts GROUP BY gender;
152
152
fetched rows / total rows = 2/2
153
-
+----------+----------+
154
-
| gender | countV |
155
-
|----------+----------|
156
-
| F | 1 |
157
-
| M | 3 |
158
-
+----------+----------+
153
+
+--------+--------+
154
+
| gender | countV |
155
+
|--------+--------|
156
+
| F | 1 |
157
+
| M | 3 |
158
+
+--------+--------+
159
159
160
160
SUM
161
161
---
@@ -169,12 +169,12 @@ Example::
169
169
170
170
os> SELECT gender, sum(age) as sumV FROM accounts GROUP BY gender;
171
171
fetched rows / total rows = 2/2
172
-
+----------+--------+
173
-
| gender | sumV |
174
-
|----------+--------|
175
-
| F | 28 |
176
-
| M | 101 |
177
-
+----------+--------+
172
+
+--------+------+
173
+
| gender | sumV |
174
+
|--------+------|
175
+
| F | 28 |
176
+
| M | 101 |
177
+
+--------+------+
178
178
179
179
AVG
180
180
---
@@ -188,12 +188,12 @@ Example::
188
188
189
189
os> SELECT gender, avg(age) as avgV FROM accounts GROUP BY gender;
190
190
fetched rows / total rows = 2/2
191
-
+----------+--------------------+
192
-
| gender | avgV |
193
-
|----------+--------------------|
194
-
| F | 28.0 |
195
-
| M | 33.666666666666664 |
196
-
+----------+--------------------+
191
+
+--------+--------------------+
192
+
| gender | avgV |
193
+
|--------+--------------------|
194
+
| F | 28.0 |
195
+
| M | 33.666666666666664 |
196
+
+--------+--------------------+
197
197
198
198
MAX
199
199
---
@@ -207,11 +207,11 @@ Example::
207
207
208
208
os> SELECT max(age) as maxV FROM accounts;
209
209
fetched rows / total rows = 1/1
210
-
+--------+
211
-
| maxV |
212
-
|--------|
213
-
| 36 |
214
-
+--------+
210
+
+------+
211
+
| maxV |
212
+
|------|
213
+
| 36 |
214
+
+------+
215
215
216
216
MIN
217
217
---
@@ -225,11 +225,11 @@ Example::
225
225
226
226
os> SELECT min(age) as minV FROM accounts;
227
227
fetched rows / total rows = 1/1
228
-
+--------+
229
-
| minV |
230
-
|--------|
231
-
| 28 |
232
-
+--------+
228
+
+------+
229
+
| minV |
230
+
|------|
231
+
| 28 |
232
+
+------+
233
233
234
234
VAR_POP
235
235
-------
@@ -364,11 +364,11 @@ To get the count of distinct values of a field, you can add a keyword ``DISTINCT
364
364
365
365
os> SELECT COUNT(DISTINCT gender), COUNT(gender) FROM accounts;
366
366
fetched rows / total rows = 1/1
367
-
+--------------------------+-----------------+
368
-
| COUNT(DISTINCT gender) | COUNT(gender) |
369
-
|--------------------------+-----------------|
370
-
| 2 | 4 |
371
-
+--------------------------+-----------------+
367
+
+------------------------+---------------+
368
+
| COUNT(DISTINCT gender) | COUNT(gender) |
369
+
|------------------------+---------------|
370
+
| 2 | 4 |
371
+
+------------------------+---------------+
372
372
373
373
PERCENTILE or PERCENTILE_APPROX
374
374
-------------------------------
@@ -382,12 +382,12 @@ Example::
382
382
383
383
os> SELECT gender, percentile(age, 90) as p90 FROM accounts GROUP BY gender;
384
384
fetched rows / total rows = 2/2
385
-
+----------+-------+
386
-
| gender | p90 |
387
-
|----------+-------|
388
-
| F | 28 |
389
-
| M | 36 |
390
-
+----------+-------+
385
+
+--------+-----+
386
+
| gender | p90 |
387
+
|--------+-----|
388
+
| F | 28 |
389
+
| M | 36 |
390
+
+--------+-----+
391
391
392
392
HAVING Clause
393
393
=============
@@ -413,11 +413,11 @@ Here is an example for typical use of ``HAVING`` clause::
413
413
... GROUP BY gender
414
414
... HAVING sum(age) > 100;
415
415
fetched rows / total rows = 1/1
416
-
+----------+------------+
417
-
| gender | sum(age) |
418
-
|----------+------------|
419
-
| M | 101 |
420
-
+----------+------------+
416
+
+--------+----------+
417
+
| gender | sum(age) |
418
+
|--------+----------|
419
+
| M | 101 |
420
+
+--------+----------+
421
421
422
422
Here is another example for using alias in ``HAVING`` condition. Note that if an identifier is ambiguous, for example present both as a select alias and an index field, preference is alias. This means the identifier will be replaced by expression aliased in ``SELECT`` clause::
423
423
@@ -427,11 +427,11 @@ Here is another example for using alias in ``HAVING`` condition. Note that if an
427
427
... GROUP BY gender
428
428
... HAVING s > 100;
429
429
fetched rows / total rows = 1/1
430
-
+----------+-----+
431
-
| gender | s |
432
-
|----------+-----|
433
-
| M | 101 |
434
-
+----------+-----+
430
+
+--------+-----+
431
+
| gender | s |
432
+
|--------+-----|
433
+
| M | 101 |
434
+
+--------+-----+
435
435
436
436
HAVING without GROUP BY
437
437
-----------------------
@@ -443,11 +443,11 @@ Additionally, a ``HAVING`` clause can work without ``GROUP BY`` clause. This is
443
443
... FROM accounts
444
444
... HAVING sum(age) > 100;
445
445
fetched rows / total rows = 1/1
446
-
+------------------------+
447
-
| 'Total of age > 100' |
448
-
|------------------------|
449
-
| Total of age > 100 |
450
-
+------------------------+
446
+
+----------------------+
447
+
| 'Total of age > 100' |
448
+
|----------------------|
449
+
| Total of age > 100 |
450
+
+----------------------+
451
451
452
452
453
453
FILTER Clause
@@ -465,12 +465,12 @@ The group by aggregation with ``FILTER`` clause can set different conditions for
465
465
466
466
os> SELECT avg(age) FILTER(WHERE balance > 10000) AS filtered, gender FROM accounts GROUP BY gender
467
467
fetched rows / total rows = 2/2
468
-
+------------+----------+
469
-
| filtered | gender |
470
-
|------------+----------|
471
-
| 28.0 | F |
472
-
| 32.0 | M |
473
-
+------------+----------+
468
+
+----------+--------+
469
+
| filtered | gender |
470
+
|----------+--------|
471
+
| 28.0 | F |
472
+
| 32.0 | M |
473
+
+----------+--------+
474
474
475
475
FILTER without GROUP BY
476
476
-----------------------
@@ -482,11 +482,11 @@ The ``FILTER`` clause can be used in aggregation functions without GROUP BY as w
482
482
... count(*) FILTER(WHERE age > 34) AS filtered
483
483
... FROM accounts
484
484
fetched rows / total rows = 1/1
485
-
+--------------+------------+
486
-
| unfiltered | filtered |
487
-
|--------------+------------|
488
-
| 4 | 1 |
489
-
+--------------+------------+
485
+
+------------+----------+
486
+
| unfiltered | filtered |
487
+
|------------+----------|
488
+
| 4 | 1 |
489
+
+------------+----------+
490
490
491
491
Distinct count aggregate with FILTER
492
492
------------------------------------
@@ -495,9 +495,9 @@ The ``FILTER`` clause is also used in distinct count to do the filtering before
495
495
496
496
os> SELECT COUNT(DISTINCT firstname) FILTER(WHERE age > 30) AS distinct_count FROM accounts
0 commit comments