-- Window functions appear between the key words SELECT and FROMSELECT...,<window_function>,...FROM<tablename>...;-- They consist of three main parts:-- 1. function type (which is the name of the function)-- 2. key word 'OVER'-- 3. specification, which rows constitute the 'sliding window' (partition, order and frame)<window_function> :=<window_function_type>OVER<window_specification><window_function_type> := ROW_NUMBER()| RANK()| LEAD(<column>)| LAG(<column>)|
FIRST_VALUE(<column>)| LAST_VALUE(<column>)| NTH_VALUE(<column>,<n>)|SUM(<column>)|MIN(<column>)|MAX(<column>)|AVG(<column>|COUNT(<column>)<window_specification> :=[<window_partition>][<window_order>][<window_frame>]<window_partition> :=PARTITIONBY<column><window_order> :=ORDERBY<column><window_frame> := see below
<window_frame> :=[ROWS| GROUPS | RANGE ]BETWEEN[UNBOUNDEDPRECEDING|<n>PRECEDING|CURRENTROW]AND[UNBOUNDEDFOLLOWING|<n>FOLLOWING|CURRENTROW]Rowsin a partitionand the according key words
-<-- UNBOUNDED PRECEDING (first row)...-<-- 2 PRECEDING-<-- 1 PRECEDING-<-- CURRENT ROW-<-- 1 FOLLOWING-<-- 2 FOLLOWING...-<-- UNBOUNDED FOLLOWING (last row)