相关文章推荐
侠义非凡的椰子  ·  arch/x86/makefile:184: ...·  2 周前    · 
粗眉毛的柑橘  ·  android - Read NFC ...·  1 年前    · 

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

GROUP BY expression [, ...]    

注意事项

  • 查询中需使用标准聚合函数( SUM AVG COUNT )声明非分组列,若未声明,则会使用 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 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 函数,其余两列( LISTID EVENTID )声明为分组列。

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