Impala 简明教程

Impala - Offset Clause

通常情况下, select 查询结果集中的行从 0 开始。使用 offset 子句,我们可以决定从何处开始考虑输出。例如,如果我们选择偏移量为 0,则结果将如常一样,如果我们选择偏移量为 5,则结果将从第五行开始。

offset clause

Syntax

以下是 Impala 中 offset 子句的语法。

select data from table_name Group BY col_name;

Example

假设数据库 my_db 中有一个名为 customers 的表,其内容如下 −

[quickstart.cloudera:21000] > select * from customers;
Query: select * from customers
+----+----------+-----+-----------+--------+
| id | name     | age | address   | salary |
+----+----------+-----+-----------+--------+
| 3  | kaushik  | 23  | Kota      | 30000  |
| 6  | Komal    | 22  | MP        | 32000  |
| 1  | Ramesh   | 32  | Ahmedabad | 20000  |
| 5  | Hardik   | 27  | Bhopal    | 40000  |
| 2  | Khilan   | 25  | Delhi     | 15000  |
| 8  | ram      | 22  | vizag     | 31000  |
| 9  | robert   | 23  | banglore  | 28000  |
| 7  | ram      | 25  | chennai   | 23000  |
| 4  | Chaitali | 25  | Mumbai    | 35000  |
+----+----------+-----+-----------+--------+
Fetched 9 row(s) in 0.51s

您可以按照 ID 的升序排列表中的记录,并使用 limitorder by 子句将记录数限制为 4,如下所示。

Query: select * from customers order by id limit 4
+----+----------+-----+-----------+--------+
| id | name     | age | address   | salary |
+----+----------+-----+-----------+--------+
| 1  | Ramesh   | 32  | Ahmedabad | 20000  |
| 2  | Khilan   | 25  | Delhi     | 15000  |
| 3  | kaushik  | 23  | Kota      | 30000  |
| 4  | Chaitali | 25  | Mumbai    | 35000  |
+----+----------+-----+-----------+--------+
Fetched 4 row(s) in 0.64s

以下是 offset 子句的一个示例。在此,我们按 ID 顺序获取 customers 表中的记录,并从第 0 行开始打印前四行。

[quickstart.cloudera:21000] > select * from customers order by id limit 4 offset 0;

执行后,上述查询将给出以下结果。

Query: select * from customers order by id limit 4 offset 0
+----+----------+-----+-----------+--------+
| id | name     | age | address   | salary |
+----+----------+-----+-----------+--------+
| 1  | Ramesh   | 32  | Ahmedabad | 20000  |
| 2  | Khilan   | 25  | Delhi     | 15000  |
| 3  | kaushik  | 23  | Kota      | 30000  |
| 4  | Chaitali | 25  | Mumbai    | 35000  |
+----+----------+-----+-----------+--------+
Fetched 4 row(s) in 0.62s

以同样的方式,您可以从偏移量为 5 的行开始获取 customers 表中的四条记录,如下所示。

[quickstart.cloudera:21000] > select * from customers order by id limit 4 offset 5;
Query: select * from customers order by id limit 4 offset 5
+----+--------+-----+----------+--------+
| id | name   | age | address  | salary |
+----+--------+-----+----------+--------+
| 6  | Komal  | 22  | MP       | 32000  |
| 7  | ram    | 25  | chennai  | 23000  |
| 8  | ram    | 22  | vizag    | 31000  |
| 9  | robert | 23  | banglore | 28000  |
+----+--------+-----+----------+--------+
Fetched 4 row(s) in 0.52s