ITPub博客

首页 > 数据库 > Oracle > sum()over()和count()over()分析函数

sum()over()和count()over()分析函数

Oracle 作者:韩云松 时间:2015-08-18 09:54:05 0 删除 编辑

Sum()over()和count()over分析函数使用

创建测试表

create table test(sales_id varchar2(2),sales varchar2(10),dest varchar2(10),dept varchar2(10),revenue number);

插入测试数据

insert into test values('11','smith','hangzhou','市场',1000);

insert into test values('12','smith','wenzhou','市场',2000);

insert into test values('13','allen','wenzhou','渠道',3000);

insert into test values('14','allen','wenzhou','渠道',4000);

insert into test values('15','jekch','shanghai','渠道',2500);

insert into test values('11','smith','hangzhou','市场',1000);

insert into test values('12','smith','wenzhou','市场',2000);

commit;

SQL> select * from test;

SALES_ID SALES DEST DEPT REVENUE

-------- ---------- ---------- ---------- ----------

11 smith hangzhou 市场 1000

12 smith wenzhou 市场 2000

13 allen wenzhou 渠道 3000

14 allen wenzhou 渠道 4000

15 jekch shanghai 渠道 2500

11 smith hangzhou 市场 1000

12 smith wenzhou 市场 2000

SQL> select sales_id,

2 sales,

3 dest,

4 dept,

5 revenue,

6 sum(revenue) over() as 总销售额

7 from test

8 ;

SALES_ID SALES DEST DEPT REVENUE 总销售额

-------- ---------- ---------- ---------- ---------- ----------

11 smith hangzhou 市场 1000 15500

12 smith wenzhou 市场 2000 15500

13 allen wenzhou 渠道 3000 15500

14 allen wenzhou 渠道 4000 15500

15 jekch shanghai 渠道 2500 15500

11 smith hangzhou 市场 1000 15500

12 smith wenzhou 市场 2000 15500

按照sales_id order by排序计算递加的销售总额

SQL> select sales_id,sales,dest,dept,revenue,sum(revenue)over(order by sales)递加销售总额 from test;

SALES_ID SALES DEST DEPT REVENUE 递加销售总额

-------- ---------- ---------- ---------- ---------- ------------

14 allen wenzhou 渠道 4000 7000

13 allen wenzhou 渠道 3000 7000

15 jekch shanghai 渠道 2500 9500

11 smith hangzhou 市场 1000 15500

12 smith wenzhou 市场 2000 15500

12 smith wenzhou 市场 2000 15500

11 smith hangzhou 市场 1000 15500

7 rows selected

SQL> select sales_id,sales,dest,dept,revenue,sum(revenue)over(partition by sales_id) 分组销售总额 from test;

SALES_ID SALES DEST DEPT REVENUE 分组销售总额

-------- ---------- ---------- ---------- ---------- ------------

11 smith hangzhou 市场 1000 2000

11 smith hangzhou 市场 1000 2000

12 smith wenzhou 市场 2000 4000

12 smith wenzhou 市场 2000 4000

13 allen wenzhou 渠道 3000 3000

14 allen wenzhou 渠道 4000 4000

15 jekch shanghai 渠道 2500 2500

对sales_id进行分组,然后分组求sum

SQL> select sales_id,sales,dest,dept,revenue,sum(revenue)over(partition by sales order by sales_id) 分组递加销售总额 from test;

SALES_ID SALES DEST DEPT REVENUE 分组递加销售总额

-------- ---------- ---------- ---------- ---------- ----------------

13 allen wenzhou 渠道 3000 3000

14 allen wenzhou 渠道 4000 7000

15 jekch shanghai 渠道 2500 2500

11 smith hangzhou 市场 1000 2000

11 smith hangzhou 市场 1000 2000

12 smith wenzhou 市场 2000 6000

12 smith wenzhou 市场 2000 6000

对sales进行分组,然后分组内递加sum。

看下来的count()over()分析函数的使用

SQL> select sales_id,sales,count(*)over()求总计数,

2 count(*)over(order by sales_id)递加求计数,

3 count(*)over(partition by sales_id)分组求计数,

4 count(*)over(partition by sales_id order by sales)分组递加求计数

5 from test

6 ;

SALES_ID SALES 求总计数 递加求计数 分组求计数 分组递加求计数

-------- ---------- ---------- ---------- ---------- --------------

11 smith 7 2 2 2

11 smith 7 2 2 2

12 smith 7 4 2 2

12 smith 7 4 2 2

13 allen 7 5 1 1

14 allen 7 6 1 1

15 jekch 7 7 1 1

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30333291/viewspace-1774844/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2015-07-07

  • 博文量
    2
  • 访问量
    3617