|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Aggregation Functions |
| 4 | +parent: SQL |
| 5 | +nav_order: 11 |
| 6 | +--- |
| 7 | + |
| 8 | +# Aggregation functions |
| 9 | + |
| 10 | +Aggregate functions use the `GROUP BY` clause to group sets of values into subsets. |
| 11 | + |
| 12 | +## Group By |
| 13 | + |
| 14 | +Use the `GROUP BY` clause as an identifier, ordinal, or expression. |
| 15 | + |
| 16 | +### Identifier |
| 17 | + |
| 18 | +```sql |
| 19 | +SELECT gender, sum(age) FROM accounts GROUP BY gender; |
| 20 | +``` |
| 21 | + |
| 22 | +| gender | sum (age) |
| 23 | +:--- | :--- |
| 24 | +F | 28 | |
| 25 | +M | 101 | |
| 26 | + |
| 27 | +### Ordinal |
| 28 | + |
| 29 | +```sql |
| 30 | +SELECT gender, sum(age) FROM accounts GROUP BY 1; |
| 31 | +``` |
| 32 | + |
| 33 | +| gender | sum (age) |
| 34 | +:--- | :--- |
| 35 | +F | 28 | |
| 36 | +M | 101 | |
| 37 | + |
| 38 | +### Expression |
| 39 | + |
| 40 | +```sql |
| 41 | +SELECT abs(account_number), sum(age) FROM accounts GROUP BY abs(account_number); |
| 42 | +``` |
| 43 | + |
| 44 | +| abs(account_number) | sum (age) |
| 45 | +:--- | :--- |
| 46 | +| 1 | 32 | |
| 47 | +| 13 | 28 | |
| 48 | +| 18 | 33 | |
| 49 | +| 6 | 36 | |
| 50 | + |
| 51 | +## Aggregation |
| 52 | + |
| 53 | +Use aggregations as a select, expression, or an argument of an expression. |
| 54 | + |
| 55 | +### Select |
| 56 | + |
| 57 | +```sql |
| 58 | +SELECT gender, sum(age) FROM accounts GROUP BY gender; |
| 59 | +``` |
| 60 | + |
| 61 | +| gender | sum (age) |
| 62 | +:--- | :--- |
| 63 | +F | 28 | |
| 64 | +M | 101 | |
| 65 | + |
| 66 | +### Argument |
| 67 | + |
| 68 | +```sql |
| 69 | +SELECT gender, sum(age) * 2 as sum2 FROM accounts GROUP BY gender; |
| 70 | +``` |
| 71 | + |
| 72 | +| gender | sum2 |
| 73 | +:--- | :--- |
| 74 | +F | 56 | |
| 75 | +M | 202 | |
| 76 | + |
| 77 | +### Expression |
| 78 | + |
| 79 | +```sql |
| 80 | +SELECT gender, sum(age * 2) as sum2 FROM accounts GROUP BY gender; |
| 81 | +``` |
| 82 | + |
| 83 | +| gender | sum2 |
| 84 | +:--- | :--- |
| 85 | +F | 56 | |
| 86 | +M | 202 | |
| 87 | + |
| 88 | +### COUNT |
| 89 | + |
| 90 | +Use the `COUNT` function to accept arguments such as a `*` or a literal like `1`. |
| 91 | +The meaning of these different forms are as follows: |
| 92 | + |
| 93 | +- `COUNT(field)` - Only counts if given a field (or expression) is not null or missing in the input rows. |
| 94 | +- `COUNT(*)` - Counts the number of all its input rows. |
| 95 | +- `COUNT(1)` (same as `COUNT(*)`) - Counts any non-null literal. |
| 96 | + |
| 97 | +## Having |
| 98 | + |
| 99 | +Use the `HAVING` clause to filter out aggregated values. |
| 100 | + |
| 101 | +### HAVING with GROUP BY |
| 102 | + |
| 103 | +You can use aggregate expressions or its aliases defined in a `SELECT` clause in a `HAVING` condition. |
| 104 | + |
| 105 | +We recommend using a non-aggregate expression in the `WHERE` clause although you can do this in a `HAVING` clause. |
| 106 | + |
| 107 | +The aggregations in a `HAVING` clause are not necessarily the same as that in a select list. As an extension to the SQL standard, you're not restricted to using identifiers only in the `GROUP BY` list. |
| 108 | +For example: |
| 109 | + |
| 110 | +```sql |
| 111 | +SELECT gender, sum(age) |
| 112 | +FROM accounts |
| 113 | +GROUP BY gender |
| 114 | +HAVING sum(age) > 100; |
| 115 | +``` |
| 116 | + |
| 117 | +| gender | sum (age) |
| 118 | +:--- | :--- |
| 119 | +M | 101 | |
| 120 | + |
| 121 | +Here's another example for using an alias in a `HAVING` condition. |
| 122 | + |
| 123 | +```sql |
| 124 | +SELECT gender, sum(age) AS s |
| 125 | +FROM accounts |
| 126 | +GROUP BY gender |
| 127 | +HAVING s > 100; |
| 128 | +``` |
| 129 | + |
| 130 | +| gender | s |
| 131 | +:--- | :--- |
| 132 | +M | 101 | |
| 133 | + |
| 134 | +If an identifier is ambiguous, for example, present both as a select alias and as an index field (preference is alias). In this case, the identifier is replaced with an expression aliased in the `SELECT` clause: |
| 135 | + |
| 136 | +### HAVING without GROUP BY |
| 137 | + |
| 138 | +You can use a `HAVING` clause without the `GROUP BY` clause. This is useful because aggregations are not supported in a `WHERE` clause: |
| 139 | + |
| 140 | +```sql |
| 141 | +SELECT 'Total of age > 100' |
| 142 | +FROM accounts |
| 143 | +HAVING sum(age) > 100; |
| 144 | +``` |
| 145 | + |
| 146 | +| Total of age > 100 | |
| 147 | +:--- | |
| 148 | +Total of age > 100 | |
0 commit comments