postgresql中的CUBE函数

数据

  1. --复杂统计函数
  2. CREATE TABLE t3 (color_type varchar(20), in_date varchar(30),color_count int);
  3. INSERT INTO t3 (color_type,in_date,color_count) VALUES('black','2010-09-11',18);
  4. INSERT INTO t3 (color_type,in_date,color_count) VALUES('black','2010-10-05',18);
  5. INSERT INTO t3 (color_type,in_date,color_count) VALUES('black','2010-10-13',31);
  6. INSERT INTO t3 (color_type,in_date,color_count) VALUES('blue','2010-09-21',23);
  7. INSERT INTO t3 (color_type,in_date,color_count) VALUES('blue','2010-09-30',15);
  8. INSERT INTO t3 (color_type,in_date,color_count) VALUES('blue','2010-10-11',62);
  9. INSERT INTO t3 (color_type,in_date,color_count) VALUES('red','2010-09-12',41);
  10. INSERT INTO t3 (color_type,in_date,color_count) VALUES('red','2010-10-01',12);
  11. INSERT INTO t3 (color_type,in_date,color_count) VALUES('red','2010-10-05',11);

函数简介

CUBE(a, b, c):和grouping sets((a,b,c),(a,b),(a,c),(a),(b,c),(b),(c),())是等效的

如下所示:

其中:

  • black共计67,
  • blue共计100
  • red共计64
  • 所有共计231

添加汇总额外信息

如下所示,添加了总计、小计等信息:

 

原文地址:https://www.cnblogs.com/bigbigtree/p/6212834.html