Aggregate functions
Note
To see which AWS data source integrations support this SQL command, see Supported OpenSearch SQL commands and functions.
Aggregate functions operate on values across rows to perform mathematical calculations such as sum, average, counting, minimum/maximum values, standard deviation, and estimation, as well as some non-mathematical operations.
Syntax
aggregate_function(input1 [, input2, ...]) FILTER (WHERE boolean_expression)
Parameters
-
boolean_expression
- Specifies any expression that evaluates to a result type boolean. Two or more expressions may be combined together using the logical operators ( AND, OR ).
Ordered-set aggregate functions
These aggregate functions use different syntax than the other aggregate functions so that to specify an expression (typically a column name) by which to order the values.
Syntax
{ PERCENTILE_CONT | PERCENTILE_DISC }(percentile) WITHIN GROUP (ORDER BY { order_by_expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [ , ... ] }) FILTER (WHERE boolean_expression)
Parameters
-
percentile
- The percentile of the value that you want to find. The percentile must be a constant between 0.0 and 1.0. -
order_by_expression
- The expression (typically a column name) by which to order the values before aggregating them. -
boolean_expression
- Specifies any expression that evaluates to a result type boolean. Two or more expressions may be combined together using the logical operators ( AND, OR ).
Examples
CREATE OR REPLACE TEMPORARY VIEW basic_pays AS SELECT * FROM VALUES ('Jane Doe','Accounting',8435), ('Akua Mansa','Accounting',9998), ('John Doe','Accounting',8992), ('Juan Li','Accounting',8870), ('Carlos Salazar','Accounting',11472), ('Arnav Desai','Accounting',6627), ('Saanvi Sarkar','IT',8113), ('Shirley Rodriguez','IT',5186), ('Nikki Wolf','Sales',9181), ('Alejandro Rosalez','Sales',9441), ('Nikhil Jayashankar','Sales',6660), ('Richard Roe','Sales',10563), ('Pat Candella','SCM',10449), ('Gerard Hernandez','SCM',6949), ('Pamela Castillo','SCM',11303), ('Paulo Santos','SCM',11798), ('Jorge Souza','SCM',10586) AS basic_pays(employee_name, department, salary); SELECT * FROM basic_pays; +-------------------+----------+------+ | employee_name |department|salary| +-------------------+----------+------+ | Arnav Desai |Accounting| 6627| | Jorge Souza | SCM| 10586| | Jane Doe |Accounting| 8435| | Nikhil Jayashankar| Sales| 6660| | Diego Vanauf | Sales| 10563| | Carlos Salazar |Accounting| 11472| | Gerard Hernandez | SCM| 6949| | John Doe |Accounting| 8992| | Nikki Wolf | Sales| 9181| | Paulo Santos | SCM| 11798| | Saanvi Sarkar | IT| 8113| | Shirley Rodriguez | IT| 5186| | Pat Candella | SCM| 10449| | Akua Mansa |Accounting| 9998| | Pamela Castillo | SCM| 11303| | Alejandro Rosalez | Sales| 9441| | Juan Li |Accounting| 8870| +-------------------+----------+------+ SELECT department, percentile_cont(0.25) WITHIN GROUP (ORDER BY salary) AS pc1, percentile_cont(0.25) WITHIN GROUP (ORDER BY salary) FILTER (WHERE employee_name LIKE '%Bo%') AS pc2, percentile_cont(0.25) WITHIN GROUP (ORDER BY salary DESC) AS pc3, percentile_cont(0.25) WITHIN GROUP (ORDER BY salary DESC) FILTER (WHERE employee_name LIKE '%Bo%') AS pc4, percentile_disc(0.25) WITHIN GROUP (ORDER BY salary) AS pd1, percentile_disc(0.25) WITHIN GROUP (ORDER BY salary) FILTER (WHERE employee_name LIKE '%Bo%') AS pd2, percentile_disc(0.25) WITHIN GROUP (ORDER BY salary DESC) AS pd3, percentile_disc(0.25) WITHIN GROUP (ORDER BY salary DESC) FILTER (WHERE employee_name LIKE '%Bo%') AS pd4 FROM basic_pays GROUP BY department ORDER BY department; +----------+-------+--------+-------+--------+-----+-----+-----+-----+ |department| pc1| pc2| pc3| pc4| pd1| pd2| pd3| pd4| +----------+-------+--------+-------+--------+-----+-----+-----+-----+ |Accounting|8543.75| 7838.25| 9746.5|10260.75| 8435| 6627| 9998|11472| | IT|5917.75| NULL|7381.25| NULL| 5186| NULL| 8113| NULL| | Sales|8550.75| NULL| 9721.5| NULL| 6660| NULL|10563| NULL| | SCM|10449.0|10786.25|11303.0|11460.75|10449|10449|11303|11798| +----------+-------+--------+-------+--------+-----+-----+-----+-----+