Aggregate Functions

Description

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

  1. aggregate_function(input1 [, input2, ...]) FILTER (WHERE boolean_expression)

Parameters

  • aggregate_function

    Please refer to the Built-in Aggregation Functions document for a complete list of Spark aggregate functions.

  • 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

Please refer to the Built-in Aggregation Functions document for all the examples of Spark aggregate functions.

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

  1. { 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

  1. CREATE OR REPLACE TEMPORARY VIEW basic_pays AS SELECT * FROM VALUES
  2. ('Diane Murphy','Accounting',8435),
  3. ('Mary Patterson','Accounting',9998),
  4. ('Jeff Firrelli','Accounting',8992),
  5. ('William Patterson','Accounting',8870),
  6. ('Gerard Bondur','Accounting',11472),
  7. ('Anthony Bow','Accounting',6627),
  8. ('Leslie Jennings','IT',8113),
  9. ('Leslie Thompson','IT',5186),
  10. ('Julie Firrelli','Sales',9181),
  11. ('Steve Patterson','Sales',9441),
  12. ('Foon Yue Tseng','Sales',6660),
  13. ('George Vanauf','Sales',10563),
  14. ('Loui Bondur','SCM',10449),
  15. ('Gerard Hernandez','SCM',6949),
  16. ('Pamela Castillo','SCM',11303),
  17. ('Larry Bott','SCM',11798),
  18. ('Barry Jones','SCM',10586)
  19. AS basic_pays(employee_name, department, salary);
  20. SELECT * FROM basic_pays;
  21. +-----------------+----------+------+
  22. | employee_name|department|salary|
  23. +-----------------+----------+------+
  24. | Anthony Bow|Accounting| 6627|
  25. | Barry Jones| SCM| 10586|
  26. | Diane Murphy|Accounting| 8435|
  27. | Foon Yue Tseng| Sales| 6660|
  28. | George Vanauf| Sales| 10563|
  29. | Gerard Bondur|Accounting| 11472|
  30. | Gerard Hernandez| SCM| 6949|
  31. | Jeff Firrelli|Accounting| 8992|
  32. | Julie Firrelli| Sales| 9181|
  33. | Larry Bott| SCM| 11798|
  34. | Leslie Jennings| IT| 8113|
  35. | Leslie Thompson| IT| 5186|
  36. | Loui Bondur| SCM| 10449|
  37. | Mary Patterson|Accounting| 9998|
  38. | Pamela Castillo| SCM| 11303|
  39. | Steve Patterson| Sales| 9441|
  40. |William Patterson|Accounting| 8870|
  41. +-----------------+----------+------+
  42. SELECT
  43. department,
  44. percentile_cont(0.25) WITHIN GROUP (ORDER BY salary) AS pc1,
  45. percentile_cont(0.25) WITHIN GROUP (ORDER BY salary) FILTER (WHERE employee_name LIKE '%Bo%') AS pc2,
  46. percentile_cont(0.25) WITHIN GROUP (ORDER BY salary DESC) AS pc3,
  47. percentile_cont(0.25) WITHIN GROUP (ORDER BY salary DESC) FILTER (WHERE employee_name LIKE '%Bo%') AS pc4,
  48. percentile_disc(0.25) WITHIN GROUP (ORDER BY salary) AS pd1,
  49. percentile_disc(0.25) WITHIN GROUP (ORDER BY salary) FILTER (WHERE employee_name LIKE '%Bo%') AS pd2,
  50. percentile_disc(0.25) WITHIN GROUP (ORDER BY salary DESC) AS pd3,
  51. percentile_disc(0.25) WITHIN GROUP (ORDER BY salary DESC) FILTER (WHERE employee_name LIKE '%Bo%') AS pd4
  52. FROM basic_pays
  53. GROUP BY department
  54. ORDER BY department;
  55. +----------+-------+--------+-------+--------+-----+-----+-----+-----+
  56. |department| pc1| pc2| pc3| pc4| pd1| pd2| pd3| pd4|
  57. +----------+-------+--------+-------+--------+-----+-----+-----+-----+
  58. |Accounting|8543.75| 7838.25| 9746.5|10260.75| 8435| 6627| 9998|11472|
  59. | IT|5917.75| NULL|7381.25| NULL| 5186| NULL| 8113| NULL|
  60. | Sales|8550.75| NULL| 9721.5| NULL| 6660| NULL|10563| NULL|
  61. | SCM|10449.0|10786.25|11303.0|11460.75|10449|10449|11303|11798|
  62. +----------+-------+--------+-------+--------+-----+-----+-----+-----+