![]() |
VOOZH | about |
The PostgreSQL ARRAY_AGG function is a aggregate function that allows users to combine values from multiple rows into an array. This function is particularly useful when managing grouped data and returning multiple values in a single row.
In this article, We will learn about the What is PostgreSQL ARRAY_AGG by understanding various examples and so on.
ARRAY_AGG?ARRAY_AGG function in PostgreSQL is an aggregate function that combines values from multiple rows into an array. Syntax of ARRAY_AGG:
The basic syntax of the ARRAY_AGG functions is as follows:
ARRAY_AGG(expression [ORDER BY sorting_expression])Explanation:
ARRAY_AGGSuppose we have a table called employees with the following structure:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50)
);
INSERT INTO employees (name, department) VALUES
('Alice', 'HR'),
('Bob', 'IT'),
('Charlie', 'HR'),
('David', 'Finance'),
('Eve', 'IT');
Now, we want to group the employees by their department and return an array of employee names for each department.
SELECT department, ARRAY_AGG(name) AS employees
FROM employees
GROUP BY department;
Output:
| department | employees |
|---|---|
| HR | {Alice, Charlie} |
| IT | {Bob, Eve} |
| Finance | {David} |
Explanation:
ARRAY_AGG function aggregates the employee names for each department into an array.GROUP BY clause ensures that names are grouped by department.ORDER BY with ARRAY_AGGWe can also specify the order in which values should appear in the array by using the ORDER BY clause.
SELECT department, ARRAY_AGG(name ORDER BY name DESC) AS employees
FROM employees
GROUP BY department;
Output:
| department | employees |
|---|---|
| HR | {Charlie, Alice} |
| IT | {Eve, Bob} |
| Finance | {David} |
Explanation:
ORDER BY name DESC orders the employee names in descending order within the array.Let’s consider a sales table where we want to aggregate sales amounts into an array for each product.
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
product_name VARCHAR(100),
amount INTEGER
);
INSERT INTO sales (product_name, amount) VALUES
('Product A', 200),
('Product A', 150),
('Product B', 300),
('Product B', 250),
('Product C', 100);
Now, aggregate the sales amounts for each product using ARRAY_AGG.
SELECT product_name, ARRAY_AGG(amount) AS sales_amounts
FROM sales
GROUP BY product_name;
Output:
| product_name | sales_amounts |
|---|---|
| Product A | {200, 150} |
| Product B | {300, 250} |
| Product C | {100} |
Explanation:
DISTINCT with ARRAY_AGGSELECT product_name, ARRAY_AGG(DISTINCT amount) AS unique_sales
FROM sales
GROUP BY product_name;
Output:
| product_name | unique_sales |
|---|---|
| Product A | {200, 150} |
| Product B | {300, 250} |
| Product C | {100} |
Explanation:
DISTINCT keyword ensures that duplicate sales amounts are excluded from the array.ARRAY_AGGARRAY_AGG simplifies the task of aggregating multiple values from rows into a single array, which is ideal for reporting and analytics.ARRAY_AGG to efficiently manage and retrieve grouped data with minimal effort.ORDER BY clause allows for controlling the order of elements in the resulting array.ARRAY_AGG() in PostgreSQL is an aggregate function that collects a set of values into an array and returns that array as a result.ARRAY_AGG() effectively handles NULL values in aggregated columns, ensuring comprehensive data aggregation across rows.ARRAY_AGG() function aggregates values specified by the expression into an array. Optional ORDER BY clause allows sorting of elements within the array based on specified criteria.Overall, the PostgreSQL ARRAY_AGG function is an essential tool for aggregating data into arrays, especially when dealing with grouped datasets. Its ability to aggregate values efficiently, work with different data types, and allow for ordered data using the ORDER BY clause makes it a powerful asset for anyone working with PostgreSQL.