search
HomeDatabaseMysql TutorialOracle分析函数/聚合函数使用总结 .
Oracle分析函数/聚合函数使用总结 .Jun 07, 2016 pm 03:32 PM
oracleusefunctionanalyzeSummarizepolymerization

总结: 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  
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
什么是oracle asm什么是oracle asmApr 18, 2022 pm 04:16 PM

oracle asm指的是“自动存储管理”,是一种卷管理器,可自动管理磁盘组并提供有效的数据冗余功能;它是做为单独的Oracle实例实施和部署。asm的优势:1、配置简单、可最大化推动数据库合并的存储资源利用;2、支持BIGFILE文件等。

oracle怎么查询所有索引oracle怎么查询所有索引May 13, 2022 pm 05:23 PM

方法:1、利用“select*from user_indexes where table_name=表名”语句查询表中索引;2、利用“select*from all_indexes where table_name=表名”语句查询所有索引。

Oracle怎么查询端口号Oracle怎么查询端口号May 13, 2022 am 10:10 AM

在Oracle中,可利用lsnrctl命令查询端口号,该命令是Oracle的监听命令;在启动、关闭或重启oracle监听器之前可使用该命令检查oracle监听器的状态,语法为“lsnrctl status”,结果PORT后的内容就是端口号。

oracle全角怎么转半角oracle全角怎么转半角May 13, 2022 pm 03:21 PM

在oracle中,可以利用“TO_SINGLE_BYTE(String)”将全角转换为半角;“TO_SINGLE_BYTE”函数可以将参数中所有多字节字符都替换为等价的单字节字符,只有当数据库字符集同时包含多字节和单字节字符的时候有效。

oracle怎么删除sequenceoracle怎么删除sequenceMay 13, 2022 pm 03:35 PM

在oracle中,可以利用“drop sequence sequence名”来删除sequence;sequence是自动增加数字序列的意思,也就是序列号,序列号自动增加不能重置,因此需要利用drop sequence语句来删除序列。

oracle怎么查询数据类型oracle怎么查询数据类型May 13, 2022 pm 04:19 PM

在oracle中,可以利用“select ... From all_tab_columns where table_name=upper('表名') AND owner=upper('数据库登录用户名');”语句查询数据库表的数据类型。

oracle查询怎么不区分大小写oracle查询怎么不区分大小写May 10, 2022 pm 05:45 PM

方法:1、利用“LOWER(字段值)”将字段转为小写,或者利用“UPPER(字段值)”将字段转为大写;2、利用“REGEXP_LIKE(字符串,正则表达式,'i')”,当参数设置为“i”时,说明进行匹配不区分大小写。

Oracle怎么修改sessionOracle怎么修改sessionMay 13, 2022 pm 05:06 PM

方法:1、利用“alter system set sessions=修改后的数值 scope=spfile”语句修改session参数;2、修改参数之后利用“shutdown immediate – startup”语句重启服务器即可生效。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.