| <!-- |
| |
| Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| |
| --> |
| |
| # 查询结果分页 |
| |
| 当查询结果集数据量很大,放在一个页面不利于显示,可以使用 `LIMIT/SLIMIT` 子句和 `OFFSET/SOFFSET `子句进行分页控制。 |
| |
| - `LIMIT` 和 `SLIMIT` 子句用于控制查询结果的行数和列数。 |
| - `OFFSET` 和 `SOFFSET` 子句用于控制结果显示的起始位置。 |
| |
| ## 按行分页 |
| |
| 用户可以通过 `LIMIT` 和 `OFFSET` 子句控制查询结果的行数,`LIMIT rowLimit` 指定查询结果的行数,`OFFSET rowOffset` 指定查询结果显示的起始行位置。 |
| |
| 注意: |
| - 当 `rowOffset` 超过结果集的大小时,返回空结果集。 |
| - 当 `rowLimit` 超过结果集的大小时,返回所有查询结果。 |
| - 当 `rowLimit` 和 `rowOffset` 不是正整数,或超过 `INT32` 允许的最大值时,系统将提示错误。 |
| |
| 我们将通过以下示例演示如何使用 `LIMIT` 和 `OFFSET` 子句。 |
| |
| - **示例 1:** 基本的 `LIMIT` 子句 |
| |
| SQL 语句: |
| |
| ```sql |
| select status, temperature from root.ln.wf01.wt01 limit 10 |
| ``` |
| |
| 含义: |
| |
| 所选设备为 ln 组 wf01 工厂 wt01 设备; 选择的时间序列是“状态”和“温度”。 SQL 语句要求返回查询结果的前 10 行。 |
| |
| 结果如下所示: |
| |
| ``` |
| +-----------------------------+------------------------+-----------------------------+ |
| | Time|root.ln.wf01.wt01.status|root.ln.wf01.wt01.temperature| |
| +-----------------------------+------------------------+-----------------------------+ |
| |2017-11-01T00:00:00.000+08:00| true| 25.96| |
| |2017-11-01T00:01:00.000+08:00| true| 24.36| |
| |2017-11-01T00:02:00.000+08:00| false| 20.09| |
| |2017-11-01T00:03:00.000+08:00| false| 20.18| |
| |2017-11-01T00:04:00.000+08:00| false| 21.13| |
| |2017-11-01T00:05:00.000+08:00| false| 22.72| |
| |2017-11-01T00:06:00.000+08:00| false| 20.71| |
| |2017-11-01T00:07:00.000+08:00| false| 21.45| |
| |2017-11-01T00:08:00.000+08:00| false| 22.58| |
| |2017-11-01T00:09:00.000+08:00| false| 20.98| |
| +-----------------------------+------------------------+-----------------------------+ |
| Total line number = 10 |
| It costs 0.000s |
| ``` |
| |
| - **示例 2:** 带 `OFFSET` 的 `LIMIT` 子句 |
| |
| SQL 语句: |
| |
| ```sql |
| select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 |
| ``` |
| |
| 含义: |
| |
| 所选设备为 ln 组 wf01 工厂 wt01 设备; 选择的时间序列是“状态”和“温度”。 SQL 语句要求返回查询结果的第 3 至 7 行(第一行编号为 0 行)。 |
| |
| 结果如下所示: |
| |
| ``` |
| +-----------------------------+------------------------+-----------------------------+ |
| | Time|root.ln.wf01.wt01.status|root.ln.wf01.wt01.temperature| |
| +-----------------------------+------------------------+-----------------------------+ |
| |2017-11-01T00:03:00.000+08:00| false| 20.18| |
| |2017-11-01T00:04:00.000+08:00| false| 21.13| |
| |2017-11-01T00:05:00.000+08:00| false| 22.72| |
| |2017-11-01T00:06:00.000+08:00| false| 20.71| |
| |2017-11-01T00:07:00.000+08:00| false| 21.45| |
| +-----------------------------+------------------------+-----------------------------+ |
| Total line number = 5 |
| It costs 0.342s |
| ``` |
| |
| - **示例 3:** `LIMIT` 子句与 `WHERE` 子句结合 |
| |
| SQL 语句: |
| |
| ```sql |
| select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 5 offset 3 |
| ``` |
| |
| 含义: |
| |
| 所选设备为 ln 组 wf01 工厂 wt01 设备; 选择的时间序列是“状态”和“温度”。 SQL 语句要求返回时间“ 2017-11-01T00:05:00.000”和“ 2017-11-01T00:12:00.000”之间的状态和温度传感器值的第 3 至 4 行(第一行) 编号为第 0 行)。 |
| |
| 结果如下所示: |
| |
| ``` |
| +-----------------------------+------------------------+-----------------------------+ |
| | Time|root.ln.wf01.wt01.status|root.ln.wf01.wt01.temperature| |
| +-----------------------------+------------------------+-----------------------------+ |
| |2017-11-01T00:03:00.000+08:00| false| 20.18| |
| |2017-11-01T00:04:00.000+08:00| false| 21.13| |
| |2017-11-01T00:05:00.000+08:00| false| 22.72| |
| |2017-11-01T00:06:00.000+08:00| false| 20.71| |
| |2017-11-01T00:07:00.000+08:00| false| 21.45| |
| +-----------------------------+------------------------+-----------------------------+ |
| Total line number = 5 |
| It costs 0.000s |
| ``` |
| |
| - **示例 4:** `LIMIT` 子句与 `GROUP BY` 子句组合 |
| |
| SQL 语句: |
| |
| ```sql |
| select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 4 offset 3 |
| ``` |
| |
| 含义: |
| |
| SQL 语句子句要求返回查询结果的第 3 至 6 行(第一行编号为 0 行)。 |
| |
| 结果如下所示: |
| |
| ``` |
| +-----------------------------+-------------------------------+----------------------------------------+ |
| | Time|count(root.ln.wf01.wt01.status)|max_value(root.ln.wf01.wt01.temperature)| |
| +-----------------------------+-------------------------------+----------------------------------------+ |
| |2017-11-04T00:00:00.000+08:00| 1440| 26.0| |
| |2017-11-05T00:00:00.000+08:00| 1440| 26.0| |
| |2017-11-06T00:00:00.000+08:00| 1440| 25.99| |
| |2017-11-07T00:00:00.000+08:00| 1380| 26.0| |
| +-----------------------------+-------------------------------+----------------------------------------+ |
| Total line number = 4 |
| It costs 0.016s |
| ``` |
| |
| ## 按列分页 |
| |
| 用户可以通过 `SLIMIT` 和 `SOFFSET` 子句控制查询结果的列数,`SLIMIT seriesLimit` 指定查询结果的列数,`SOFFSET seriesOffset` 指定查询结果显示的起始列位置。 |
| |
| 注意: |
| - 仅用于控制值列,对时间列和设备列无效。 |
| - 当 `seriesOffset` 超过结果集的大小时,返回空结果集。 |
| - 当 `seriesLimit` 超过结果集的大小时,返回所有查询结果。 |
| - 当 `seriesLimit` 和 `seriesOffset` 不是正整数,或超过 `INT32` 允许的最大值时,系统将提示错误。 |
| |
| 我们将通过以下示例演示如何使用 `SLIMIT` 和 `SOFFSET` 子句。 |
| |
| - **示例 1:** 基本的 `SLIMIT` 子句 |
| |
| SQL 语句: |
| |
| ```sql |
| select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 |
| ``` |
| |
| 含义: |
| |
| 所选设备为 ln 组 wf01 工厂 wt01 设备; 所选时间序列是该设备下的第二列,即温度。 SQL 语句要求在"2017-11-01T00:05:00.000"和"2017-11-01T00:12:00.000"的时间点之间选择温度传感器值。 |
| |
| 结果如下所示: |
| |
| ``` |
| +-----------------------------+-----------------------------+ |
| | Time|root.ln.wf01.wt01.temperature| |
| +-----------------------------+-----------------------------+ |
| |2017-11-01T00:06:00.000+08:00| 20.71| |
| |2017-11-01T00:07:00.000+08:00| 21.45| |
| |2017-11-01T00:08:00.000+08:00| 22.58| |
| |2017-11-01T00:09:00.000+08:00| 20.98| |
| |2017-11-01T00:10:00.000+08:00| 25.52| |
| |2017-11-01T00:11:00.000+08:00| 22.91| |
| +-----------------------------+-----------------------------+ |
| Total line number = 6 |
| It costs 0.000s |
| ``` |
| |
| - **示例 2:** 带 `SOFFSET` 的 `SLIMIT` 子句 |
| |
| SQL 语句: |
| |
| ```sql |
| select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 |
| ``` |
| |
| 含义: |
| |
| 所选设备为 ln 组 wf01 工厂 wt01 设备; 所选时间序列是该设备下的第一列,即电源状态。 SQL 语句要求在" 2017-11-01T00:05:00.000"和"2017-11-01T00:12:00.000"的时间点之间选择状态传感器值。 |
| |
| 结果如下所示: |
| |
| ``` |
| +-----------------------------+------------------------+ |
| | Time|root.ln.wf01.wt01.status| |
| +-----------------------------+------------------------+ |
| |2017-11-01T00:06:00.000+08:00| false| |
| |2017-11-01T00:07:00.000+08:00| false| |
| |2017-11-01T00:08:00.000+08:00| false| |
| |2017-11-01T00:09:00.000+08:00| false| |
| |2017-11-01T00:10:00.000+08:00| true| |
| |2017-11-01T00:11:00.000+08:00| false| |
| +-----------------------------+------------------------+ |
| Total line number = 6 |
| It costs 0.003s |
| ``` |
| |
| - **示例 3:** `SLIMIT` 子句与 `GROUP BY` 子句结合 |
| |
| SQL 语句: |
| |
| ```sql |
| select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 |
| ``` |
| |
| 含义: |
| |
| ``` |
| +-----------------------------+-----------------------------------+ |
| | Time|max_value(root.ln.wf01.wt01.status)| |
| +-----------------------------+-----------------------------------+ |
| |2017-11-01T00:00:00.000+08:00| true| |
| |2017-11-02T00:00:00.000+08:00| true| |
| |2017-11-03T00:00:00.000+08:00| true| |
| |2017-11-04T00:00:00.000+08:00| true| |
| |2017-11-05T00:00:00.000+08:00| true| |
| |2017-11-06T00:00:00.000+08:00| true| |
| |2017-11-07T00:00:00.000+08:00| true| |
| +-----------------------------+-----------------------------------+ |
| Total line number = 7 |
| It costs 0.000s |
| ``` |
| |
| - **示例 4:** `SLIMIT` 子句与 `LIMIT` 子句结合 |
| |
| SQL 语句: |
| |
| ```sql |
| select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 |
| ``` |
| |
| 含义: |
| |
| 所选设备为 ln 组 wf01 工厂 wt01 设备; 所选时间序列是此设备下的第 0 列至第 1 列(第一列编号为第 0 列)。 SQL 语句子句要求返回查询结果的第 100 至 109 行(第一行编号为 0 行)。 |
| |
| 结果如下所示: |
| |
| ``` |
| +-----------------------------+-----------------------------+------------------------+ |
| | Time|root.ln.wf01.wt01.temperature|root.ln.wf01.wt01.status| |
| +-----------------------------+-----------------------------+------------------------+ |
| |2017-11-01T01:40:00.000+08:00| 21.19| false| |
| |2017-11-01T01:41:00.000+08:00| 22.79| false| |
| |2017-11-01T01:42:00.000+08:00| 22.98| false| |
| |2017-11-01T01:43:00.000+08:00| 21.52| false| |
| |2017-11-01T01:44:00.000+08:00| 23.45| true| |
| |2017-11-01T01:45:00.000+08:00| 24.06| true| |
| |2017-11-01T01:46:00.000+08:00| 22.6| false| |
| |2017-11-01T01:47:00.000+08:00| 23.78| true| |
| |2017-11-01T01:48:00.000+08:00| 24.72| true| |
| |2017-11-01T01:49:00.000+08:00| 24.68| true| |
| +-----------------------------+-----------------------------+------------------------+ |
| Total line number = 10 |
| It costs 0.009s |
| ``` |