Collections:
LAG() - N-Row before Current Row within Window
How to evaluate a field expression on n-row before the current row in the current result set window using the LAG() function?
✍: FYIcenter.com
LAG(exp, n, default) is a MySQL built-in window function that
evaluates a field expression on n-row before the current row
in the current result set window.
For example:
SELECT help_topic_id AS tip, help_category_id AS cid, name, LAG(name, 2, NULL) OVER w FROM mysql.help_topic WINDOW w AS (PARTITION BY help_category_id); -- +-----+-----+----------------+----------------------------+ -- | tip | cid | name | LAG(name, 2, NULL) OVER w | -- +-----+-----+----------------+----------------------------+ -- | 0 | 1 | HELP_DATE | NULL | -- | 1 | 1 | HELP_VERSION | NULL | -- | 2 | 2 | AUTO_INCREMENT | NULL | -- | 6 | 2 | BIT | NULL | -- | 7 | 2 | TINYINT | AUTO_INCREMENT | -- | 8 | 2 | BOOLEAN | BIT | -- | 9 | 2 | SMALLINT | TINYINT | -- | 10 | 2 | MEDIUMINT | BOOLEAN | -- ... -- +-----+-----+----------------+----------------------------+
Reference information of the LAG() function:
LAG(exp, n, default): val Evaluates a field expression on n-row before the current row in the current result set window. Arguments, return value and availability: exp: Required. The field expression to be evaluated. n: Optional. Default is 1. The number of rows lagging the current row. default: Optional. Default is NULL. The default value for non-existing rows. val: Return value. The exp value on n-row before the current row. Available since MySQL 8.
Related MySQL functions:
⇒ LAST_VALUE() - Last Value of Result Set Window
⇐ FIRST_VALUE() - First Value of Result Set Window
2024-09-28, 1311🔥, 0💬
Popular Posts:
How To Find Out What Privileges a User Currently Has in Oracle? Privileges granted to users are list...
What are binary literals supported in SQL Server Transact-SQL? Binary literals in Transact-SQL are s...
Where to find tutorials to answer some frequently asked questions on Microsoft SQL Server Transact-S...
How To Break Query Output into Pages in MySQL? If you have a query that returns hundreds of rows, an...
How To Select All Columns of All Rows from a Table with a SELECT statement in SQL Server? The simple...