Window Functions

Description

Window functions operate on a group of rows, referred to as a window, and calculate a return value for each row based on the group of rows. Window functions are useful for processing tasks such as calculating a moving average, computing a cumulative statistic, or accessing the value of rows given the relative position of the current row.

Syntax

  1. window_function [ nulls_option ] OVER
  2. ( [ { PARTITION | DISTRIBUTE } BY partition_col_name = partition_col_val ( [ , ... ] ) ]
  3. { ORDER | SORT } BY expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [ , ... ]
  4. [ window_frame ] )

Parameters

  • window_function

    • Ranking Functions

      Syntax: RANK | DENSE_RANK | PERCENT_RANK | NTILE | ROW_NUMBER

    • Analytic Functions

      Syntax: CUME_DIST | LAG | LEAD | NTH_VALUE | FIRST_VALUE | LAST_VALUE

    • Aggregate Functions

      Syntax: MAX | MIN | COUNT | SUM | AVG | ...

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

  • nulls_option

    Specifies whether or not to skip null values when evaluating the window function. RESPECT NULLS means not skipping null values, while IGNORE NULLS means skipping. If not specified, the default is RESPECT NULLS.

    Syntax:

    { IGNORE | RESPECT } NULLS

    Note: Only LAG | LEAD | NTH_VALUE | FIRST_VALUE | LAST_VALUE can be used with IGNORE NULLS.

  • window_frame

    Specifies which row to start the window on and where to end it.

    Syntax:

    { RANGE | ROWS } { frame_start | BETWEEN frame_start AND frame_end }

    • frame_start and frame_end have the following syntax:

      Syntax:

      UNBOUNDED PRECEDING | offset PRECEDING | CURRENT ROW | offset FOLLOWING | UNBOUNDED FOLLOWING

      offset: specifies the offset from the position of the current row.

    Note: If frame_end is omitted it defaults to CURRENT ROW.

Examples

  1. CREATE TABLE employees (name STRING, dept STRING, salary INT, age INT);
  2. INSERT INTO employees VALUES ("Lisa", "Sales", 10000, 35);
  3. INSERT INTO employees VALUES ("Evan", "Sales", 32000, 38);
  4. INSERT INTO employees VALUES ("Fred", "Engineering", 21000, 28);
  5. INSERT INTO employees VALUES ("Alex", "Sales", 30000, 33);
  6. INSERT INTO employees VALUES ("Tom", "Engineering", 23000, 33);
  7. INSERT INTO employees VALUES ("Jane", "Marketing", 29000, 28);
  8. INSERT INTO employees VALUES ("Jeff", "Marketing", 35000, 38);
  9. INSERT INTO employees VALUES ("Paul", "Engineering", 29000, 23);
  10. INSERT INTO employees VALUES ("Chloe", "Engineering", 23000, 25);
  11. SELECT * FROM employees;
  12. +-----+-----------+------+-----+
  13. | name| dept|salary| age|
  14. +-----+-----------+------+-----+
  15. |Chloe|Engineering| 23000| 25|
  16. | Fred|Engineering| 21000| 28|
  17. | Paul|Engineering| 29000| 23|
  18. |Helen| Marketing| 29000| 40|
  19. | Tom|Engineering| 23000| 33|
  20. | Jane| Marketing| 29000| 28|
  21. | Jeff| Marketing| 35000| 38|
  22. | Evan| Sales| 32000| 38|
  23. | Lisa| Sales| 10000| 35|
  24. | Alex| Sales| 30000| 33|
  25. +-----+-----------+------+-----+
  26. SELECT name, dept, salary, RANK() OVER (PARTITION BY dept ORDER BY salary) AS rank FROM employees;
  27. +-----+-----------+------+----+
  28. | name| dept|salary|rank|
  29. +-----+-----------+------+----+
  30. | Lisa| Sales| 10000| 1|
  31. | Alex| Sales| 30000| 2|
  32. | Evan| Sales| 32000| 3|
  33. | Fred|Engineering| 21000| 1|
  34. | Tom|Engineering| 23000| 2|
  35. |Chloe|Engineering| 23000| 2|
  36. | Paul|Engineering| 29000| 4|
  37. |Helen| Marketing| 29000| 1|
  38. | Jane| Marketing| 29000| 1|
  39. | Jeff| Marketing| 35000| 3|
  40. +-----+-----------+------+----+
  41. SELECT name, dept, salary, DENSE_RANK() OVER (PARTITION BY dept ORDER BY salary ROWS BETWEEN
  42. UNBOUNDED PRECEDING AND CURRENT ROW) AS dense_rank FROM employees;
  43. +-----+-----------+------+----------+
  44. | name| dept|salary|dense_rank|
  45. +-----+-----------+------+----------+
  46. | Lisa| Sales| 10000| 1|
  47. | Alex| Sales| 30000| 2|
  48. | Evan| Sales| 32000| 3|
  49. | Fred|Engineering| 21000| 1|
  50. | Tom|Engineering| 23000| 2|
  51. |Chloe|Engineering| 23000| 2|
  52. | Paul|Engineering| 29000| 3|
  53. |Helen| Marketing| 29000| 1|
  54. | Jane| Marketing| 29000| 1|
  55. | Jeff| Marketing| 35000| 2|
  56. +-----+-----------+------+----------+
  57. SELECT name, dept, age, CUME_DIST() OVER (PARTITION BY dept ORDER BY age
  58. RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cume_dist FROM employees;
  59. +-----+-----------+------+------------------+
  60. | name| dept|age | cume_dist|
  61. +-----+-----------+------+------------------+
  62. | Alex| Sales| 33|0.3333333333333333|
  63. | Lisa| Sales| 35|0.6666666666666666|
  64. | Evan| Sales| 38| 1.0|
  65. | Paul|Engineering| 23| 0.25|
  66. |Chloe|Engineering| 25| 0.75|
  67. | Fred|Engineering| 28| 0.25|
  68. | Tom|Engineering| 33| 1.0|
  69. | Jane| Marketing| 28|0.3333333333333333|
  70. | Jeff| Marketing| 38|0.6666666666666666|
  71. |Helen| Marketing| 40| 1.0|
  72. +-----+-----------+------+------------------+
  73. SELECT name, dept, salary, MIN(salary) OVER (PARTITION BY dept ORDER BY salary) AS min
  74. FROM employees;
  75. +-----+-----------+------+-----+
  76. | name| dept|salary| min|
  77. +-----+-----------+------+-----+
  78. | Lisa| Sales| 10000|10000|
  79. | Alex| Sales| 30000|10000|
  80. | Evan| Sales| 32000|10000|
  81. |Helen| Marketing| 29000|29000|
  82. | Jane| Marketing| 29000|29000|
  83. | Jeff| Marketing| 35000|29000|
  84. | Fred|Engineering| 21000|21000|
  85. | Tom|Engineering| 23000|21000|
  86. |Chloe|Engineering| 23000|21000|
  87. | Paul|Engineering| 29000|21000|
  88. +-----+-----------+------+-----+
  89. SELECT name, salary,
  90. LAG(salary) OVER (PARTITION BY dept ORDER BY salary) AS lag,
  91. LEAD(salary, 1, 0) OVER (PARTITION BY dept ORDER BY salary) AS lead
  92. FROM employees;
  93. +-----+-----------+------+-----+-----+
  94. | name| dept|salary| lag| lead|
  95. +-----+-----------+------+-----+-----+
  96. | Lisa| Sales| 10000|NULL |30000|
  97. | Alex| Sales| 30000|10000|32000|
  98. | Evan| Sales| 32000|30000| 0|
  99. | Fred|Engineering| 21000| NULL|23000|
  100. |Chloe|Engineering| 23000|21000|23000|
  101. | Tom|Engineering| 23000|23000|29000|
  102. | Paul|Engineering| 29000|23000| 0|
  103. |Helen| Marketing| 29000| NULL|29000|
  104. | Jane| Marketing| 29000|29000|35000|
  105. | Jeff| Marketing| 35000|29000| 0|
  106. +-----+-----------+------+-----+-----+
  107. SELECT id, v,
  108. LEAD(v, 0) IGNORE NULLS OVER w lead,
  109. LAG(v, 0) IGNORE NULLS OVER w lag,
  110. NTH_VALUE(v, 2) IGNORE NULLS OVER w nth_value,
  111. FIRST_VALUE(v) IGNORE NULLS OVER w first_value,
  112. LAST_VALUE(v) IGNORE NULLS OVER w last_value
  113. FROM test_ignore_null
  114. WINDOW w AS (ORDER BY id)
  115. ORDER BY id;
  116. +--+----+----+----+---------+-----------+----------+
  117. |id| v|lead| lag|nth_value|first_value|last_value|
  118. +--+----+----+----+---------+-----------+----------+
  119. | 0|NULL|NULL|NULL| NULL| NULL| NULL|
  120. | 1| x| x| x| NULL| x| x|
  121. | 2|NULL|NULL|NULL| NULL| x| x|
  122. | 3|NULL|NULL|NULL| NULL| x| x|
  123. | 4| y| y| y| y| x| y|
  124. | 5|NULL|NULL|NULL| y| x| y|
  125. | 6| z| z| z| y| x| z|
  126. | 7| v| v| v| y| x| v|
  127. | 8|NULL|NULL|NULL| y| x| v|
  128. +--+----+----+----+---------+-----------+----------+