> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> lag ウィンドウ関数のドキュメント

# lag

並び順が定義されたフレーム内で、現在の行から指定された物理オフセット分だけ前にある行で評価された値を返します。
この関数は [`lagInFrame`](/ja/reference/functions/window-functions/lagInFrame) に似ていますが、常に `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING` フレームを使用します。

**Syntax**

```sql theme={null}
lag(x[, offset[, default]])
  OVER ([[PARTITION BY grouping_column] [ORDER BY sorting_column]] | [window_name])
FROM table_name
WINDOW window_name as ([[PARTITION BY grouping_column] [ORDER BY sorting_column])
```

ウィンドウ関数の構文の詳細については、[ウィンドウ関数 - 構文](/ja/reference/functions/window-functions#syntax)を参照してください。

**パラメータ**

* `x` — カラム名。
* `offset` — 適用するオフセット。[(U)Int\*](/ja/reference/data-types/int-uint)。 (省略可能。デフォルトは `1`) 。
* `default` — 計算対象の行がウィンドウフレームの境界を超えた場合に返される値。 (省略可能。省略時はカラム型のデフォルト値) 。

**戻り値**

* 順序付けされたフレーム内で、現在の行より指定した物理オフセット分だけ前にある行で評価された値。

**例**

この例では、特定の株式の過去データを対象に、`lag` 関数を使用して終値の日次差分と変化率を計算します。

```sql title="Query" theme={null}
CREATE TABLE stock_prices
(
    `date`   Date,
    `open`   Float32, -- 始値
    `high`   Float32, -- 日中高値
    `low`    Float32, -- 日中安値
    `close`  Float32, -- 終値
    `volume` UInt32   -- 取引量
)
Engine = Memory;

INSERT INTO stock_prices FORMAT Values
    ('2024-06-03', 113.62, 115.00, 112.00, 115.00, 438392000),
    ('2024-06-04', 115.72, 116.60, 114.04, 116.44, 403324000),
    ('2024-06-05', 118.37, 122.45, 117.47, 122.44, 528402000),
    ('2024-06-06', 124.05, 125.59, 118.32, 121.00, 664696000),
    ('2024-06-07', 119.77, 121.69, 118.02, 120.89, 412386000);
```

```sql title="Query" theme={null}
SELECT
    date,
    close,
    lag(close, 1, close) OVER (ORDER BY date ASC) AS previous_day_close,
    COALESCE(ROUND(close - previous_day_close, 2)) AS delta,
    COALESCE(ROUND((delta / previous_day_close) * 100, 2)) AS percent_change
FROM stock_prices
ORDER BY date DESC
```

```response title="Response" theme={null}
   ┌───────date─┬──close─┬─previous_day_close─┬─delta─┬─percent_change─┐
1. │ 2024-06-07 │ 120.89 │                121 │ -0.11 │          -0.09 │
2. │ 2024-06-06 │    121 │             122.44 │ -1.44 │          -1.18 │
3. │ 2024-06-05 │ 122.44 │             116.44 │     6 │           5.15 │
4. │ 2024-06-04 │ 116.44 │                115 │  1.44 │           1.25 │
5. │ 2024-06-03 │    115 │                115 │     0 │              0 │
   └────────────┴────────┴────────────────────┴───────┴────────────────┘
```
