> ## 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.

> lagInFrame 窗口函数文档

# lagInFrame

返回有序窗口帧内当前行之前指定物理偏移量对应行上计算得到的值。

<Warning>
  `lagInFrame` 的行为与标准 SQL 窗口函数 `lag` 不同。
  ClickHouse 窗口函数 `lagInFrame` 会遵循窗口帧。
  若要获得与 `lag` 完全相同的行为，请使用 `ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING`。
</Warning>

**语法**

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

有关窗口函数语法的更多信息，请参见：[Window Functions - Syntax](/zh/reference/functions/window-functions#syntax)。

**参数**

* `x` — 列名。
* `offset` — 要应用的偏移量。[(U)Int\*](/zh/reference/data-types/int-uint)。 (可选 — 默认为 `1`) 。
* `default` — 如果计算出的行超出窗口帧边界时返回的值。 (可选 — 省略时使用列类型的默认值) 。

**返回值**

* 在有序窗口帧中，当前行之前指定物理偏移处对应行的值。

**示例**

此示例查看特定股票的历史数据，并使用 `lagInFrame` 函数计算该股票收盘价的日环比变化值和百分比变化。

```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,
    lagInFrame(close, 1, close) OVER (ORDER BY date ASC
       ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
     ) 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 │
   └────────────┴────────┴────────────────────┴───────┴────────────────┘
```
