- 特定于数据库的语句
- Impala - 创建数据库
- Impala - 删除数据库
- Impala - 选择数据库
- 特定于表的语句
- Impala - 创建表语句
- Impala - 插入语句
- Impala - 选择语句
- Impala - 描述语句
- Impala - 更改表
- Impala - 删除表
- Impala - 截断表
- Impala - 显示表
- Impala - 创建视图
- Impala - 更改视图
- Impala - 删除视图
- Impala - 子句
- Impala - 排序子句
- Impala - 分组条款
- Impala - 拥有子句
- Impala - 限制子句
- Impala - 偏移子句
- Impala - 并集子句
- Impala - With子句
- Impala - 区分运算符
- Impala 有用资源
- Impala - 快速指南
- Impala - 有用资源
- Impala - 讨论
Impala - 分组条款
Impala GROUP BY 子句配合 SELECT 语句一起使用,将相同数据组织到组中。
语法
以下是 GROUP BY 子句的语法。
select data from table_name Group BY col_name;
示例
假设数据库 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 | +----+----------+-----+-----------+--------+ Fetched 6 row(s) in 0.51s
可以使用如下所示的 GROUP BY 查询,获得每个客户的总薪水金额。
[quickstart.cloudera:21000] > Select name, sum(salary) from customers Group BY name;
执行上述查询后,会得到如下输出。
Query: select name, sum(salary) from customers Group BY name +----------+-------------+ | name | sum(salary) | +----------+-------------+ | Ramesh | 20000 | | Komal | 32000 | | Hardik | 40000 | | Khilan | 15000 | | Chaitali | 35000 | | kaushik | 30000 | +----------+-------------+ Fetched 6 row(s) in 1.75s
假设该表有多个如下所示的记录。
+----+----------+-----+-----------+--------+ | id | name | age | address | salary | +----+----------+-----+-----------+--------+ | 1 | Ramesh | 32 | Ahmedabad | 20000 | | 2 | Ramesh | 32 | Ahmedabad | 1000| | | 3 | Khilan | 25 | Delhi | 15000 | | 4 | kaushik | 23 | Kota | 30000 | | 5 | Chaitali | 25 | Mumbai | 35000 | | 6 | Chaitali | 25 | Mumbai | 2000 | | 7 | Hardik | 27 | Bhopal | 40000 | | 8 | Komal | 22 | MP | 32000 | +----+----------+-----+-----------+--------+
现在,仍可以使用如下所示的 Group By 子句,获取员工总薪水金额,同时考虑记录的重复条目。
Select name, sum(salary) from customers Group BY name;
执行上述查询后,会得到如下输出。
Query: select name, sum(salary) from customers Group BY name +----------+-------------+ | name | sum(salary) | +----------+-------------+ | Ramesh | 21000 | | Komal | 32000 | | Hardik | 40000 | | Khilan | 15000 | | Chaitali | 37000 | | kaushik | 30000 | +----------+-------------+ Fetched 6 row(s) in 1.75s
广告