📜  MySQL LEAD和LAG函数

📅  最后修改于: 2020-11-19 00:47:21             🧑  作者: Mango

MySQL LEAD和LAG功能

LEAD和LAG是MySQL中的窗口函数,用于访问其分区内当前行中指定行的前后值。这些函数是非聚合函数的一种。

MySQL中的Window函数用于对分区或窗口中的每一行执行操作或计算。这些函数产生的结果类似于使用聚合函数进行的计算。但是,与对整个表执行操作的聚合函数不同,窗口函数不会产生要分组为一行的结果。因此,每一行都保持唯一标识。在window函数,我们必须了解以下内容:

  • 发生函数评估的行称为当前行
  • 窗口是与当前行相关的行集合,或者是在该行上使用哪个函数操作的窗口

让我们详细了解这些功能。

MySQL LEAD功能

此函数使我们可以向前或向后查找行,以从当前行获取/访问该行的值。计算同一输出中当前行与后续行之间的差异是一种非常有用的方法。

以下是在MySQL中使用LEAD函数的一般语法:

LEAD(expression, offset , default_value) OVER (
    PARTITION BY (expr)
    ORDER BY (expr)
)

参数说明

LEAD函数语法包含以下参数。

Parameter Descriptions
expression It is a column name or any built-in function whose value return by the function.
offset It contains the number of rows succeeding from the current row. It should be a positive integer value. If it is zero, the function evaluates the result for the current row. If we omit this, the function uses 1 by default.
default_value It is a value that will return when we have no subsequent row from the current row. If we omit this, the function returns the null value.
OVER OVERIt is responsible for partitioning rows into groups. If it is empty, the function performs an operation using all rows.
PARTITION BY It split the rows in the result set into partitions to which a function is applied. If we have not specified this clause, all rows treated as a single row in the result set.
ORDER BY It determines the sequence of rows in the partitions before the function is applied.

MySQL LEAD()函数示例

在这里,我们将了解LEAD函数如何与MySQL表一起使用。首先,我们需要使用以下语句创建一个名为sales_table的表。

CREATE TABLE sales_table (
    Employee_Name VARCHAR(45) NOT NULL,
    Year INT NOT NULL,
    Country VARCHAR(45) NOT NULL,
    Product VARCHAR(45) NOT NULL,
    Sale DECIMAL(12,2) NOT NULL,
    PRIMARY KEY(Employee_Name, Year)  
);

接下来,我们将使用INSERT语句将记录添加到该表中,如下所示:

INSERT INTO sales_table VALUES
('Stephen', 2017, 'India', 'Laptop', 10000),  
('Stephen', 2018, 'India', 'Laptop', 15000),  
('Stephen', 2019, 'India', 'TV', 20000),  
('Bob', 2017, 'US', 'Computer', 15000),  
('Bob', 2018, 'US', 'Computer', 10000),  
('Bob', 2019, 'US', 'TV', 20000),  
('Mandy', 2017, 'Canada', 'Mobile', 20000),  
('Mandy', 2018, 'Canada', 'Calculator', 1500),  
('Mandy', 2019, 'Canada', 'Mobile', 25000);

我们可以使用SELECT语句将记录验证到表中。它将给出如下输出:

以下语句查找每个员工的销售和下一个销售详细信息:

SELECT Year, Product, Sale,   
LEAD(Sale,1) OVER (
PARTITION BY Year
ORDER BY Country) AS Next_Sale  
FROM sales_table;

此示例首先将结果集按年份划分为多个分区,然后使用“国家/地区”列对每个分区进行排序。最后,我们在每个分区上应用了LEAD()函数以获得下一个销售明细。以下输出更清楚地说明了这一点:

在输出中,我们可以看到每个分区中的空值。当下一行越过分区边界时,每个分区的最后一行中的下一个值始终为NULL。

MySQL LAG功能

此函数使我们可以查看有关反向词行或前一行的信息,以获取/访问当前行中前一行的值。计算同一结果集中当前行与上一行之间的差异是一种非常有用的方法。

以下是在MySQL中使用LAG函数的常规语法:

LAG (expression, offset , default_value) OVER (
    PARTITION BY (expr)
    ORDER BY (expr [ASC|DESC])
)

参数说明

LAG函数语法包含以下参数。

Parameter Descriptions
expression It is a column name or any built-in function.
offset It contains the number of rows preceding from the current row. It should be zero or any positive integer value. If it is zero, the function evaluates the result for the current row. If we omit this, the function uses 1 by default.
default_value It is a value that will return when we have no preceding row from the current row. If we omit this, the function returns the null value.

其他参数(例如OVER,PARTITION BY,ORDER BY子句的含义)与LEAD函数相同。

MySQL LAG()函数示例

在这里,我们将了解LAG函数如何与MySQL表一起使用。我们可以使用上面名为sales_table的表进行演示。

以下语句查找每个员工的销售和以前的销售明细:

SELECT Year, Product, Sale,   
LAG(Sale, 1, 0) OVER (
PARTITION BY Year
ORDER BY Country) AS Previous_Sale_LAG
FROM sales_table;

此示例首先将结果集按年份划分为多个分区,然后使用“国家/地区”列对每个分区进行排序。最后,我们在每个分区上应用了LAG()函数以获得以前的销售明细。执行以上语句后,我们可以看到以下输出:

在输出中,我们可以看到每个分区中的0.00值。它指示表中将不存在的行的值。如果我们没有提供默认值,它将变为NULL。

注意:在MySQL 8.0版中引入了LEAD()和LAG()函数。因此我们不能在以前的版本中使用它。它们始终与OVER()子句一起使用。如果我们没有使用此子句,它将引发一个错误。