分析函数(窗口函数)

分析函数介绍

分析函数是一类特殊的内置函数。和聚合函数类似,分析函数也是对于多个输入行做计算得到一个数据值。不同的是,分析函数是在一个特定的窗口内对输入数据做处理,而不是按照group by来分组计算。每个窗口内的数据可以用over()从句进行排序和分组。分析函数会对结果集的每一行计算出一个单独的值,而不是每个group by分组计算一个值。这种灵活的方式允许用户在select从句中增加额外的列,给用户提供了更多的机会来对结果集进行重新组织和过滤。分析函数只能出现在select列表和最外层的order by从句中。在查询过程中,分析函数会在最后生效,就是说,在执行完join,where和group by等操作之后再执行。分析函数在金融和科学计算领域经常被使用到,用来分析趋势、计算离群值以及对大量数据进行分桶分析等。

分析函数的语法:

  1. function(args) OVER(partition_by_clause order_by_clause [window_clause])
  2. partition_by_clause ::= PARTITION BY expr [, expr ...]
  3. order_by_clause ::= ORDER BY expr [ASC | DESC] [, expr [ASC | DESC] ...]

window_clause: 见后面Window Clause)

Function

目前支持的Function包括AVG(), COUNT(), DENSE_RANK(), FIRST_VALUE(), LAG(), LAST_VALUE(), LEAD(), MAX(), MIN(), RANK(), ROW_NUMBER()和SUM()。

PARTITION BY从句

Partition By从句和Group By类似。它把输入行按照指定的一列或多列分组,相同值的行会被分到一组。

ORDER BY从句

Order By从句和外层的Order By基本一致。它定义了输入行的排列顺序,如果指定了Partition By,则Order By定义了每个Partition分组内的顺序。与外层Order By的唯一不同点是,OVER从句中的Order By n(n是正整数)相当于不做任何操作,而外层的Order By n表示按照第n列排序。

举例:

这个例子展示了在select列表中增加一个id列,它的值是1,2,3等等,顺序按照events表中的date_and_time列排序。

  1. SELECT
  2. row_number() OVER (ORDER BY date_and_time) AS id,
  3. c1, c2, c3, c4
  4. FROM events;

Window从句

Window从句用来为分析函数指定一个运算范围,以当前行为准,前后若干行作为分析函数运算的对象。Window从句支持的方法有:AVG(), COUNT(), FIRST_VALUE(), LAST_VALUE()和SUM()。对于 MAX()和MIN(), window从句可以指定开始范围UNBOUNDED PRECEDING

语法:

  1. ROWS BETWEEN [ { m | UNBOUNDED } PRECEDING | CURRENT ROW] [ AND [CURRENT ROW | { UNBOUNDED | n } FOLLOWING] ]

举例:

假设我们有如下的股票数据,股票代码是JDR,closing price是每天的收盘价。

  1. create table stock_ticker (stock_symbol string, closing_price decimal(8,2), closing_date timestamp);
  2. ...load some data...
  3. select * from stock_ticker order by stock_symbol, closing_date
  4. | stock_symbol | closing_price | closing_date |
  5. |--------------|---------------|---------------------|
  6. | JDR | 12.86 | 2014-10-02 00:00:00 |
  7. | JDR | 12.89 | 2014-10-03 00:00:00 |
  8. | JDR | 12.94 | 2014-10-04 00:00:00 |
  9. | JDR | 12.55 | 2014-10-05 00:00:00 |
  10. | JDR | 14.03 | 2014-10-06 00:00:00 |
  11. | JDR | 14.75 | 2014-10-07 00:00:00 |
  12. | JDR | 13.98 | 2014-10-08 00:00:00 |

这个查询使用分析函数产生moving_average这一列,它的值是3天的股票均价,即前一天、当前以及后一天三天的均价。第一天没有前一天的值,最后一天没有后一天的值,所以这两行只计算了两天的均值。这里Partition By没有起到作用,因为所有的数据都是JDR的数据,但如果还有其他股票信息,Partition By会保证分析函数值作用在本Partition之内。

  1. select stock_symbol, closing_date, closing_price,
  2. avg(closing_price) over (partition by stock_symbol order by closing_date
  3. rows between 1 preceding and 1 following) as moving_average
  4. from stock_ticker;
  5. | stock_symbol | closing_date | closing_price | moving_average |
  6. |--------------|---------------------|---------------|----------------|
  7. | JDR | 2014-10-02 00:00:00 | 12.86 | 12.87 |
  8. | JDR | 2014-10-03 00:00:00 | 12.89 | 12.89 |
  9. | JDR | 2014-10-04 00:00:00 | 12.94 | 12.79 |
  10. | JDR | 2014-10-05 00:00:00 | 12.55 | 13.17 |
  11. | JDR | 2014-10-06 00:00:00 | 14.03 | 13.77 |
  12. | JDR | 2014-10-07 00:00:00 | 14.75 | 14.25 |
  13. | JDR | 2014-10-08 00:00:00 | 13.98 | 14.36 |

Function使用举例

本节介绍Palo中可以用作分析函数的方法。

AVG()

语法:

AVG([DISTINCT | ALL] expression) [OVER (analytic_clause)]

举例:

计算当前行和它前后各一行数据的x平均值

  1. select x, property,
  2. avg(x) over
  3. (
  4. partition by property
  5. order by x
  6. rows between 1 preceding and 1 following
  7. ) as 'moving average'
  8. from int_t where property in ('odd','even');
  9. | x | property | moving average |
  10. |----|----------|----------------|
  11. | 2 | even | 3 |
  12. | 4 | even | 4 |
  13. | 6 | even | 6 |
  14. | 8 | even | 8 |
  15. | 10 | even | 9 |
  16. | 1 | odd | 2 |
  17. | 3 | odd | 3 |
  18. | 5 | odd | 5 |
  19. | 7 | odd | 7 |
  20. | 9 | odd | 8 |
COUNT()

语法:

  1. COUNT([DISTINCT | ALL] expression) [OVER (analytic_clause)]

举例:

计算从当前行到第一行x出现的次数。

  1. select x, property,
  2. count(x) over
  3. (
  4. partition by property
  5. order by x
  6. rows between unbounded preceding and current row
  7. ) as 'cumulative total'
  8. from int_t where property in ('odd','even');
  9. | x | property | cumulative count |
  10. |----|----------|------------------|
  11. | 2 | even | 1 |
  12. | 4 | even | 2 |
  13. | 6 | even | 3 |
  14. | 8 | even | 4 |
  15. | 10 | even | 5 |
  16. | 1 | odd | 1 |
  17. | 3 | odd | 2 |
  18. | 5 | odd | 3 |
  19. | 7 | odd | 4 |
  20. | 9 | odd | 5 |
DENSE_RANK()

DENSE_RANK()函数用来表示排名,与RANK()不同的是,DENSE_RANK()不会出现空缺数字。比如,如果出现了两个并列的1,DENSE_RANK()的第三个数仍然是2,而RANK()的第三个数是3。

语法:

  1. DENSE_RANK() OVER(partition_by_clause order_by_clause)

举例:

下例展示了按照property列分组对x列排名:

  1. select x, y, dense_rank() over(partition by x order by y) as rank from int_t;
  2. | x | y | rank |
  3. |----|------|----------|
  4. | 1 | 1 | 1 |
  5. | 1 | 2 | 2 |
  6. | 1 | 2 | 2 |
  7. | 2 | 1 | 1 |
  8. | 2 | 2 | 2 |
  9. | 2 | 3 | 3 |
  10. | 3 | 1 | 1 |
  11. | 3 | 1 | 1 |
  12. | 3 | 2 | 2 |
FIRST_VALUE()

FIRST_VALUE()返回窗口范围内的第一个值。

语法:

  1. FIRST_VALUE(expr) OVER(partition_by_clause order_by_clause [window_clause])

举例:

我们有如下数据

  1. select name, country, greeting from mail_merge;
  2. | name | country | greeting |
  3. |---------|---------|--------------|
  4. | Pete | USA | Hello |
  5. | John | USA | Hi |
  6. | Boris | Germany | Guten tag |
  7. | Michael | Germany | Guten morgen |
  8. | Bjorn | Sweden | Hej |
  9. | Mats | Sweden | Tja |

使用FIRST_VALUE(),根据country分组,返回每个分组中第一个greeting的值:

  1. select country, name,
  2. first_value(greeting)
  3. over (partition by country order by name, greeting) as greeting from mail_merge;
  4. | country | name | greeting |
  5. |---------|---------|-----------|
  6. | Germany | Boris | Guten tag |
  7. | Germany | Michael | Guten tag |
  8. | Sweden | Bjorn | Hej |
  9. | Sweden | Mats | Hej |
  10. | USA | John | Hi |
  11. | USA | Pete | Hi |
LAG()

LAG()方法用来计算当前行向前数若干行的值。

语法:

  1. LAG (expr, offset, default) OVER (partition_by_clause order_by_clause)

举例:

计算前一天的收盘价

  1. select stock_symbol, closing_date, closing_price,
  2. lag(closing_price,1, 0) over (partition by stock_symbol order by closing_date) as "yesterday closing"
  3. from stock_ticker
  4. order by closing_date;
  5. | stock_symbol | closing_date | closing_price | yesterday closing |
  6. |--------------|---------------------|---------------|-------------------|
  7. | JDR | 2014-09-13 00:00:00 | 12.86 | 0 |
  8. | JDR | 2014-09-14 00:00:00 | 12.89 | 12.86 |
  9. | JDR | 2014-09-15 00:00:00 | 12.94 | 12.89 |
  10. | JDR | 2014-09-16 00:00:00 | 12.55 | 12.94 |
  11. | JDR | 2014-09-17 00:00:00 | 14.03 | 12.55 |
  12. | JDR | 2014-09-18 00:00:00 | 14.75 | 14.03 |
  13. | JDR | 2014-09-19 00:00:00 | 13.98 | 14.75
LAST_VALUE()

LAST_VALUE()返回窗口范围内的最后一个值。与FIRST_VALUE()相反。

语法:

  1. LAST_VALUE(expr) OVER(partition_by_clause order_by_clause [window_clause])

使用FIRST_VALUE()举例中的数据:

  1. select country, name,
  2. last_value(greeting)
  3. over (partition by country order by name, greeting) as greeting
  4. from mail_merge;
  5. | country | name | greeting |
  6. |---------|---------|--------------|
  7. | Germany | Boris | Guten morgen |
  8. | Germany | Michael | Guten morgen |
  9. | Sweden | Bjorn | Tja |
  10. | Sweden | Mats | Tja |
  11. | USA | John | Hello |
  12. | USA | Pete | Hello
LEAD()

LEAD()方法用来计算当前行向后数若干行的值。

语法:

  1. LEAD (expr, offset, default]) OVER (partition_by_clause order_by_clause)

举例:

计算第二天的收盘价对比当天收盘价的走势,即第二天收盘价比当天高还是低。

  1. select stock_symbol, closing_date, closing_price,
  2. case
  3. (lead(closing_price,1, 0)
  4. over (partition by stock_symbol order by closing_date)-closing_price) > 0
  5. when true then "higher"
  6. when false then "flat or lower"
  7. end as "trending"
  8. from stock_ticker
  9. order by closing_date;
  10. | stock_symbol | closing_date | closing_price | trending |
  11. |--------------|---------------------|---------------|---------------|
  12. | JDR | 2014-09-13 00:00:00 | 12.86 | higher |
  13. | JDR | 2014-09-14 00:00:00 | 12.89 | higher |
  14. | JDR | 2014-09-15 00:00:00 | 12.94 | flat or lower |
  15. | JDR | 2014-09-16 00:00:00 | 12.55 | higher |
  16. | JDR | 2014-09-17 00:00:00 | 14.03 | higher |
  17. | JDR | 2014-09-18 00:00:00 | 14.75 | flat or lower |
  18. | JDR | 2014-09-19 00:00:00 | 13.98 | flat or lower |
MAX()

语法:

  1. MAX([DISTINCT | ALL] expression) [OVER (analytic_clause)]

举例:

计算从第一行到当前行之后一行的最大值

  1. select x, property,
  2. max(x) over
  3. (
  4. order by property, x
  5. rows between unbounded preceding and 1 following
  6. ) as 'local maximum'
  7. from int_t where property in ('prime','square');
  8. | x | property | local maximum |
  9. |---|----------|---------------|
  10. | 2 | prime | 3 |
  11. | 3 | prime | 5 |
  12. | 5 | prime | 7 |
  13. | 7 | prime | 7 |
  14. | 1 | square | 7 |
  15. | 4 | square | 9 |
  16. | 9 | square | 9 |
MIN()

语法:

  1. MIN([DISTINCT | ALL] expression) [OVER (analytic_clause)]

举例:

计算从第一行到当前行之后一行的最小值

  1. select x, property,
  2. min(x) over
  3. (
  4. order by property, x desc
  5. rows between unbounded preceding and 1 following
  6. ) as 'local minimum'
  7. from int_t where property in ('prime','square');
  8. | x | property | local minimum |
  9. |---|----------|---------------|
  10. | 7 | prime | 5 |
  11. | 5 | prime | 3 |
  12. | 3 | prime | 2 |
  13. | 2 | prime | 2 |
  14. | 9 | square | 2 |
  15. | 4 | square | 1 |
  16. | 1 | square | 1 |
RANK()

RANK()函数用来表示排名,与DENSE_RANK()不同的是,RANK()会出现空缺数字。比如,如果出现了两个并列的1, RANK()的第三个数就是3,而不是2。

语法:

  1. RANK() OVER(partition_by_clause order_by_clause)

举例:

根据x列进行排名

  1. select x, y, rank() over(partition by x order by y) as rank from int_t;
  2. | x | y | rank |
  3. |----|------|----------|
  4. | 1 | 1 | 1 |
  5. | 1 | 2 | 2 |
  6. | 1 | 2 | 2 |
  7. | 2 | 1 | 1 |
  8. | 2 | 2 | 2 |
  9. | 2 | 3 | 3 |
  10. | 3 | 1 | 1 |
  11. | 3 | 1 | 1 |
  12. | 3 | 2 | 3 |
ROW_NUMBER()

为每个Partition的每一行返回一个从1开始连续递增的整数。与RANK()和DENSE_RANK()不同的是,ROW_NUMBER()返回的值不会重复也不会出现空缺,是连续递增的。

语法:

  1. ROW_NUMBER() OVER(partition_by_clause order_by_clause)

举例:

  1. select x, y, row_number() over(partition by x order by y) as rank from int_t;
  2. | x | y | rank |
  3. |---|------|----------|
  4. | 1 | 1 | 1 |
  5. | 1 | 2 | 2 |
  6. | 1 | 2 | 3 |
  7. | 2 | 1 | 1 |
  8. | 2 | 2 | 2 |
  9. | 2 | 3 | 3 |
  10. | 3 | 1 | 1 |
  11. | 3 | 1 | 2 |
  12. | 3 | 2 | 3 |
SUM()

语法:

  1. SUM([DISTINCT | ALL] expression) [OVER (analytic_clause)]

举例:

按照property进行分组,在组内计算当前行以及前后各一行的x列的和。

  1. select x, property,
  2. sum(x) over
  3. (
  4. partition by property
  5. order by x
  6. rows between 1 preceding and 1 following
  7. ) as 'moving total'
  8. from int_t where property in ('odd','even');
  9. | x | property | moving total |
  10. |----|----------|--------------|
  11. | 2 | even | 6 |
  12. | 4 | even | 12 |
  13. | 6 | even | 18 |
  14. | 8 | even | 24 |
  15. | 10 | even | 18 |
  16. | 1 | odd | 4 |
  17. | 3 | odd | 9 |
  18. | 5 | odd | 15 |
  19. | 7 | odd | 21 |
  20. | 9 | odd | 16 |