PostgreSQL 分组集合新功能(GROUPING SETS,CUBE,ROLLUP)
https://blog.csdn.net/sunbocong/article/details/79097713
My test :
```
postgres=# select * from t1;
a | b | c | d
---+---+---+---
1 | 1 | 2 | 1
2 | 2 | 1 | 2
1 | 1 | 1 | 3
(3 rows)
postgres=# select a,b,c,sum(d) from t1 group by a,b,c ;
a | b | c | sum
---+---+---+-----
1 | 1 | 1 | 3
2 | 2 | 1 | 2
1 | 1 | 2 | 1
(3 rows)
postgres=# select a,b,c,sum(d) from t1 group by rollup(a,b,c) ;
a | b | c | sum
---+---+---+-----
| | | 6
1 | 1 | 1 | 3
2 | 2 | 1 | 2
1 | 1 | 2 | 1
1 | 1 | | 4
2 | 2 | | 2
2 | | | 2
1 | | | 4
(8 rows)
postgres=# select a,b,c,sum(d) from t1 group by cube(a,b,c) ;
a | b | c | sum
---+---+---+-----
| | | 6
1 | 1 | 1 | 3
2 | 2 | 1 | 2
1 | 1 | 2 | 1
1 | 1 | | 4
2 | 2 | | 2
2 | | | 2
1 | | | 4
| 1 | 1 | 3
| 2 | 1 | 2
| 1 | 2 | 1
| 2 | | 2
| 1 | | 4
1 | | 1 | 3
1 | | 2 | 1
2 | | 1 | 2
| | 2 | 1
| | 1 | 5
(18 rows)
```