1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Aggregate A Column Into An Array as a postgres til.

This commit is contained in:
jbranchaud
2015-12-22 23:31:24 -06:00
parent aafb1553fa
commit 531175dbca
2 changed files with 30 additions and 0 deletions

View File

@@ -115,6 +115,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
- [A Better Null Display Character](postgres/a-better-null-display-character.md)
- [Adding Composite Uniqueness Constraints](postgres/adding-composite-uniqueness-constraints.md)
- [Aggregate A Column Into An Array](postgres/aggregate-a-column-into-an-array.md)
- [Auto Expanded Display](postgres/auto-expanded-display.md)
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
- [Configure The Timezone](postgres/configure-the-timezone.md)

View File

@@ -0,0 +1,29 @@
# Aggregate A Column Into An Array
PostgreSQL's `array_agg` function can be used to aggregate a column into an
array. Consider the following column:
```sql
> select num from generate_series(1,5) as num;
num
-----
1
2
3
4
5
```
By wrapping the `array_agg` aggregate function around `num` we are able to
*aggregate* the values in that column into an array, like so:
```sql
> select array_agg(num) from generate_series(1,5) as num;
array_agg
-------------
{1,2,3,4,5}
```
See the docs on [aggregate
functions](http://www.postgresql.org/docs/current/static/functions-aggregate.html)
for more details.