group by
众所周知,group by 语法是对给定的字段进行分组统计,最后得到一个分组统计数据。
满足以下要求:select语句中的字段必须为分组字段或者聚合函数(如:sum() 、max()、min()、avg()、count()等),聚合后的过滤需要使用having而不是where(where是先过滤再聚合)
对于高维度和大数据量情况下,使用rollup,cube,grouping set 记录数会出现倍数的膨胀,可能会产生GC问题。此时可以考虑使用union all进行改写来避免GC问题
rollup
从右到左,依次递减
select a, b, c, count(1)
from table1
group by rollup(a, b, c)
-- 等价于下面的写法
select a, b, c, count(1)
from table1
group by a, b, c
union all
select a, b, null, count(1)
from table1
group by a, b
union all
select a, null, null, count(1)
from table1
group by a
union all
select null, null, null, count(1)
from table1
cube
cube是立方体的意思,即为各个字段间的交叉组合
ΣC(n,i)(0-n)
select a, b, c, count(1)
from table1
group by cube(a