全部产品
Search
文档中心

云原生数据仓库AnalyticDB:GROUP BY

更新时间:Sep 24, 2024

您可以使用GROUP BY子句根据指定的列对查询结果进行分析,也可以在GROUP BY子句中使用GROUPING SETSCUBEROLLUP,以不同的形式展示分组结果。

GROUP BY expression [, ...]    

注意事项

  • 查询中需使用标准聚合函数(SUMAVGCOUNT)声明非分组列,若未声明,则会使用ARBITRARY函数声明非分组列。

  • GROUP BY中的列或表达式列表必须与查询列表中的非聚合表达式的列相同。

GROUPING SETS

GROUPING SETS用于在同一结果集中指定多个GROUP BY选项,作用相当于多个GROUP BY查询的UNION组合形式。

SELECT origin_state, origin_zip, destination_state, sum(package_weight)
FROM shipping
GROUP BY GROUPING SETS (
    (origin_state),
    (origin_state, origin_zip),
    (destination_state));         

上述示例等同于:

SELECT origin_state, NULL, NULL, sum(package_weight)
FROM shipping GROUP BY origin_state
UNION ALL
SELECT origin_state, origin_zip, NULL, sum(package_weight)
FROM shipping GROUP BY origin_state, origin_zip
UNION ALL
SELECT NULL, NULL, destination_state, sum(package_weight)
FROM shipping GROUP BY destination_state;       

CUBE

CUBE用于列出所有可能的分组集。

SELECT origin_state, destination_state, sum(package_weight)
FROM shipping
GROUP BY origin_state, destination_state WITH CUBE    

上述示例等同于:

SELECT origin_state, destination_state, sum(package_weight)
FROM shipping
GROUP BY GROUPING SETS (
    (origin_state, destination_state),
    (origin_state),
    (destination_state),
    ())        

ROLLUP

ROLLUP可以以层级的方式列出分组集。

重要

AnalyticDB for MySQL不支持GROUP BY ROLLUP ()后加列名。如果您想以层级方式实现GROUP BY查询的UNION组合,可以通过GROUP BY GROUPING SETS ((column1, column2), (column1), ()) ,column3;实现。

SELECT origin_state, origin_zip, SUM(package_weight)
FROM shipping
GROUP BY ROLLUP (origin_state, origin_zip)          

上述示例等同于:

SELECT origin_state, origin_zip, sum(package_weight)
FROM shipping
GROUP BY GROUPING SETS ((origin_state, origin_zip), (origin_state), ())          

示例

以下查询列表中包含两个聚合表达式,第一个聚合表达式使用SUM函数,第二个聚合表达式使用COUNT函数,其余两列(LISTIDEVENTID)声明为分组列。

SELECT listid, eventid, SUM(pricepaid) as revenue,
count(qtysold) as numtix
FROM sales
GROUP BY listid, eventid
ORDER BY 3, 4, 2, 1
LIMIT 5;

返回结果如下:

listid | eventid | revenue | numtix
-------+---------+---------+--------
89397  |      47 |   20.00 |      1
106590 |      76 |   20.00 |      1
124683 |     393 |   20.00 |      1
103037 |     403 |   20.00 |      1
147685 |     429 |   20.00 |      1
(5 rows)         

GROUP BY子句中的表达式也可以使用序号来引用所需的列。上述示例可改写为以下形式。

SELECT listid, eventid, SUM(pricepaid) as revenue,
count(qtysold) as numtix
FROM sales
GROUP BY 1,2
ORDER BY 3, 4, 2, 1
LIMIT 5;       

返回结果如下:

listid | eventid | revenue | numtix
-------+---------+---------+--------
89397  |      47 |   20.00 |      1
106590 |      76 |   20.00 |      1
124683 |     393 |   20.00 |      1
103037 |     403 |   20.00 |      1
147685 |     429 |   20.00 |      1