MySQL LAST_VALUE() 函数
简介:在本教程中,您将学习如何使用MySQL
LAST_VALUE()
函数返回有序行集中的最后一行。
MySQL LAST_VALUE() 函数概述
LAST_VALUE()
函数是一个窗口函数,允许您选择有序行集中的最后一行。
以下显示了LAST_VALUE()
函数的语法:
LAST_VALUE (expression) OVER ( [partition_clause] [order_clause] [frame_clause] )
LAST_VALUE()
函数返回expression
有序行集的最后一行的值。
OVER
有三个子句:partition_clause
,order_clause
,和frame_clause
。
partition_clause
partition_clause
语法如下:
PARTITION BY expr1, expr2, ...
PARTITION BY
子句分配结果集成由一个或多个表达式指定多个分区expr1
,expr2
等LAST_VALUE()
函数被独立地施加到每个分区。
order_clause
order_clause
语法如下:
ORDER BY expr1 [ASC|DESC],...
ORDER BY
子句指定LAST_VALUE()
函数运行的分区中行的逻辑顺序。
frame_clause
frame_clause
定义了所述当前分区的所述子集LAST_VALUE()
函数应用。有关更多详细信息frame_clause
,请查看窗口功能教程。
MySQL LAST_VALUE() 函数示例
让我们设置一个示例表进行演示。 以下是创建overtime
表并将数据填充到表中的脚本。
CREATE TABLE overtime ( employee_name VARCHAR(50) NOT NULL, department VARCHAR(50) NOT NULL, hours INT NOT NULL, PRIMARY KEY (employee_name , department) ); INSERT INTO overtime(employee_name, department, hours) VALUES('Diane Murphy','Accounting',37), ('Mary Patterson','Accounting',74), ('Jeff Firrelli','Accounting',40), ('William Patterson','Finance',58), ('Gerard Bondur','Finance',47), ('Anthony Bow','Finance',66), ('Leslie Jennings','IT',90), ('Leslie Thompson','IT',88), ('Julie Firrelli','Sales',81), ('Steve Patterson','Sales',29), ('Foon Yue Tseng','Sales',65), ('George Vanauf','Marketing',89), ('Loui Bondur','Marketing',49), ('Gerard Hernandez','Marketing',66), ('Pamela Castillo','SCM',96), ('Larry Bott','SCM',100), ('Barry Jones','SCM',65);
mysql> select * from overtime; +-------------------+------------+-------+ | employee_name | department | hours | +-------------------+------------+-------+ | Anthony Bow | Finance | 66 | | Barry Jones | SCM | 65 | | Diane Murphy | Accounting | 37 | | Foon Yue Tseng | Sales | 65 | | George Vanauf | Marketing | 89 | | Gerard Bondur | Finance | 47 | | Gerard Hernandez | Marketing | 66 | | Jeff Firrelli | Accounting | 40 | | Julie Firrelli | Sales | 81 | | Larry Bott | SCM | 100 | | Leslie Jennings | IT | 90 | | Leslie Thompson | IT | 88 | | Loui Bondur | Marketing | 49 | | Mary Patterson | Accounting | 74 | | Pamela Castillo | SCM | 96 | | Steve Patterson | Sales | 29 | | William Patterson | Finance | 58 | +-------------------+------------+-------+ 17 rows in set (0.01 sec)
1)MySQL LAST_VALUE()
在整个查询结果示例中
以下语句获取员工姓名,加班时间和加班时间最长的员工:
SELECT employee_name, hours, LAST_VALUE(employee_name) OVER ( ORDER BY hours RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) highest_overtime_employee FROM overtime;输出是:
+-------------------+-------+---------------------------+ | employee_name | hours | highest_overtime_employee | +-------------------+-------+---------------------------+ | Steve Patterson | 29 | Larry Bott | | Diane Murphy | 37 | Larry Bott | | Jeff Firrelli | 40 | Larry Bott | | Gerard Bondur | 47 | Larry Bott | | Loui Bondur | 49 | Larry Bott | | William Patterson | 58 | Larry Bott | | Barry Jones | 65 | Larry Bott | | Foon Yue Tseng | 65 | Larry Bott | | Anthony Bow | 66 | Larry Bott | | Gerard Hernandez | 66 | Larry Bott | | Mary Patterson | 74 | Larry Bott | | Julie Firrelli | 81 | Larry Bott | | Leslie Thompson | 88 | Larry Bott | | George Vanauf | 89 | Larry Bott | | Leslie Jennings | 90 | Larry Bott | | Pamela Castillo | 96 | Larry Bott | | Larry Bott | 100 | Larry Bott | +-------------------+-------+------------------------在此示例中,
ORDER BY
子句将结果集中行的逻辑顺序指定为从低到高的小时。
默认帧规范如下:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW这意味着框架从第一行开始,到结果集的当前行结束。 因此,为了获得加班时间最长的员工,我们将框架规格更改为以下内容:
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING这表示框架从第一行开始,到结果集的最后一行结束。
2)MySQL LAST_VALUE()
上的分区示例
以下语句查找每个部门加班时间最长的员工:
SELECT employee_name, department, hours, LAST_VALUE(employee_name) OVER ( PARTITION BY department ORDER BY hours RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) most_overtime_employee FROM overtime;下图显示了输出:
+-------------------+------------+-------+------------------------+ | employee_name | department | hours | most_overtime_employee | +-------------------+------------+-------+------------------------+ | Diane Murphy | Accounting | 37 | Mary Patterson | | Jeff Firrelli | Accounting | 40 | Mary Patterson | | Mary Patterson | Accounting | 74 | Mary Patterson | | Gerard Bondur | Finance | 47 | Anthony Bow | | William Patterson | Finance | 58 | Anthony Bow | | Anthony Bow | Finance | 66 | Anthony Bow | | Leslie Thompson | IT | 88 | Leslie Jennings | | Leslie Jennings | IT | 90 | Leslie Jennings | | Loui Bondur | Marketing | 49 | George Vanauf | | Gerard Hernandez | Marketing | 66 | George Vanauf | | George Vanauf | Marketing | 89 | George Vanauf | | Steve Patterson | Sales | 29 | Julie Firrelli | | Foon Yue Tseng | Sales | 65 | Julie Firrelli | | Julie Firrelli | Sales | 81 | Julie Firrelli | | Barry Jones | SCM | 65 | Larry Bott | | Pamela Castillo | SCM | 96 | Larry Bott | | Larry Bott | SCM | 100 | Larry Bott | +-------------------+------------+-------+------------------------+ 17 rows in set (0.02 sec)在这个例子中,首先,
PARTITION BY
子句按部门划分了员工。然后,ORDER BY
子句通过加班从低到高命令每个部门的员工。
在这种情况下,帧规范是整个分区。结果,LAST_VALUE()
函数选择了每个分区中的最后一行,分区是加班时间最高的员工。
在本教程中,您学习了如何使用MySQL LAST_VALUE()
函数获取有序行集中的最后一行。