欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

Oracle分析函数/聚合函数使用总结 .

程序员文章站 2022-06-17 16:41:24
...

总结: group by rollup(field1,field2); group by cube(field1,field2); group by grouping sets(field1,field2); 生成测试脚本: [c-sharp:nogutter:collapse:showcolumns] view plaincopyprint? ·········10········20········30

总结:

 group by rollup(field1,field2); 

group by cube(field1,field2); 

group by grouping sets(field1,field2);

生成测试脚本:

[c-sharp:nogutter:collapse:showcolumns] view plaincopyprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. SQL> CREATE TABLE Bill
  2. 2 (Bill_Month VARCHAR2(6),
  3. 3 Area_Code INTEGER,
  4. 4 Net_Type CHAR(1),
  5. 5 Local_Fare NUMBER(10,2))

插入测试数据:

[c-sharp:nogutter:collapse:showcolumns] view plaincopyprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200405',5761,'J',5667089.85 );
  2. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200405',5762,'G',6315075.96 );
  3. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200405',5762,'J',6328716.15 );
  4. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200405',5763,'G',8861742.59 );
  5. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200405',5763,'J',7788036.32 );
  6. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200405',5764,'G',6028670.45 );
  7. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200405',5764,'J',6459121.49 );
  8. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200405',5765,'G',13156065.77);
  9. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200405',5765,'J',11901671.70);
  10. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200406',5761,'G',7614587.96 );
  11. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200406',5761,'J',5704343.05 );
  12. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200406',5762,'G',6556992.60 );
  13. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200406',5762,'J',6238068.05 );
  14. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200406',5763,'G',9130055.46 );
  15. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200406',5763,'J',7990460.25 );
  16. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200406',5764,'G',6387706.01 );
  17. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200406',5764,'J',6907481.66 );
  18. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200406',5765,'G',13562968.81);
  19. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200406',5765,'J',12495492.50);
  20. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200407',5761,'G',7987050.65 );
  21. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200407',5761,'J',5723215.28 );
  22. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200407',5762,'G',6833096.68 );
  23. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200407',5762,'J',6391201.44 );
  24. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200407',5763,'G',9410815.91 );
  25. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200407',5763,'J',8076677.41 );
  26. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200407',5764,'G',6456433.23 );
  27. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200407',5764,'J',6987660.53 );
  28. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200407',5765,'G',14000101.20);
  29. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200407',5765,'J',12301780.20);
  30. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200408',5761,'G',8085170.84 );
  31. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200408',5761,'J',6050611.37 );
  32. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200408',5762,'G',6854584.22 );
  33. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200408',5762,'J',6521884.50 );
  34. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200408',5763,'G',9468707.65 );
  35. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200408',5763,'J',8460049.43 );
  36. insert into Bill (Bill_Month,Area_Code,Net_Type,Local_Fare) values('200408',5764,'G',6587559.23 );

1.ROLLUP

ROLLUP是对group by的扩展,因此,它只能出现在group by子句中,依赖于分组的列,对每个分组会生成汇总数据,如下:
SELECT ….
FROM ….
GROUP BY ROLLUP(C1,C2,C3….C(n-1),C(n));
总共会进行n+1个分组,那么实际上有n+1个group by的union all结果。
第1个分组:全分组。C1,C2,C3….C(n-1),C(n)
第2个分组:C1,C2,C3….C(n-1);//这个分组实际上就是对前面前n-1列分组的小计.
----然后逐渐递减分组列
第n个分组:C1。对上一个分组的小计。
第n+1个分组。不分组全量汇总,相当于合计,也是对group by C1的小计,相当于group by null。

[c-sharp:nogutter:collapse:showcolumns] view plaincopyprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. SELECT NVL(TO_CHAR(AREA_CODE), '总计') AREA_CODE,
  2. SUM(LOCAL_FARE) LOCAL_FARE
  3. FROM BILL
  4. GROUP BY ROLLUP(AREA_CODE)

--result

[c-sharp:nogutter:collapse:showcolumns] view plaincopyprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. AREA_CODE LOCAL_FARE
  2. 5764 45814632.6
  3. 5761 54225413.04
  4. 5762 52039619.6
  5. 5763 69186545.02
  6. 5765 77418080.18
  7. 合计 298684290.44

2.cube

CUBE(交叉列表)也是对group by运算的一种扩展,它比rollup扩展更加精细,组合类型更多,rollup是按组合的列从右到左递减分组计算,而CUBE则是对所有可能的组合情况进行分组,这样分组的情况更多,覆盖所有的可能分组,并计算所有可能的分组的小计。比如:
CUBE(C1,C2,C3……C(N))对N个列进行CUBE分组,那么可能的分组情况有:
不分组:C(n,0)
取一列分组:C(n,1)
-----
取N列分组,全分组:C(n,n)
那么运用数学上的组合公式,得出所有所有可能的组合方式有:C(n,0)+C(n,1)+….+C(n,n)=2^n种。
我们以前面的rollup组合列为例子:rollup(name,month)是计算按区域名和月份分组以及每个区域的所有月份的小计以及总计。但是使用 cube(name,month)则有4种分组,比rollup多一个每个月的所有区域的小计。下面比较一下这两种分组方式:

分组公式 描述
rollup(name,month) 分组情况有:
group by name,month
group by name,null //每个区域所有月份小计
group by null,null //合计
cube(name,month) 分组情况有:
group by null,null //总计
group by null,month //每个月份的所有区域小计
group by name,null //每个区域的所有月份小计
group by name,month

CUBE使用方式:
和rollup一样,是
select …
from …
group by cube(分组列列表)

[c-sharp:nogutter:collapse:showcolumns] view plaincopyprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. SELECT (NVL(BILL_MONTH, '月份')) BILL_MONTH,
  2. (TO_CHAR(AREA_CODE)) AREA_CODE,
  3. SUM(LOCAL_FARE) LOCAL_FARE
  4. FROM BILL
  5. GROUP BY CUBE(AREA_CODE, BILL_MONTH)
  6. ORDER BY BILL_MONTH, AREA_CODE

--result

[c-sharp:nogutter:collapse:showcolumns] view plaincopyprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. BILL_MONTH AREA_CODE LOCAL_FARE
  2. 200405 5761 13060433.89
  3. 200405 5762 12643792.11
  4. 200405 5763 16649778.91
  5. 200405 5764 12487791.94
  6. 200405 5765 25057737.47
  7. 200405 79899534.32
  8. 200406 5761 13318931.01
  9. 200406 5762 12795060.65
  10. 200406 5763 17120515.71
  11. 200406 5764 13295187.67
  12. 200406 5765 26058461.31
  13. 200406 82588156.35
  14. 200407 5761 13710265.93
  15. 200407 5762 13224298.12
  16. 200407 5763 17487493.32
  17. 200407 5764 13444093.76
  18. 200407 5765 26301881.4
  19. 200407 84168032.53
  20. 200408 5761 14135782.21
  21. 200408 5762 13376468.72
  22. 200408 5763 17928757.08
  23. 200408 5764 6587559.23
  24. 200408 52028567.24
  25. 月份 5761 54225413.04
  26. 月份 5762 52039619.6
  27. 月份 5763 69186545.02
  28. 月份 5764 45814632.6
  29. 月份 5765 77418080.18
  30. 月份 298684290.44

扩展一下,GROUPING函数

[c-sharp:nogutter:collapse:showcolumns] view plaincopyprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. SELECT DECODE(GROUPING(AREA_CODE), 1, 'all area', TO_CHAR(AREA_CODE)) AREA_CODE,
  2. DECODE(GROUPING(BILL_MONTH), 1, 'all month', BILL_MONTH) BILL_MONTH,
  3. SUM(LOCAL_FARE) LOCAL_FARE
  4. FROM bill
  5. GROUP BY CUBE(AREA_CODE, BILL_MONTH)
  6. ORDER BY AREA_CODE, BILL_MONTH NULLS LAST

--Result

[c-sharp:nogutter:collapse:showcolumns] view plaincopyprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. 5761 200405 13060433.89
  2. 5761 200406 13318931.01
  3. 5761 200407 13710265.93
  4. 5761 200408 14135782.21
  5. 5761 all month 54225413.04
  6. 5762 200405 12643792.11
  7. 5762 200406 12795060.65
  8. 5762 200407 13224298.12
  9. 5762 200408 13376468.72
  10. 5762 all month 52039619.6
  11. 5763 200405 16649778.91
  12. 5763 200406 17120515.71
  13. 5763 200407 17487493.32
  14. 5763 200408 17928757.08
  15. 5763 all month 69186545.02
  16. 5764 200405 12487791.94
  17. 5764 200406 13295187.67
  18. 5764 200407 13444093.76
  19. 5764 200408 6587559.23
  20. 5764 all month 45814632.6
  21. 5765 200405 25057737.47
  22. 5765 200406 26058461.31
  23. 5765 200407 26301881.4
  24. 5765 all month 77418080.18
  25. all area 200405 79899534.32
  26. all area 200406 82588156.35
  27. all area 200407 84168032.53
  28. all area 200408 52028567.24
  29. all area all month 298684290.44

以上我们已经掌握了rollup,cube分组统计的知识。但是rollup和cube的分组统计包含了常规group by的统计明细以及对相关列的小计和合计值。如果我们需要的只是按每个分组列小计呢?oracle提供了grouping sets操作,对group by的另一个扩展,专门对分组列分别进行小计计算,不包括合计。使用方式和rollup和cube一样,都是放在group by中。如:
grouping sets(C1,C2….Cn)则分组方式有n种,等于列的数目。
group by c1,null,null…..null。
group by null,c2,null….null。
….
group by null,null……..Cn。
无group by null,null….null,也就是说没有合计行。
注意:grouping sets的统计结果和列的顺序无关。

有时我们只需要月、地区统计结果:

[c-sharp:nogutter:collapse:showcolumns] view plaincopyprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. SELECT DECODE(GROUPING(AREA_CODE), 1, 'all area', TO_CHAR(AREA_CODE)) AREA_CODE,
  2. DECODE(GROUPING(BILL_MONTH), 1, 'all month', BILL_MONTH) BILL_MONTH,
  3. SUM(LOCAL_FARE) LOCAL_FARE
  4. FROM BILL
  5. GROUP BY GROUPING SETS(AREA_CODE, BILL_MONTH);

--result

[c-sharp:nogutter:collapse:showcolumns] view plaincopyprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  1. AREA_CODE BILL_MONTH LOCAL_FARE
  2. 5764 all month 45814632.6
  3. 5761 all month 54225413.04
  4. 5762 all month 52039619.6
  5. 5763 all month 69186545.02
  6. 5765 all month 77418080.18
  7. all area 200405 79899534.32
  8. all area 200406 82588156.35
  9. all area 200407 84168032.53
  10. all area 200408 52028567.24

3.ROWS

rows是物理行,就是按行的位置,根据位置计算窗口范围

sql query

[c-sharp:collapse] view plaincopyprint?

  1. select bill_month,area_code,net_type,local_fare,sum(local_fare) over(order by local_fare rows between current row and 1 following) sum_fare
  2. from bill
  3. /
  4. BILL_M AREA_CODE N LOCAL_FARE SUM_FARE
  5. ------ ---------- - ---------- ----------
  6. 200405 5761 J 5667089.85 11371432.9
  7. 200406 5761 J 5704343.05 11733013.5
  8. 200405 5764 G 6028670.45 12266738.5
  9. 200406 5762 J 6238068.05 12625774.1
  10. 200406 5764 G 6387706.01 12846827.5
  11. 200405 5764 J 6459121.49 13016114.1
  12. 200406 5762 G 6556992.6 13464474.3
  13. 200406 5764 J 6907481.66 14522069.6
  14. 200406 5761 G 7614587.96 15402624.3
  15. 200405 5763 J 7788036.32 15778496.6
  16. 200406 5763 J 7990460.25 16852202.8
  17. BILL_M AREA_CODE N LOCAL_FARE SUM_FARE
  18. ------ ---------- - ---------- ----------
  19. 200405 5763 G 8861742.59 17991798.1
  20. 200406 5763 G 9130055.46 9130055.46
  21. 13 rows selected.

4.RANGE

RANGE是逻辑行,是按单元格值和偏移量计算窗口范围.

Range是逻辑行的范围 ,要经过 计算 的,一般range后面是数值或时间间隔等,这样根据 当行和range的表达 式能计算当

行对应的窗口范围;

[c-sharp:collapse] view plaincopyprint?

  1. select bill_month,area_code,net_type,local_fare,sum(local_fare) over(order by local_fare range between current row and 122350 following) sum_fare
  2. from bill
  3. /
  4. BILL_M AREA_CODE N LOCAL_FARE SUM_FARE
  5. ------ ---------- - ---------- ----------
  6. 200405 5761 J 5667089.85 11371432.9
  7. 200406 5761 J 5704343.05 5704343.05
  8. 200405 5764 G 6028670.45 6028670.45
  9. 200406 5762 J 6238068.05 6238068.05
  10. 200406 5764 G 6387706.01 12846827.5
  11. 200405 5764 J 6459121.49 13016114.1
  12. 200406 5762 G 6556992.6 6556992.6
  13. 200406 5764 J 6907481.66 6907481.66
  14. 200406 5761 G 7614587.96 7614587.96
  15. 200405 5763 J 7788036.32 7788036.32
  16. 200406 5763 J 7990460.25 7990460.25
  17. BILL_M AREA_CODE N LOCAL_FARE SUM_FARE
  18. ------ ---------- - ---------- ----------
  19. 200405 5763 G 8861742.59 8861742.59
  20. 200406 5763 G 9130055.46 9130055.46
  21. 13 rows selected.

5.RATIO_TO_REPORT

The RATIO_TO_REPORT function computes the ratio of a value to the sum of a set of values. If the expression value expression evaluates to NULL , RATIO_TO_REPORT also evaluates to NULL , but it is treated as zero for computing the sum of values for the denominator. Its syntax is:

RATIO_TO_REPORT ( expr ) OVER ( [query_partition_clause] )
 

[c-sharp:collapse] view plaincopyprint?

  1. select bill_month,area_code,net_type,local_fare,ratio_to_report(local_fare) over() rate
  2. from bill
  3. /
  4. BILL_M AREA_CODE N LOCAL_FARE RATE
  5. ------ ---------- - ---------- ----------
  6. 200405 5761 J 5667089.85 .062047734
  7. 200405 5763 G 8861742.59 .097025293
  8. 200405 5763 J 7788036.32 .085269516
  9. 200405 5764 G 6028670.45 .066006602
  10. 200405 5764 J 6459121.49 .070719517
  11. 200406 5761 G 7614587.96 .083370468
  12. 200406 5761 J 5704343.05 .062455612
  13. 200406 5762 G 6556992.6 .071791086
  14. 200406 5762 J 6238068.05 .068299251
  15. 200406 5763 G 9130055.46 .099962992
  16. 200406 5763 J 7990460.25 .087485812
  17. BILL_M AREA_CODE N LOCAL_FARE RATE
  18. ------ ---------- - ---------- ----------
  19. 200406 5764 G 6387706.01 .069937604
  20. 200406 5764 J 6907481.66 .075628515