Group by (a,b,c) with rollup;
表示给定的表达式列表及其所有前缀(包括空列表),等效于:
Group by (a,b,c) union all
Group by (a,b) union all
Group by (a) union all
Group by ();
Group by (a,b,c) with cube;
表示给定表达式列表的所有组合,等效于:
Group by (a,b,c) union all
Group by (a,b) union all
Group by (a,c) union all
Group by (b,c) union all
Group by (a) union all
Group by (b) union all
Group by (c) union all
Group by ();
Group by grouping sets((a,b),(b,c),());
表示给定表达式列表的分组,等效于:
Group by (a,b) union all
Group by (b,c) union all
Group by ();
设计一个表,表格式如下:
向表中插入下列数据
我们可以使用下面的语句分组查询总收益:
select year,country,product,sum(profit)
from sales
group by year,country,product;
当我们想知道某一层次的小计时,比如某年某国所有产品的收益,可以使用ROLL UP
select year,country,product,sum(profit)
from sales
group by year,country,product with rollup;
但ROLL UP只展示所选列某个层次,当我们想知道所选列全部组合时,就可以使用CUBE
select year,country,product,sum(profit)
from sales
group by year,country,product with cube;
我们有时也需要自己选择想要查询的列,此时可以使用grouping sets
select year,country, product,sum(profit)
from sales
group by grouping sets ((year,country),(year,product),());