【问题标题】:Multiple aggregations and group by in single query单个查询中的多个聚合和分组
【发布时间】:2020-05-15 10:10:28
【问题描述】:

我有一个大致如下结构的 SQL 表:

Employee| date | department | Country | Designation

我希望得到具有以下结构的结果:

count_emp_per_department | count_emp_per_country | count_emp_per_designation |

目前我正在使用UNION ALL,它正在构建一个类似的查询:

SELECT emp_ID, NULL, count(1)
FROM employee
GROUP BY country
UNION ALL
SELECT NULL, emp_ID, count(1)
FROM film
GROUP BY designation

这是在 Hive 中执行多个聚合并将所有聚合返回到单个结果集中的最有效方法吗?

如果您有可以优化/提高性能的新方法,请分享。

【问题讨论】:

  • 选择 ONE DBMS,并删除不相关的标签。如果这是 MySQL - 指定它的版本。
  • 如果没有这个“东西”,每个东西(不完全)的员工数量是没有意义的。
  • @Akina 我标记了 MySQL 以获得更大的影响力,因为 HiveQL 与 MySQL 非常相似。
  • HiveQL 支持 CTEwindow functions - 使用它们。
  • 感谢您的回复,我的第一种方法只是 CTE,您能否分享我如何在我的场景中使用窗口功能,我很想通过一个例子来学习这一点。提前致谢。

标签: mysql hive hiveql


【解决方案1】:

不确定它是否是一个真正的需求..因为输出没有那么有用..无论如何

这是结构和查询。

+-----------+------------+----------+
| col_name  | data_type  | comment  |
+-----------+------------+----------+
| emp       | int        |          |
| dt        | date       |          |
| dept      | string     |          |
| country   | string     |          |
| desig     | string     |          |
+-----------+------------+----------+

+--------+-------------+---------+------------+----------+
| t.emp  |    t.dt     | t.dept  | t.country  | t.desig  |
+--------+-------------+---------+------------+----------+
| 1      | 2020-02-02  | human   | usa        | hr       |
| 2      | 2020-02-02  | dir     | usa        | hr       |
| 3      | 2020-02-02  | dir     | canada     | it       |
+--------+-------------+---------+------------+----------+


with q1 as (select dept,count(*) as deptcount from t group by dept),
q2 as (select country,count(*) as countrycount from t group by country),
q3 as (select desig,count(*) as desigcount from t group by desig)
select *  from q1, q2, q3;

输出会是这样的..

+----------+---------------+-------------+------------------+-----------+----------------+
| q1.dept  | q1.deptcount  | q2.country  | q2.countrycount  | q3.desig  | q3.desigcount  |
+----------+---------------+-------------+------------------+-----------+----------------+
| dir      | 2             | canada      | 1                | hr        | 2              |
| dir      | 2             | usa         | 2                | hr        | 2              |
| dir      | 2             | canada      | 1                | it        | 1              |
| dir      | 2             | usa         | 2                | it        | 1              |
| human    | 1             | canada      | 1                | hr        | 2              |
| human    | 1             | usa         | 2                | hr        | 2              |
| human    | 1             | canada      | 1                | it        | 1              |
| human    | 1             | usa         | 2                | it        | 1              |
+----------+---------------+-------------+------------------+-----------+----------------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多