Window Expressions in ABAP SQL - SAP Community
Window Expressions in ABAP SQL - SAP Community
SAP Community > Groups > Interest Groups > Application Development
> Blog Posts > Window Expressions in ABAP SQL
former_member763514
Active Participant
11-24-2021 1:26 PM
11 Kudos
6,241 Views
Window expressions were introduced to ABAP SQL with release 7.54. A window
expression uses a window function to determine an individual value from the rows of a
window of the result set of a query. The term window describes the set of rows on which
the function operates. In other words, a window function uses values from the rows in a
window to calculate the returned values.
The window functions of a window expression work with the rows of a virtual table,
which is defined by the specifications after addition OVER ( ... ) where in the
parentheses the window on the result set is defined for whose rows the window function
is evaluated. It is done using the following additions:
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/application-developmentblog-board/article-id/44800 1/8
8/1/24, 6:26 PM Window Expressions in ABAP SQL - SAP Community
ORDER BY
The optional addition ORDER BY introduces both an order and a frame within the
current window, which further restricts the rows for which the window function is
calculated. It is also a prerequisite for certain ranking functions.
The ORDER and PARTITION define what is referred to as the "window", the ordered
subset of data over which calculations are made. The addition PARTITION works in a
similar way as in the GROUP BY clause in an aggregate expression. However, unlike
aggregate expressions, there is no aggregation of the rows defined using PARTITION.
Instead, these are retained and are all assigned the value calculated using the window
expression.
This means, use of a window function does not cause rows to become grouped into a
single output row, the rows retain their separate identities. The addition ORDER BY of
the SELECT statement has no effect on the result of a window expression. Hence, the
rows of the result set can be sorted according to the results of window expressions by
using their alias name.
A window expression can only be specified in the result set in the SELECT list of a
query. It cannot be used like an aggregate expression in the GROUP BY or HAVING
clause. If a window expression is used in the SELECT list of a SELECT statement with
GROUP BY clause, the windows are defined in the merged result list and aggregate
expressions can be used as arguments of the window functions. Each column that is
specified in any position in the window expression must also be specified in the GROUP
BY clause.
window expressions can be used in other SQL expressions as well. However, the
calculations combine values of the current row with the results of window expressions,
for example, the percentage of a column in the current window, or the distance to the
minimum or maximum value of the current window.
Example:
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/application-developmentblog-board/article-id/44800 2/8
8/1/24, 6:26 PM Window Expressions in ABAP SQL - SAP Community
The example demonstrates how different window functions are applied to windows of a
result set of a query defined using PARTITION. The content of the columns CHAR1 and
CHAR2 is used as a window criterion. All rows that have identical content in these
columns form a window. The result of a window function for a row is determined from all
rows of the window this row is a part of. The final column perc demonstrates how a
window function can be used as arguments of an SQL expression and hence produce
row-dependent results, in this case the percentage of the value of the column NUM1 as
part of the entire window.
The window frame specification is an optional addition within the OVER( ... ORDER BY
... ) clause. It allows the definition of a subset of rows inside a window, which is also
referred to as a frame. Frames are determined with respect to the current row, which
enables the frame to move within a window. Some of these optional frames are as
follows.
The definition of the window, the processing sequence, and the frame by ORDER BY
following OVER is totally independent of the addition ORDER BY clause of the SELECT
statement. The optional addition ORDER BY, which can be specified independently of
PARTITION, defines an order in the current window and an evaluation framework for the
window function. ORDER BY is followed by a comma-separated list of columns of the
data sources of the current query. These columns are used to sort the rows of the
window. A column can only be specified directly using the column name col1, col2 ... .
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/application-developmentblog-board/article-id/44800 3/8
8/1/24, 6:26 PM Window Expressions in ABAP SQL - SAP Community
Alias names defined using AS cannot be specified. The additions ASCENDING and
DESCENDING determine whether the rows are sorted in ascending or descending order
by the column in question. The default is ASCENDING.
The rows of the window are processed by the window function in the order defined
by the sort order. The order of the processing of rows that appear more than once
regarding the sort criterion is not defined. If the addition ORDER BY is not
specified, this applies to all rows of the window.
The rows processed by the window function are additionally restricted by a frame.
Only the rows of the window that are in front of the current row in the sorting, or
that have the same values in the columns of the sort criterion are respected.
While the restricted frame mainly affects the results of aggregate functions specified as
a window function, the processing sequence primarily affects the ranking functions. The
addition ORDER BY must be specified for the ranking functions RANK and
DENSE_RANK.
A frame is defined by a starting frame boundary and an ending frame boundary. There
are three options for the starting and ending frame boundaries:
CURRENT ROW can be used as both starting and ending frame boundary. It
specifies that the window starts or ends at the current row, including the current
row in the frame.
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/application-developmentblog-board/article-id/44800 4/8
8/1/24, 6:26 PM Window Expressions in ABAP SQL - SAP Community
n must be 0, a positive integer literal, or a host expression that can be resolved into a
constant of type b, s,i, or int8.
ORDER BY is mandatory. Keep in mind that the ending frame boundary cannot be
smaller than the starting frame boundary. If no window frame is used, the window
function computes all rows up to the current row. As a result, the function returns
cumulative values.
Example
The example demonstrates how different window functions are applied to frames inside
a window.
count sorts the rows by column ID and counts the number of rows from the first
row of the window to the current row. The result is the same as that returned by
the ROW_NUMBER( ) function.
count_reverse sorts the rows by column ID and counts the number of rows from
the current row to the last row of the window. The result is reverse numbering.
average sorts the rows by column ID and calculates the rolling averages of a
subset of rows from column col1. The subset consists of the current row plus one
preceding and one following row. With this function, it is possible, for example, to
calculate the 3-day-average temperature for every day from a list of temperature
data.
accumulate sorts the rows by column ID and accumulates the values from the first
row of the window up to the current row, thus computing the running total of
column col1.
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/application-developmentblog-board/article-id/44800 5/8
8/1/24, 6:26 PM Window Expressions in ABAP SQL - SAP Community
Window expressions are not supported by ABAP CDS. In order to use them before 7.54,
you can use AMDP. More details and executable examples can be found in the ABAP
Keyword Documentation.
Tags:
Add tags
Comment
Comments
11-26-2021
Triantafillos_p Explorer
12:34 PM
After over 20 years of experience in SAP ABAP, every day I'm learning something
new.
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/application-developmentblog-board/article-id/44800 6/8
8/1/24, 6:26 PM Window Expressions in ABAP SQL - SAP Community
Thanks Safa!
11-26-2021
former_member763514 Active Participant
1:59 PM
you're welcome
so don't miss the second part about new window function supported by ABAP
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/application-developmentblog-board/article-id/44800 7/8
8/1/24, 6:26 PM Window Expressions in ABAP SQL - SAP Community
https://community.sap.com/t5/blogs/blogarticleprintpage/blog-id/application-developmentblog-board/article-id/44800 8/8