LAG function give access to multiple rows within a table, without the need for a self-join
(The LEAD function is used to return data from the next row, syntax and usage is same as LAG)
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 preceeding/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.
Example:
SELECT A.GP_PAYGROUP, LAG(A.GP_PAYGROUP,1,0) OVER (PARTITION BY A.EMPLID,A.EMPL_RCD ORDER BY A.EFFDT, A.EFFSEQ) AS PREV_GP_PAYGROUP, A.EMPLID,A.EMPL_RCD, A.EFFDT , A.EFFSEQ
FROM PS_JOB A ORDER BY A.EMPLID,A.EMPL_RCD, A.EFFDT DESC, A.EFFSEQ;
No comments:
Post a Comment