array_agg()
is an aggregate function in DuckDB that allows you to combine values from multiple rows into a list. This article explores how array_agg()
works, along with some examples that demonstrate its usage.
Tag: arrays
Concatenate Array Elements into a String in PostgreSQL
You may be aware that PostgreSQL has a couple of functions that allow us to concatenate strings. In particular, the concat()
function allows us to concatenate multiple strings into one string, and the concat_ws()
function allows us to do the same, but to also specify a separator for the concatenated strings.
But did you know that we have the ability to pass an array to these functions?
Continue readingEnlarging an Array in PostgreSQL
In PostgreSQL it’s possible to make an array larger without providing any values. For example, we can take an array with three elements, and enlarge it so that it contains say, five elements, with the two extra elements being NULL
.
We can also do it so that some of the extra elements are non-NULL
and others are NULL
.
How to Update an Array Slice in PostgreSQL
When we update an array in PostgreSQL, we can update one specific element or we can update a whole slice (a range of elements).
To update a slice, we use a syntax that references the lower and upper bounds of the slice, separated by a colon. We also provide the new values that will be assigned to each element within that slice.
Continue readingUsing the VARIADIC Keyword with the FORMAT() Function in PostgreSQL
When we use the format()
function in PostgreSQL, we can pass any number of strings to insert into the resulting formatted string in positions that are specified in the first argument. When we do this, we might typically pass the strings as separate arguments. But this isn’t the only way to do it.
We have the option of passing the strings as array elements. That is, we can pass an array to the function, and have it extract each array element as though it’s a separate argument to insert into the formatted string.
Continue readingHow to Select an Element from an Array in PostgreSQL
PostgreSQL provides us with the ability to create arrays, extract element values from them, and more. When it comes to selecting values, there’s a special syntax that we must use in order to get the exact element that we want to select.
Basically, the syntax involves the array or column name, followed by a pair of square brackets with the subscript of the element that we want to select.
Continue reading2 Ways to Insert an Array into a Table in PostgreSQL
PostgreSQL allows us to create columns that store arrays. That is, we can define the column as an array column so that we can insert arrays into the column. This enables us to work with the arrays in the columns without it being confused with plain text.
We have a couple of options for inserting arrays into columns. One is to use an array literal. Another is to use an array constructor. Examples of each of these options are below.
Continue reading4 Ways to Prepend an Element to an Array in PostgreSQL
When it comes to prepending a values to arrays in PostgreSQL, we have a number of options available to us. Below are four methods we can use to prepend a value to an array in PostgreSQL.
Continue readingList of Array Functions in PostgreSQL
Below is an alphabetical list of functions that we can use when working with arrays in PostgreSQL.
Continue readingHow to Create an Array Column in PostgreSQL
PostgreSQL allows us to create arrays and store them in a database column. When we do this, we can use various array related tools to retrieve data from such arrays, as well as manipulate the data within them.
We do need to define the column as an array column though. If we don’t do this, we will likely run into trouble when we want to retrieve data from the array. For example, we can’t just store an array as the text type and then expect to be able to use subscripts to refer to its individual elements.
Continue reading