Showing posts with label Database SQL. Show all posts
Showing posts with label Database SQL. Show all posts

Saturday, July 16, 2016

LAG and LEAD analytical function

LAG is an analytical function that can be used to get the value of a column in a previous row. It provides access to a row at a given offset prior to the current position.

LAG  (value_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause)

value_expression - Can be a column or a built-in function, except for other analytic functions.
offset - The number of rows preceding/following the current row, from which the data is to be retrieved. The default value is 1.
default - The value returned if the offset is outside the scope of the window. The default value is NULL.
query_partition_clause - Optional. It is used to partition the results into groups based on one or more expressions.

order_by_clause - Optional. It is used to order the data within each partition.