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)

 

```

posted @ 2020-03-18 16:12  mangoCzp  阅读(1445)  评论(0编辑  收藏  举报