> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 分析函数 - 时间序列

> 在 ClickHouse 中用于分析时间序列数据的函数。

在 ClickHouse 中，可以使用标准 SQL 聚合和窗口函数进行时间序列分析。
处理时间序列数据时，通常会遇到三种主要的指标类型：

* 计数器类型指标，会随时间单调递增 (例如页面浏览量或事件总数)
* Gauge 指标，表示某一时刻的测量值，既可能上升也可能下降 (例如 CPU 使用率或温度)
* 直方图，通过对观测值采样并按桶计数来表示其分布 (例如请求耗时或响应大小)

这些指标的常见分析方式包括比较不同时期的值、计算累计总量、确定变化速率以及分析分布。
这些都可以通过组合使用聚合、像 `sum() OVER` 这样的窗口函数，以及像 `histogram()` 这样的专用函数来实现。

<div id="time-series-period-over-period-changes">
  ## 环比变化
</div>

在分析时间序列数据时，我们经常需要了解不同时间周期之间的值如何变化。
这对于 Gauge 和计数器类型指标都非常重要。
[`lagInFrame`](/docs/zh/reference/functions/window-functions/lagInFrame) 窗口函数可让我们访问上一周期的值，从而计算这些变化。

下面的查询通过计算 “Weird Al” Yankovic 的 Wikipedia 页面浏览量日环比变化来演示这一点。
trend 列显示与前一天相比，流量是增加了 (正值) 还是减少了 (负值) ，从而有助于识别活动中的异常峰值或下降。

```sql theme={null}
SELECT
    toDate(time) AS day,
    sum(hits) AS h,
    lagInFrame(h) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS p,
    h - p AS trend
FROM wikistat
WHERE path = '"Weird_Al"_Yankovic'
GROUP BY ALL
LIMIT 10;
```

```text theme={null}
┌────────day─┬────h─┬────p─┬─trend─┐
│ 2015-05-01 │ 3934 │    0 │  3934 │
│ 2015-05-02 │ 3411 │ 3934 │  -523 │
│ 2015-05-03 │ 3195 │ 3411 │  -216 │
│ 2015-05-04 │ 3076 │ 3195 │  -119 │
│ 2015-05-05 │ 3450 │ 3076 │   374 │
│ 2015-05-06 │ 3053 │ 3450 │  -397 │
│ 2015-05-07 │ 2890 │ 3053 │  -163 │
│ 2015-05-08 │ 3898 │ 2890 │  1008 │
│ 2015-05-09 │ 3092 │ 3898 │  -806 │
│ 2015-05-10 │ 3508 │ 3092 │   416 │
└────────────┴──────┴──────┴───────┘
```

<div id="time-series-cumulative-values">
  ## 累积值
</div>

计数器类型指标会随着时间自然累积。
为了分析这种累积增长，我们可以使用窗口函数来计算累计总和。

下面的查询通过使用 `sum() OVER` 子句来演示这一点，该子句会创建累计总和。`bar()` 函数则以可视化方式展示增长趋势。

```sql theme={null}
SELECT
    toDate(time) AS day,
    sum(hits) AS h,
    sum(h) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND 0 FOLLOWING) AS c,
    bar(c, 0, 50000, 25) AS b
FROM wikistat
WHERE path = '"Weird_Al"_Yankovic'
GROUP BY ALL
ORDER BY day
LIMIT 10;
```

```text theme={null}
┌────────day─┬────h─┬─────c─┬─b─────────────────┐
│ 2015-05-01 │ 3934 │  3934 │ █▉                │
│ 2015-05-02 │ 3411 │  7345 │ ███▋              │
│ 2015-05-03 │ 3195 │ 10540 │ █████▎            │
│ 2015-05-04 │ 3076 │ 13616 │ ██████▊           │
│ 2015-05-05 │ 3450 │ 17066 │ ████████▌         │
│ 2015-05-06 │ 3053 │ 20119 │ ██████████        │
│ 2015-05-07 │ 2890 │ 23009 │ ███████████▌      │
│ 2015-05-08 │ 3898 │ 26907 │ █████████████▍    │
│ 2015-05-09 │ 3092 │ 29999 │ ██████████████▉   │
│ 2015-05-10 │ 3508 │ 33507 │ ████████████████▊ │
└────────────┴──────┴───────┴───────────────────┘
```

<div id="time-series-rate-calculations">
  ## 速率计算
</div>

在分析时间序列数据时，了解单位时间内的事件发生速率通常很有帮助。
此查询通过将每小时总量除以一小时的秒数 (3600) ，计算出每秒页面浏览量的速率。
条形图有助于识别活动高峰时段。

```sql theme={null}
SELECT
    toStartOfHour(time) AS time,
    sum(hits) AS hits,
    round(hits / (60 * 60), 2) AS rate,
    bar(rate * 10, 0, max(rate * 10) OVER (), 25) AS b
FROM wikistat
WHERE path = '"Weird_Al"_Yankovic'
GROUP BY time
LIMIT 10;
```

```text theme={null}
┌────────────────time─┬───h─┬─rate─┬─b─────┐
│ 2015-07-01 01:00:00 │ 143 │ 0.04 │ █▊    │
│ 2015-07-01 02:00:00 │ 170 │ 0.05 │ ██▏   │
│ 2015-07-01 03:00:00 │ 148 │ 0.04 │ █▊    │
│ 2015-07-01 04:00:00 │ 190 │ 0.05 │ ██▏   │
│ 2015-07-01 05:00:00 │ 253 │ 0.07 │ ███▏  │
│ 2015-07-01 06:00:00 │ 233 │ 0.06 │ ██▋   │
│ 2015-07-01 07:00:00 │ 359 │  0.1 │ ████▍ │
│ 2015-07-01 08:00:00 │ 190 │ 0.05 │ ██▏   │
│ 2015-07-01 09:00:00 │ 121 │ 0.03 │ █▎    │
│ 2015-07-01 10:00:00 │  70 │ 0.02 │ ▉     │
└─────────────────────┴─────┴──────┴───────┘
```

<div id="time-series-histograms">
  ## 直方图
</div>

时间序列数据的一个常见用途，是基于已跟踪的事件构建直方图。
假设我们想了解总点击量超过 10,000 的页面，其总点击量的分布情况。
我们可以使用 `histogram()` 函数，根据 bin 的数量自动生成自适应直方图：

```sql theme={null}
SELECT
    histogram(10)(hits) AS hist
FROM
(
    SELECT
        path,
        sum(hits) AS hits
    FROM wikistat
    WHERE date(time) = '2015-06-15'
    GROUP BY path
    HAVING hits > 10000
)
FORMAT Vertical;
```

```text theme={null}
Row 1:
──────
hist: [(10033,23224.55065359477,60.625),(23224.55065359477,37855.38888888889,15.625),(37855.38888888889,52913.5,3.5),(52913.5,69438,1.25),(69438,83102.16666666666,1.25),(83102.16666666666,94267.66666666666,2.5),(94267.66666666666,116778,1.25),(116778,186175.75,1.125),(186175.75,946963.25,1.75),(946963.25,1655250,1.125)]
```

然后，我们可以使用 [`arrayJoin()`](/docs/zh/reference/functions/regular-functions/array-join) 对数据进行处理，并用 `bar()` 将其可视化：

```sql theme={null}
WITH histogram(10)(hits) AS hist
SELECT
    round(arrayJoin(hist).1) AS lowerBound,
    round(arrayJoin(hist).2) AS upperBound,
    arrayJoin(hist).3 AS count,
    bar(count, 0, max(count) OVER (), 20) AS b
FROM
(
    SELECT
        path,
        sum(hits) AS hits
    FROM wikistat
    WHERE date(time) = '2015-06-15'
    GROUP BY path
    HAVING hits > 10000
);
```

```text theme={null}
┌─lowerBound─┬─upperBound─┬──count─┬─b────────────────────┐
│      10033 │      19886 │ 53.375 │ ████████████████████ │
│      19886 │      31515 │ 18.625 │ ██████▉              │
│      31515 │      43518 │  6.375 │ ██▍                  │
│      43518 │      55647 │  1.625 │ ▌                    │
│      55647 │      73602 │  1.375 │ ▌                    │
│      73602 │      92880 │   3.25 │ █▏                   │
│      92880 │     116778 │  1.375 │ ▌                    │
│     116778 │     186176 │  1.125 │ ▍                    │
│     186176 │     946963 │   1.75 │ ▋                    │
│     946963 │    1655250 │  1.125 │ ▍                    │
└────────────┴────────────┴────────┴──────────────────────┘
```
