Question Details

No question body available.

Tags

mysql mariadb

Answers (1)

February 2, 2026 Score: 5 Rep: 526,266 Quality: Medium Completeness: 80%

As the MariaDB documentation for LAG() mentions, on MariaDB there is only a one or two (but not three) parameter version of the LAG() function:

LAG (expr[, offset]) OVER ( [ PARTITION BY partitionexpression ] < ORDER BY orderlist > )

The third parameter in MySQL corresponds to a default value, which kicks in when LAG() cannot find any previous record.

In this case, assuming that the electricity would never be null, we could use COALESCE() here to capture and replace the first call to LAG():

CASE season WHEN '2008/09' THEN 4029 WHEN '2013/14' THEN 3389 WHEN '2016/17' THEN 6367 WHEN '2018/19' THEN 5311 ELSE electricity - COALESCE(LAG(electricity, 1) OVER (ORDER BY season), 106740) END AS consumed