高级聚合
---------------
GROUPING SETS.
group by + union all
//查询每个cust的订单数
select count(*) from orders group by cid ;
select count(*) from orders group by orderno ;
//group + union all
select count(*) from orders group by cid union all select count(*) from orders group by orderno ;
//group by :指定按照哪些字段分组,
//grouping sets : 以上字段集如何组合。
select count(*) from orders group by cid,orderno grouping sets(cid,orderno,())
rollup
-------------
rollup扩展了group by,
rollup比grouping sets多了一层聚合(n + 1)。
GROUP BY a,b,c WITH ROLLUP
GROUP BY a,b,c GROUPING SETS ((a,b,c),(a,b),(a),())
select cid,orderno,count(*) GROUP BY cid,orderno GROUPING SETS ((cid,orderno),(cid),())
cube
-------------
扩展了grouping sets,做了各种条件的组合,不做排序。
等价于
GROUP BY a,b,c GROUPING SETS ((a,b,c),(a,b),(b,c),(a,c),(a),(b),(c),())
聚合条件
-------------
having,用于在组内过滤。
//使用having
select cid , max(price) mx from orders group by cid having mx > 100.1 ;
//嵌套子查询
select t.cid , t.mx from (select cid , max(price) mx from orders group by cid) t where t.mx > 100.1 ;