Impala - Having 子句



Impala 中的 Having 子句使您可以指定条件,以筛选最终结果中显示的组结果。

通常,Having 子句与 group by 子句一起使用,它对 GROUP BY 子句所创建的组设置条件。

语法

以下是 Having 子句的语法。

select * from table_name ORDER BY col_name [ASC|DESC] [NULLS FIRST|NULLS LAST]

示例

假设我们在数据库 my_db 中有一张名为 customers 的表,其内容如下:

[quickstart.cloudera:21000] > select * from customers; 
Query: select * from customers 
+----+----------+-----+-------------+--------+ 
| 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  | 
| 5  | Hardik   | 27  | Bhopal      | 40000  | 
| 6  | Komal    | 22  | MP          | 32000  | 
| 7  | ram      | 25  | chennai     | 23000  | 
| 8  | rahim    | 22  | vizag       | 31000  | 
| 9  | robert   | 23  | banglore    | 28000  | 
+----+----------+-----+-----------+--------+ 
Fetched 9 row(s) in 0.51s

以下是 Impala 中使用 Having 子句的一个示例:

[quickstart.cloudera:21000] > select max(salary) from customers group by age having max(salary) > 20000;

此查询最初按年龄对表进行分组,然后选择每组的最大薪水,并显示如下所示大于 20000 的薪水。

20000 
+-------------+ 
| max(salary) |
+-------------+ 
| 30000       |
| 35000       | 
| 40000       | 
| 32000       | 
+-------------+ 
Fetched 4 row(s) in 1.30s
广告
© . All rights reserved.