コンテンツにスキップ

LAG を使用すると、同じ結果セット内の前の行にある列の値にアクセスできます。通常は、指定した順序に基づいて、前の行の列の値を取得するために使用します。

関連項目: LEAD

func.lag(<expr>, <offset>).over(partition_by=[<columns>], order_by=[<columns>])
table.product_name, table.sale_amount, func.lag(table.sale_amount, 1).over(partition_by=table.product_name, order_by=table.sale_id).alias('previous_sale_amount')
product_name | sale_amount | previous_sale_amount
-----------------------------------------------
Product A | 1000.00 | NULL
Product A | 1500.00 | 1000.00
Product A | 2000.00 | 1500.00
Product B | 500.00 | NULL
Product B | 800.00 | 500.00
Product B | 1200.00 | 800.00
LAG(expression [, offset [, default]]) OVER (PARTITION BY partition_expression ORDER BY sort_expression)
  • offset: 値を取得する対象として、パーティション内で現在の行から先(LEAD)または後ろ(LAG)に何行離れた行かを指定します。デフォルトは 1 です。

負のオフセットを指定すると、LEAD関数を使用した場合と同じ効果になります。

  • default: オフセットがパーティションの境界を超えたために、LEAD または LAG 関数で利用可能な値がない場合に返す値を指定します。デフォルトは NULL です。
CREATE TABLE sales (
sale_id INT,
product_name VARCHAR(50),
sale_amount DECIMAL(10, 2)
);
INSERT INTO sales (sale_id, product_name, sale_amount)
VALUES (1, 'Product A', 1000.00),
(2, 'Product A', 1500.00),
(3, 'Product A', 2000.00),
(4, 'Product B', 500.00),
(5, 'Product B', 800.00),
(6, 'Product B', 1200.00);
SELECT product_name, sale_amount, LAG(sale_amount) OVER (PARTITION BY product_name ORDER BY sale_id) AS previous_sale_amount
FROM sales;
product_name | sale_amount | previous_sale_amount
-----------------------------------------------
Product A | 1000.00 | NULL
Product A | 1500.00 | 1000.00
Product A | 2000.00 | 1500.00
Product B | 500.00 | NULL
Product B | 800.00 | 500.00
Product B | 1200.00 | 800.00
-- The following statements return the same result.
SELECT product_name, sale_amount, LAG(sale_amount, -1) OVER (PARTITION BY product_name ORDER BY sale_id) AS next_sale_amount
FROM sales;
SELECT product_name, sale_amount, LEAD(sale_amount) OVER (PARTITION BY product_name ORDER BY sale_id) AS next_sale_amount
FROM sales;
product_name|sale_amount|next_sale_amount|
------------+-----------+----------------+
Product A | 1000.00| 1500.00|
Product A | 1500.00| 2000.00|
Product A | 2000.00| |
Product B | 500.00| 800.00|
Product B | 800.00| 1200.00|
Product B | 1200.00| |