Chinaunix首页 | 论坛 | 博客
  • 博客访问: 927851
  • 博文数量: 358
  • 博客积分: 8185
  • 博客等级: 中将
  • 技术积分: 3751
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-15 16:27
个人简介

The views and opinions expressed all for my own,only for study and test, not reflect the views of Any Company and its affiliates.

文章分类

全部博文(358)

文章存档

2012年(8)

2011年(18)

2010年(50)

2009年(218)

2008年(64)

我的朋友

分类:

2008-12-21 18:52:35

Oracle8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,而聚合函数对于每个组只返回一行。

一、over函数

  over函数指定了分析函数工作的数据窗口的大小,这个数据窗口大小可能会随着行的变化而变化,例如:

over(order by salary)按照salary排序进行累计,order by是个默认的开窗函数

over(partition by deptno) 按照部门分区

over(order by salary range between 50 preceding and 150 following)每行对应的数据窗口是之前行幅度值不超过50,之后行幅度值不超过150的数据记录

over(order by salary rows between 50 perceding and 150 following)50行,后150

overorder by salary rows between unbounded preceding and unbounded following)所有行

overorder by salary range between unbounded preceding and unbounded following)所有行

二、sum函数

  功能描述:该函数计算组中表达式的累积和。
SAMPLE
:下例计算同一经理下员工的薪水累积值

SELECT manager_id, last_name, salary,
SUM (salary) OVER (PARTITION BY manager_id ORDER BY salary
RANGE UNBOUNDED PRECEDING) l_csum
FROM employees
WHERE manager_id in (101,103,108);

三、应用实例

1,  测试环境设置

设有销售表t_sales (subcompany,branch,region,customer,sale_qty); 存储客户的销售明细,记录如下所示。

Subcompany

Branch

Region

Customer

Sale_qty

北京分公司

北京经营部

片区1

客户1

1

北京分公司

北京经营部

片区1

客户1

1

北京分公司

北京经营部

片区1

客户2

1

北京分公司

北京经营部

片区1

客户2

1

北京分公司

北京经营部

片区2

客户1

1

北京分公司

北京经营部

片区2

客户1

1

北京分公司

北京经营部

片区2

客户2

1

北京分公司

北京经营部

片区2

客户2

1

北京分公司

其他经营部

片区1

客户1

1

北京分公司

其他经营部

片区1

客户1

1

北京分公司

其他经营部

片区1

客户2

1

北京分公司

其他经营部

片区1

客户2

1

北京分公司

其他经营部

片区2

客户1

1

北京分公司

其他经营部

片区2

客户1

1

北京分公司

其他经营部

片区2

客户2

1

北京分公司

其他经营部

片区2

客户2

1

create 管理员在2009年8月13日编辑了该文章文章。

-->
阅读(749) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~