Function that is specified in front of the definition of a window in an ABAP SQL window expression and evaluates the rows of the window, is known as window function. Such a function performs analytic operations over a set of input table rows that are somehow related to the current row and help us to solve complex query challenges in easy ways. Window functions are aimed for writing a single query which shows grouped information AND individual information in a query. You might say, it is also possible to use the GROUP BY clause to show grouped information. But the GROUP BY query has its limitation, in that it won’t show you individual data as well.
The addition OVER designates a function as a window function. If you’d like to narrow the window from the entire dataset to individual groups within the dataset, you can use PARTITION BY to do so. You can use the ORDER BY clause as well in defining a window function. It simply orders by the designated column(s) the same way the ORDER BY clause would, except that it treats every partition as separate. The ORDER and PARTITION define what is referred to as the “window”, the ordered subset of data over which calculations are made. Detailed explanation on the windowing syntax and related additions can be found in this blog about Window Expressions in ABAP SQL.
Window functions can be divided in three main groups:
◉ Aggregate functions like AVG( … ), MAX( sql_exp ), MIN( sql_exp ), SUM( sql_exp ) …, COUNT(*) that can also be used in aggregate expressions.
◉ Ranking functions like ROW_NUMBER( ), RANK( ), DENSE_RANK( ), and NTILE (n) that can only be used in window expressions.
◉ Value functions LEAD( … ), LAG( … ), FIRST_VALUE ( … ), LAST_VALUE ( … ) that can be used only in window expressions.
Many of these functions are already known to the ABAP community. From release 7.54 ABAP SQL supports the following new window functions in window expressions as well:
LEAD and LAG
The window functions LEAD or LAG are suitable for calculations, such as determining the difference between values in the current row and values of the preceding or following rows.
These two functions can only be specified together with ORDER BY after OVER with the following syntax
LEAD|LAG( sql_exp1[, diff[, sql_exp2]] )
The result of the functions is the value of the SQL expression sql_exp1 for the row of the current window defined by the addition diff or the box defined by the addition ORDER BY after OVER.
◉ For the function LEAD, diff determines the row positioned the corresponding distance after the current row.
◉ For the function LAG, diff determines the row positioned the corresponding distance in front of the current row.
If diff is not specified, the value 1 is used implicitly. If the row determined by diff is not in the current window, the result is the null value by default. If the optional SQL expression sql_exp2 is specified, it is evaluated and returned for the current row in cases where the row does not exist.
Example
SELECT statement with the window functions LEAD and LAG as operands of an arithmetic expression. The addition PARTITION is not specified, which means there is only one window with all rows of the result set. Both LEAD and LAG have only one argument each, which means that the difference between the values of the column NUM1 is calculated using the directly following or preceding row, and any nonexistent rows produce null values. The latter are defined using a null indicator. The program DEMO_SELECT_OVER_LEAD_LAG_DIFF uses this SELECT statement :
SELECT num1 AS number,
num1 - LEAD( num1 ) OVER( ORDER BY id ) AS diff_lead,
num1 - LAG( num1 ) OVER( ORDER BY id ) AS diff_lag
FROM demo_expressions
ORDER BY id
INTO TABLE @DATA(lead_lag_diffs)
INDICATORS NULL STRUCTURE null_ind.
and, when executed, the program displays the result, as partly shown below.
No comments:
Post a Comment