Netezza SQL Analytic Functions 分析函数

应用场景:

分组排序,分组累加求和...

 

 

基本语法:

Func( value_expression) OVER ( [<partition_by_clause>] [<order_by_clause>] [<frame_spec_clause>] )
<partition_by_clause> = PARTITION BY <value_expression> [, ...]+
<order_by_clause> = ORDER BY <value_expression> [asc | desc] [nulls {first|last}] [, ...]+
<frame_spec_clause> = <frame_extent> [<exclusion clause>]
<frame_extent> = 
       ROWS  UNBOUNDED PRECEDING
      |ROWS  <constant> PRECEDING
      |ROWS   CURRENT ROW
      |RANGE  UNBOUNDED PRECEDING
      |RANGE  <constant> PRECEDING
      |RANGE  CURRENT ROW
      |ROWS BETWEEN {UNBOUNDED PRECEDING| <constant> PRECEDING | CURRENT 
ROW } AND { UNBOUNDED FOLLOWING | <constant>  FOLLOWING | CURRENT ROW }
      |RANGE BETWEEN {UNBOUNDED PRECEDING| <constant> PRECEDING | CURRENT 
ROW } AND { UNBOUNDED FOLLOWING | <constant>  FOLLOWING | CURRENT ROW } 
<exclusion_clause> =  EXCLUDE CURRENT ROW | EXCLUDE TIES | EXCLUDE 
GROUP | EXCLUDE  NO OTHERS

 

根据分析函数中的窗口特点基本可以分为两大类:

 

1,row_number(), lead() , lag()...

该类别over字句中必须有order by,且不能再指定窗口范围(frame_spec_clause

2, max(), count(), sum(), first(), last()...

若将每个partition分组后的所有记录集合按order by字段排序形成队列集合S.

若没有指定order by语句,默认窗口范围为全集S

若指定order by语句,默认窗口范围为从队首[S1-SCURRENT]

若指定order by语句同时也指定窗口范围,则按窗口范围来进行计算

 

参考文章:

http://www-01.ibm.com/support/knowledgecenter/SSULQD_7.2.0/com.ibm.nz.dbu.doc/c_dbuser_window_aggregation_family_syntax.html?lang=en

http://www.cnblogs.com/CareySon/p/3411176.html

http://sqlmag.com/sql-server-2012/how-use-microsoft-sql-server-2012s-window-functions-part-1

 

你可能感兴趣的:(Netezza SQL Analytic Functions 分析函数)