©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The LAST_VALUE SQL Function in MySQL and MariaDB - Evaluate expressions and return last

LAST_VALUESyntax:LAST_VALUE(Expression [, Expression ...])
Return value:Depending on the data types used
Function type:Window function, Information function
Function Description

The LAST_VALUE() function evaluates the passed expressions and returns the value of the last expression.

Along with setting user variables in the expressions, for example, data from updated or deleted data rows can be returned to the table in a single query.

If there is no last value, the function returns NULL.

SQL Examples for the LAST_VALUE function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

select last_value(1,2);
select last_value(19,12,14,13);
select last_value('a','b','c','d');



DROP TABLE IF EXISTS `test_table`;

CREATE TABLE `test_table` (
  `id` int(11) NOT null AUTO_INCREMENT,
  `name` varchar(20) DEFAULT null,
  `type` enum('First','Second','Third','Last'DEFAULT null,
  `set` SET('First','Second','Third','Last'DEFAULT null,
  `num` int(11) NOT null DEFAULT 0,
  `price` decimal(10,2) NOT null DEFAULT 0.00,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

INSERT INTO `test_table` (`id`,`name`,`type`,`set`,`num`,`price`VALUES 
(1,'Art 1','First','Last',1,10.50),
(2,'Art 2','Second','First,Last',3,12.50),
(3,'Art 3','Second','Second,Last',2,15.50),
(4,'Art 4','Third','First,Second',5,25.50),
(5,'Art 5','Last','First,Last',2,5.50),
(6,'Art 6','First','First',12,1.50),
(7,'Art 7','Third','First,Third',20,1.75),
(8,'Art 8','Last','Second,Third',100,11.90);

SELECT * FROM `test_table`;

SELECT * FROM `test_table` where id=last_value(@n:=name, @t:=`type`@s:=`set`, 1);
select @n@t@s;

delete FROM `test_table` where id=last_value(@n:=name, @t:=`type`@s:=`set`, 8);
select @n@t@s;

update `test_table` set `type`='first' where last_value(@n:=name, @t:=`type`@s:=`set`, 1);
select @n@t@s;
SELECT * FROM `test_table` 

last_value(1,2)
int(1)
2
last_value(19,12,14,13)
int(2)
13
last_value('a','b','c','d')
varchar(1) BINARY
d
id
name
type
set
num
price
int(11)varchar(20) LATIN1enum LATIN1set LATIN1int(11)decimal(10,2)
1Art 1FirstLast110.50
2Art 2SecondFirst,Last312.50
3Art 3SecondSecond,Last215.50
4Art 4ThirdFirst,Second525.50
5Art 5LastFirst,Last25.50
6Art 6FirstFirst121.50
7Art 7ThirdFirst,Third201.75
8Art 8LastSecond,Third10011.90
id
name
type
set
num
price
int(11)varchar(20) LATIN1enum LATIN1set LATIN1int(11)decimal(10,2)
1Art 1FirstLast110.50
@n
@t
@s
Art 8LastSecond,Third
@n
@t
@s
Art 8LastSecond,Third
@n
@t
@s
Art 7ThirdFirst,Third
id
name
type
set
num
price
int(11)varchar(20) LATIN1enum LATIN1set LATIN1int(11)decimal(10,2)
1Art 1FirstLast110.50
2Art 2FirstFirst,Last312.50
3Art 3FirstSecond,Last215.50
4Art 4FirstFirst,Second525.50
5Art 5FirstFirst,Last25.50
6Art 6FirstFirst121.50
7Art 7FirstFirst,Third201.75
The examples were created with the MyWAY SQL manager: Download

How to use the LAST_VALUE() function in MySQL and MariaDB databases

The LAST_VALUE() function is used in MySQL and MariaDB to retrieve the last value in an ordered series of data rows. It is typically used in conjunction with the OVER clause, which defines the window or subset of rows on which the function is performed. The function can be used to access the last values ​​in this data window.

The function is used, among other things, to analyze time-series data, for example in tables storing time-series data such as stock prices or sensor values, LAST_VALUE() can be used to get the latest values ​​in the series by order of data lines by timestamp. The feature can also be used to track data changes over time, for example an audit trail or revision history, the feature can be used to determine the latest status of a particular field.

When searching for the last occurrence of a specific event or condition, LAST_VALUE() can be used to retrieve the last instance of a specific value or condition in a table column. Also, the function can be useful when you want to calculate cumulative values ​​based on a specific order, such as a running total, where the value of each row depends on the value of the previous row plus the value of the current row.

Using LAST_VALUE() mostly requires an ORDER BY clause in the OVER clause, since the order of the data determines which rows are considered for the calculation of the last value.
IS_UUIDISNULLLAST_INSERT_IDLAST_VALUELASTVALLCASELEAST

Further MySQL und MariaDB SQL Window functions

AVGAVG(Expression or column)
More about AVG Function

BIT_ANDBIT_AND(Expression or column)
More about BIT_AND Function

BIT_ORBIT_OR(Expression or column)
More about BIT_OR Function

BIT_XORBIT_XOR(Expression or column)
More about BIT_XOR Function

CUME_DISTCUME_DIST()

DENSE_RANKDENSE_RANK()

FIRST_VALUEFIRST_VALUE(Expression)

JSON_ARRAYAGGJSON_ARRAYAGG(Expression or column)

JSON_OBJECTAGGJSON_OBJECTAGG(Expression or column)

LAGLAG(Expression [, Rows] [, Default])

LEADLEAD(Expression [, Rows] [, Default])

MAXMAX(Expression or column)
More about MAX Function

MEDIANMEDIAN(Expression or column)
More about MEDIAN Function

MINMIN(Expression or column)
More about MIN Function

NTH_VALUENTH_VALUE(Expression [, Row])

NTILENTILE(Number)

PERCENT_RANKPERCENT_RANK()

PERCENTILE_CONTPERCENTILE_CONT()

PERCENTILE_DISCPERCENTILE_DISC()

RANKRANK()

ROW_NUMBERROW_NUMBER()

STDSTD(Expression or column)
More about STD Function

STDDEVSTDDEV(Expression or column)
More about STDDEV Function

STDDEV_POPSTDDEV_POP(Expression or column)
More about STDDEV_POP Function

STDDEV_SAMPSTDDEV_SAMP(Expression or column)
More about STDDEV_SAMP Function

SUMSUM(Expression or column)
More about SUM Function

VAR_POPVAR_POP(Expression or column)
More about VAR_POP Function

VAR_SAMPVAR_SAMP(Expression or column)
More about VAR_SAMP Function

VARIANCEVARIANCE(Expression or column)
More about VARIANCE Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the LAST_VALUE SQL function: mysql.com and mariadb.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer