diff --git a/README.md b/README.md index 166955d..a174d8f 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/postgres/aggregate-a-column-into-an-array.md b/postgres/aggregate-a-column-into-an-array.md new file mode 100644 index 0000000..c769d73 --- /dev/null +++ b/postgres/aggregate-a-column-into-an-array.md @@ -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.