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

oracle中110个常用函数介绍

程序员文章站 2023-11-25 16:41:10
1. ascii 返回与指定的字符对应的十进制数; sql> select ascii(a) a,ascii(a) a,ascii(0) zero,asci...

1. ascii
返回与指定的字符对应的十进制数;
sql> select ascii(a) a,ascii(a) a,ascii(0) zero,ascii( ) space from dual;
a a zero space
--------- --------- --------- ---------
65 97 48 32

2. chr
给出整数,返回对应的字符;
sql> select chr(54740) zhao,chr(65) chr65 from dual;
zh c
-- -
赵 a

3. concat
连接两个字符串;
sql> select concat(010-,88888888)||转23 高乾竞电话 from dual;
高乾竞电话
----------------
010-88888888转23

4. initcap
返回字符串并将字符串的第一个字母变为大写;
sql> select initcap(smith) upp from dual;
upp
-----
smith

5.instr(c1,c2,i,j)
在一个字符串中搜索指定的字符,返回发现指定的字符的位置;
c1 被搜索的字符串
c2 希望搜索的字符串
i 搜索的开始位置,默认为1
j 出现的位置,默认为1
sql> select instr(oracle traning,ra,1,2) instring from dual;
instring
---------
9

6.length
返回字符串的长度;
sql> select name,length(name),addr,length(addr),sal,length(to_char(sal)) from gao.nchar_tst;
name length(name) addr length(addr) sal length(to_char(sal))
------ ------------ ---------------- ------------ --------- --------------------
高乾竞 3 北京市海锭区 6 9999.99 7

7.lower
返回字符串,并将所有的字符小写
sql> select lower(aabbccdd)aabbccdd from dual;
aabbccdd
--------
aabbccdd

8.upper
返回字符串,并将所有的字符大写
sql> select upper(aabbccdd) upper from dual;
upper
--------
aabbccdd

9.rpad和lpad(粘贴字符)
rpad 在列的右边粘贴字符
lpad 在列的左边粘贴字符
sql> select lpad(rpad(gao,10,*),17,*)from dual;
lpad(rpad(gao,1
-----------------
*******gao*******
不够字符则用*来填满

10.ltrim和rtrim
ltrim 删除左边出现的字符串
rtrim 删除右边出现的字符串
sql> select ltrim(rtrim( gao qian jing , ), ) from dual;
ltrim(rtrim(
-------------
gao qian jing

11.substr(string,start,count)
取子字符串,从start开始,取count个
sql> select substr(13088888888,3,8) from dual;
substr(
--------
08888888

12.replace(string,s1,s2)
string 希望被替换的字符或变量
s1 被替换的字符串
s2 要替换的字符串
sql> select replace(he love you,he,i) from dual;
replace(h
----------
i love you

13.soundex
返回一个与给定的字符串读音相同的字符串
sql> create table table1(xm varchar(8));
sql> insert into table1 values(weather);
sql> insert into table1 values(wether);
sql> insert into table1 values(gao);
sql> select xm from table1 where soundex(xm)=soundex(weather);
xm
--------
weather
wether
• 14.trim(s from string)
leading 剪掉前面的字符
trailing 剪掉后面的字符
如果不指定,默认为空格符
15.abs
返回指定值的绝对值
sql> select abs(100),abs(-100) from dual;
abs(100) abs(-100)
--------- ---------
100 100

16.acos
给出反余弦的值
sql> select acos(-1) from dual;
acos(-1)
---------
3.1415927

17.asin
给出反正弦的值
sql> select asin(0.5) from dual;
asin(0.5)
---------
.52359878

18.atan
返回一个数字的反正切值
sql> select atan(1) from dual;
atan(1)
---------
.78539816

19.ceil
返回大于或等于给出数字的最小整数
sql> select ceil(3.1415927) from dual;
ceil(3.1415927)
---------------
4

20.cos
返回一个给定数字的余弦
sql> select cos(-3.1415927) from dual;
cos(-3.1415927)
---------------
-1
21.cosh
返回一个数字反余弦值
sql> select cosh(20) from dual;
cosh(20)
---------
242582598

22.exp
返回一个数字e的n次方根
sql> select exp(2),exp(1) from dual;
exp(2) exp(1)
--------- ---------
7.3890561 2.7182818

23.floor
对给定的数字取整数
sql> select floor(2345.67) from dual;
floor(2345.67)
--------------
2345

24.ln
返回一个数字的对数值
sql> select ln(1),ln(2),ln(2.7182818) from dual;
ln(1) ln(2) ln(2.7182818)
--------- --------- -------------
0 .69314718 .99999999

25.log(n1,n2)
返回一个以n1为底n2的对数
sql> select log(2,1),log(2,4) from dual;
log(2,1) log(2,4)
--------- ---------
0 2

26.mod(n1,n2)
返回一个n1除以n2的余数
sql> select mod(10,3),mod(3,3),mod(2,3) from dual;
mod(10,3) mod(3,3) mod(2,3)
--------- --------- ---------
1 0 2

27.power
返回n1的n2次方根
sql> select power(2,10),power(3,3) from dual;
power(2,10) power(3,3)
----------- ----------
1024 27

28.round和trunc
按照指定的精度进行舍入
sql> select round(55.5),round(-55.4),trunc(55.5),trunc(-55.5) from dual;
round(55.5) round(-55.4) trunc(55.5) trunc(-55.5)
----------- ------------ ----------- ------------
56 -55 55 -55

29.sign
取数字n的符号,大于0返回1,小于0返回-1,等于0返回0
sql> select sign(123),sign(-100),sign(0) from dual;
sign(123) sign(-100) sign(0)
--------- ---------- ---------
1 -1 0

30.sin
返回一个数字的正弦值
sql> select sin(1.57079) from dual;
sin(1.57079)
------------
1
• 31.sigh
返回双曲正弦的值
sql> select sin(20),sinh(20) from dual;
sin(20) sinh(20)
--------- ---------
.91294525 242582598

32.sqrt
返回数字n的根
sql> select sqrt(64),sqrt(10) from dual;
sqrt(64) sqrt(10)
--------- ---------
8 3.1622777

33.tan
返回数字的正切值
sql> select tan(20),tan(10) from dual;
tan(20) tan(10)
--------- ---------
2.2371609 .64836083

34.tanh
返回数字n的双曲正切值
sql> select tanh(20),tan(20) from dual;
tanh(20) tan(20)
--------- ---------
1 2.2371609

35.trunc
按照指定的精度截取一个数
sql> select trunc(124.1666,-2) trunc1,trunc(124.16666,2) from dual;
trunc1 trunc(124.16666,2)
--------- ------------------
100 124.16

36.add_months
增加或减去月份
sql> select to_char(add_months(to_date(199912,yyyymm),2),yyyymm) from dual;
to_cha
------
200002
sql> select to_char(add_months(to_date(199912,yyyymm),-2),yyyymm) from dual;
to_cha
------
199910

37.last_day
返回日期的最后一天
sql> select to_char(sysdate,yyyy.mm.dd),to_char((sysdate)+1,yyyy.mm.dd) from dual;
to_char(sy to_char((s
---------- ----------
2004.05.09 2004.05.10
sql> select last_day(sysdate) from dual;
last_day(s
----------
31-5月 -04

38.months_between(date2,date1)
给出date2-date1的月份
sql> select months_between(19-12月-1999,19-3月-1999) mon_between from dual;
mon_between
-----------
9
sql>selectmonths_between(to_date(2000.05.20,yyyy.mm.dd),to_date(2005.05.20,yyyy.mm.dd)) mon_betw from dual;
mon_betw
---------
-60

39.new_time(date,this,that)
给出在this时区=other时区的日期和时间
sql> select to_char(sysdate,yyyy.mm.dd hh24:mi:ss) bj_time,to_char(new_time
2 (sysdate,pdt,gmt),yyyy.mm.dd hh24:mi:ss) los_angles from dual;
bj_time los_angles
------------------- -------------------
2004.05.09 11:05:32 2004.05.09 18:05:32

40.next_day(date,day)
给出日期date和星期x之后计算下一个星期的日期
sql> select next_day('18-5月-2001','星期五') next_day from dual;
next_day
----------
25-5月 -01

41.sysdate
用来得到系统的当前日期
sql> select to_char(sysdate,dd-mm-yyyy day) from dual;
to_char(sysdate,
-----------------
09-05-2004 星期日
trunc(date,fmt)按照给出的要求将日期截断,如果fmt=mi表示保留分,截断秒
sql> select to_char(trunc(sysdate,hh),yyyy.mm.dd hh24:mi:ss) hh,
2 to_char(trunc(sysdate,mi),yyyy.mm.dd hh24:mi:ss) hhmm from dual;
hh hhmm
------------------- -------------------
2004.05.09 11:00:00 2004.05.09 11:17:00

42.chartorowid
将字符数据类型转换为rowid类型
sql> select rowid,rowidtochar(rowid),ename from scott.emp;
rowid rowidtochar(rowid) ename
------------------ ------------------ ----------
aaaafkaacaaaaeqaaa aaaafkaacaaaaeqaaa smith
aaaafkaacaaaaeqaab aaaafkaacaaaaeqaab allen
aaaafkaacaaaaeqaac aaaafkaacaaaaeqaac ward
aaaafkaacaaaaeqaad aaaafkaacaaaaeqaad jones

43.convert(c,dset,sset)
将源字符串 sset从一个语言字符集转换到另一个目的dset字符集
sql> select convert(strutz,we8hp,f7dec) "conversion" from dual;
conver
------
strutz

44.hextoraw
将一个十六进制构成的字符串转换为二进制

45.rawtohext
将一个二进制构成的字符串转换为十六进制

46.rowidtochar
将rowid数据类型转换为字符类型

47.to_char(date,format)
sql> select to_char(sysdate,yyyy/mm/dd hh24:mi:ss) from dual;
to_char(sysdate,yy
-------------------
2004/05/09 21:14:41
• 48.to_date(string,format)
将字符串转化为oracle中的一个日期

49.to_multi_byte
将字符串中的单字节字符转化为多字节字符
sql> select to_multi_byte(高) from dual;
to
--

50.to_number
将给出的字符转换为数字
sql> select to_number(1999) year from dual;
year
---------
1999
51.bfilename(dir,file)
指定一个外部二进制文件
sql>insert into file_tb1 values(bfilename(lob_dir1,image1.gif));

52.convert(x,desc,source)
将x字段或变量的源source转换为desc
sql> select sid,serial#,username,decode(command,
2 0,none,
3 2,insert,
4 3,
5 select,
6 6,update,
7 7,delete,
8 8,drop,
9 other) cmd from v$session where type!=background;
sid serial# username cmd
--------- --------- ------------------------------ ------
1 1 none
2 1 none
3 1 none
4 1 none
5 1 none
6 1 none
7 1275 none
8 1275 none
9 20 gao select
10 40 gao none

53.dump(s,fmt,start,length)
dump函数以fmt指定的内部数字格式返回一个varchar2类型的值
sql> col global_name for a30
sql> col dump_string for a50
sql> set lin 200
sql> select global_name,dump(global_name,1017,8,5) dump_string from global_name;
global_name dump_string
------------------------------ --------------------------------------------------
oracle.world typ=1 len=12 characterset=zhs16gbk: w,o,r,l,d

54.empty_blob()和empty_clob()
这两个函数都是用来对大数据类型字段进行初始化操作的函数

55.greatest
返回一组表达式中的最大值,即比较字符的编码大小.
sql> select greatest(aa,ab,ac) from dual;
gr
--
ac
sql> select greatest(啊,安,天) from dual;
gr
--

56.least
返回一组表达式中的最小值
sql> select least(啊,安,天) from dual;
le
--

57.uid
返回标识当前用户的唯一整数
sql> show user
user 为"gao"
sql> select username,user_id from dba_users where user_id=uid;
username user_id
------------------------------ ---------
gao 25

58.user
返回当前用户的名字
sql> select user from dual;
user
------------------------------
gao

59.userevn
返回当前用户环境的信息,opt可以是:
entryid,sessionid,terminal,isdba,lable,language,client_info,lang,vsize
isdba 查看当前用户是否是dba如果是则返回true
sql> select userenv(isdba) from dual;
useren
------
false
sql> select userenv(isdba) from dual;
useren
------
true
session
返回会话标志
sql> select userenv(sessionid) from dual;
userenv(sessionid)
--------------------
152
entryid
返回会话人口标志
sql> select userenv(entryid) from dual;
userenv(entryid)
------------------
0
instance
返回当前instance的标志
sql> select userenv(instance) from dual;
userenv(instance)
-------------------
1
language
返回当前环境变量
sql> select userenv(language) from dual;
userenv(language)
----------------------------------------------------
simplified chinese_china.zhs16gbk
lang
返回当前环境的语言的缩写
sql> select userenv(lang) from dual;
userenv(lang)
----------------------------------------------------
zhs
terminal
返回用户的终端或机器的标志
sql> select userenv(terminal) from dual;
userenv(termina
----------------
gao
vsize(x)
返回x的大小(字节)数
sql> select vsize(user),user from dual;
vsize(user) user
----------- ------------------------------
6 system
• 60.avg(distinct|all)
all表示对所有的值求平均值,distinct只对不同的值求平均值
sqlwks> create table table3(xm varchar(8),sal number(7,2));
语句已处理。
sqlwks> insert into table3 values(gao,1111.11);
sqlwks> insert into table3 values(gao,1111.11);
sqlwks> insert into table3 values(zhu,5555.55);
sqlwks> commit;
sql> select avg(distinct sal) from gao.table3;
avg(distinctsal)
----------------
3333.33
sql> select avg(all sal) from gao.table3;
avg(allsal)
-----------
2592.59

61.max(distinct|all)
求最大值,all表示对所有的值求最大值,distinct表示对不同的值求最大值,相同的只取一次
sql> select max(distinct sal) from scott.emp;
max(distinctsal)
----------------
5000

62.min(distinct|all)
求最小值,all表示对所有的值求最小值,distinct表示对不同的值求最小值,相同的只取一次
sql> select min(all sal) from gao.table3;
min(allsal)
-----------
1111.11

63.stddev(distinct|all)
求标准差,all表示对所有的值求标准差,distinct表示只对不同的值求标准差
sql> select stddev(sal) from scott.emp;
stddev(sal)
-----------
1182.5032
sql> select stddev(distinct sal) from scott.emp;
stddev(distinctsal)
-------------------
1229.951

64.variance(distinct|all)
求协方差
sql> select variance(sal) from scott.emp;
variance(sal)
-------------
1398313.9

65.group by
主要用来对一组数进行统计
sql> select deptno,count(*),sum(sal) from scott.emp group by deptno;
deptno count(*) sum(sal)
--------- --------- ---------
10 3 8750
20 5 10875
30 6 9400

66.having
对分组统计再加限制条件
sql> select deptno,count(*),sum(sal) from scott.emp group by deptno having count(*)>=5;
deptno count(*) sum(sal)
--------- --------- ---------
20 5 10875
30 6 9400
sql> select deptno,count(*),sum(sal) from scott.emp having count(*)>=5 group by deptno ;
deptno count(*) sum(sal)
--------- --------- ---------
20 5 10875
30 6 9400

67.order by
用于对查询到的结果进行排序输出
sql> select deptno,ename,sal from scott.emp order by deptno,sal desc;
deptno ename sal
--------- ---------- ---------
10 king 5000
10 clark 2450
10 miller 1300
20 scott 3000
20 ford 3000
20 jones 2975
20 adams 1100
20 smith 800
30 blake 2850
30 allen 1600
30 turner 1500
30 ward 1250
30 martin 1250
30 james 950
68. pl/sql中的case语句
select  (case  when  dummy='x'  then  0  else  1  end)  as  flag  from  dual;
case的第1种用法:
case col when 'a' then 1
when 'b' then 2
else 0 end
这种用法跟decode一样没什么区别
case的第2种用法:
case when score <60 then 'd'
when score >=60 and score <70 then 'c'
when score >=70 and score <80 then 'b'
else 'a' end
69.nvl(expr1, expr2)
nvl(expr1, expr2)->expr1为null,返回expr2;不为null,返回expr1。注意两者的类型要一致
nvl2 (expr1, expr2, expr3) ->expr1不为null,返回expr2;为null,返回expr3。expr2和expr3类型不同的话,expr3会转换为expr2的类型
nullif (expr1, expr2) ->相等返回null,不等返回expr1
• oracle分析函数参考手册
=============================================
作者: xsb([url]http://xsb.itpub.net)[/url]
发表于:2006.03.01 12:22
分类: dw&bi
出处:http://xsb.itpub.net/post/419/33028
---------------------------------------------------------------
    oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,
而聚合函数对于每个组只返回一行。
常用的分析函数如下所列:
row_number() over(partition by ... order by ...)
rank() over(partition by ... order by ...)
dense_rank() over(partition by ... order by ...)
count() over(partition by ... order by ...)
max() over(partition by ... order by ...)
min() over(partition by ... order by ...)
sum() over(partition by ... order by ...)
avg() over(partition by ... order by ...)
first_value() over(partition by ... order by ...)
last_value() over(partition by ... order by ...)
lag() over(partition by ... order by ...)
lead() over(partition by ... order by ...)

    下面例子中使用的表来自oracle自带的hr用户下的表,如果没有安装该用户,可以在sys用户下运行
$oracle_home/demo/schema/human_resources/hr_main.sql来创建。
除本文内容外,你还可参考:
rollup与cube [url]http://xsb.itpub.net/post/419/29159[/url]
分析函数使用例子介绍:[url]http://xsb.itpub.net/post/419/44634[/url]
本文如果未指明,缺省是在hr用户下运行例子。
开窗函数的的理解:
开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下:
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 preceding and 150 following)
每行对应的数据窗口是之前50行,之后150行
over(order by salary rows between unbounded preceding and unbounded following)
每行对应的数据窗口是从第一行到最后一行,等效:
over(order by salary range between unbounded preceding and unbounded following)
主要参考资料:《expert one-on-one》 tom kyte 《oracle9i sql reference》第6章
ohwww 2007-3-12 09:19
70。avg
功能描述:用于计算一个组和数据窗口内表达式的平均值。
sample:下面的例子中列c_mavg计算员工表中每个员工的平均薪水报告,该平均值由当前员工和与之具有相同经理的前一个和后一个三者的平均数得来;
select manager_id, last_name, hire_date, salary,
avg(salary) over (partition by manager_id order by hire_date
rows between 1 preceding and 1 following) as c_mavg
from employees;
manager_id last_name hire_date salary c_mavg
---------- ------------------------- --------- ---------- ----------
100 kochhar 21-sep-89 17000 17000
100 de haan 13-jan-93 17000 15000
100 raphaely 07-dec-94 11000 11966.6667
100 kaufling 01-may-95 7900 10633.3333
100 hartstein 17-feb-96 13000 9633.33333
100 weiss 18-jul-96 8000 11666.6667
100 russell 01-oct-96 14000 11833.3333
71。corr
功能描述:返回一对表达式的相关系数,它是如下的缩写:
covar_pop(expr1,expr2)/stddev_pop(expr1)*stddev_pop(expr2))
从统计上讲,相关性是变量之间关联的强度,变量之间的关联意味着在某种程度
上一个变量的值可由其它的值进行预测。通过返回一个-1~1之间的一个数, 相关
系数给出了关联的强度,0表示不相关。
sample:下例返回1998年月销售收入和月单位销售的关系的累积系数(本例在sh用户下运行)
select t.calendar_month_number,
corr (sum(s.amount_sold), sum(s.quantity_sold))
over (order by t.calendar_month_number) as cum_corr
from sales s, times t
where s.time_id = t.time_id and calendar_year = 1998
group by t.calendar_month_number
order by t.calendar_month_number;
calendar_month_number cum_corr
--------------------- ----------
1
2 1
3 .994309382
4 .852040875
5 .846652204
6 .871250628
7 .910029803
8 .917556399
9 .920154356
10 .86720251
11 .844864765
12 .903542662

72。covar_pop
功能描述:返回一对表达式的总体协方差。
sample:下例cum_covp返回定价和最小产品价格的累积总体协方差
select product_id, supplier_id,
covar_pop(list_price, min_price)
over (order by product_id, supplier_id) as cum_covp,
covar_samp(list_price, min_price)
over (order by product_id, supplier_id) as cum_covs
from product_information p
where category_id = 29
order by product_id, supplier_id;
product_id supplier_id cum_covp cum_covs
---------- ----------- ---------- ----------
1774 103088 0
1775 103087 1473.25 2946.5
1794 103096 1702.77778 2554.16667
1825 103093 1926.25 2568.33333
2004 103086 1591.4 1989.25
2005 103086 1512.5 1815
2416 103088 1475.97959 1721.97619
.
.

73。covar_samp
功能描述:返回一对表达式的样本协方差
sample:下例cum_covs返回定价和最小产品价格的累积样本协方差
select product_id, supplier_id,
covar_pop(list_price, min_price)
over (order by product_id, supplier_id) as cum_covp,
covar_samp(list_price, min_price)
over (order by product_id, supplier_id) as cum_covs
from product_information p
where category_id = 29
order by product_id, supplier_id;
product_id supplier_id cum_covp cum_covs
---------- ----------- ---------- ----------
1774 103088 0
1775 103087 1473.25 2946.5
1794 103096 1702.77778 2554.16667
1825 103093 1926.25 2568.33333
2004 103086 1591.4 1989.25
2005 103086 1512.5 1815
2416 103088 1475.97959 1721.97619
• 74。count
功能描述:对一组内发生的事情进行累积计数,如果指定*或一些非空常数,count将对所有行计数,如果指定一个表达式,count
返回表达式非空赋值的计数,当有相同值出现时,这些相等的值都会被纳入被计算的值;可以使用distinct来记录去掉一组中完全
相同的数据后出现的行数。
sample:下面例子中计算每个员工在按薪水排序中当前行附近薪水在[n-50,n+150]之间的行数,n表示当前行的薪水
例如,philtanker的薪水2200,排在他之前的行中薪水大于等于2200-50的有1行,排在他之后的行中薪水小于等于2200+150的行
没有,所以count计数值cnt3为2(包括自己当前行);cnt2值相当于小于等于当前行的salary值的所有行数
select last_name, salary, count(*) over () as cnt1,
count(*) over (order by salary) as cnt2,
count(*) over (order by salary range between 50 preceding
and 150 following) as cnt3 from employees;
last_name salary cnt1 cnt2 cnt3
------------------------- ---------- ---------- ---------- ----------
olson 2100 107 1 3
markle 2200 107 3 2
philtanker 2200 107 3 2
landry 2400 107 5 8
gee 2400 107 5 8
colmenares 2500 107 11 10
patel 2500 107 11 10
.
.
75。cume_dist
功能描述:计算一行在组中的相对位置,cume_dist总是返回大于0、小于或等于1的数,该数表示该行在n行中的位置。例如,
在一个3行的组中,返回的累计分布值为1/3、2/3、3/3
sample:下例中计算每个工种的员工按薪水排序依次累积出现的分布百分比
select job_id, last_name, salary, cume_dist()
over (partition by job_id order by salary) as cume_dist
from employees where job_id like 'pu%';
job_id last_name salary cume_dist
---------- ------------------------- ---------- ----------
pu_clerk colmenares 2500 .2
pu_clerk himuro 2600 .4
pu_clerk tobias 2800 .6
pu_clerk baida 2900 .8
pu_clerk khoo 3100 1
pu_man raphaely 11000 1
76。dense_rank
功能描述:根据order by子句中表达式的值,从查询返回的每一行,计算它们与其它行的相对位置。组内的数据按order by子句排序,然后给每一行赋一个号,从而形成一个序列,该序列从1开始,往后累加。每次order by表达式的值发生变化时,该序列也随之增加。有同样值的行得到同样的数字序号(认为null时相等的)。密集的序列返回的时没有间隔的数
sample:下例中计算每个员工按部门分区再按薪水排序,依次出现的序列号(注意与rank函数的区别)
select d.department_id , e.last_name, e.salary, dense_rank()
over (partition by e.department_id order by e.salary) as drank
from employees e, departments d
where e.department_id = d.department_id
and d.department_id in ('60', '90');
department_id last_name salary drank
------------- ------------------------- ---------- ----------
60 lorentz 4200 1
60 austin 4800 2
60 pataballa 4800 2
60 ernst 6000 3
60 hunold 9000 4
90 kochhar 17000 1
90 de haan 17000 1
90 king 24000 2

77。first
功能描述:从dense_rank返回的集合中取出排在最前面的一个值的行(可能多行,因为值可能相等),因此完整的语法需要在开始处加上一个集合函数以从中取出记录
sample:下面例子中dense_rank按部门分区,再按佣金commission_pct排序,first取出佣金最低的对应的所有行,然后前面的max函数从这个集合中取出薪水最低的值;last取出佣金最高的对应的所有行,然后前面的min函数从这个集合中取出薪水最高的值
select last_name, department_id, salary,
min(salary) keep (dense_rank first order by commission_pct)
over (partition by department_id) "worst",
max(salary) keep (dense_rank last order by commission_pct)
over (partition by department_id) "best"
from employees
where department_id in (20,80)
order by department_id, salary;
last_name department_id salary worst best
------------------------- ------------- ---------- ---------- ----------
fay 20 6000 6000 13000
hartstein 20 13000 6000 13000
kumar 80 6100 6100 14000
banda 80 6200 6100 14000
johnson 80 6200 6100 14000
ande 80 6400 6100 14000
lee 80 6800 6100 14000
tuvault 80 7000 6100 14000
sewall 80 7000 6100 14000
marvins 80 7200 6100 14000
bates 80 7300 6100 14000
.
.
.
78。first_value
功能描述:返回组中数据窗口的第一个值。
sample:下面例子计算按部门分区按薪水排序的数据窗口的第一个值对应的名字,如果薪水的第一个值有多个,则从多个对应的名字中取缺省排序的第一个名字
select department_id, last_name, salary, first_value(last_name)
over (partition by department_id order by salary asc ) as lowest_sal
from employees
where department_id in(20,30);
department_id last_name salary lowest_sal
------------- ------------------------- ---------- --------------
20 fay 6000 fay
20 hartstein 13000 fay
30 colmenares 2500 colmenares
30 himuro 2600 colmenares
30 tobias 2800 colmenares
30 baida 2900 colmenares
30 khoo 3100 colmenares
30 raphaely 11000 colmenares

79。lag
功能描述:可以访问结果集中的其它行而不用进行自连接。它允许去处理游标,就好像游标是一个数组一样。在给定组中可参考当前行之前的行,这样就可以从组中与当前行一起选择以前的行。offset是一个正整数,其默认值为1,若索引超出窗口的范围,就返回默认值(默认返回的是组中第一行),其相反的函数是lead
sample:下面的例子中列prev_sal返回按hire_date排序的前1行的salary值
select last_name, hire_date, salary,
lag(salary, 1, 0) over (order by hire_date) as prev_sal
from employees
where job_id = 'pu_clerk';
last_name hire_date salary prev_sal
------------------------- ---------- ---------- ----------
khoo 18-5月 -95 3100 0
tobias 24-7月 -97 2800 3100
baida 24-12月-97 2900 2800
himuro 15-11月-98 2600 2900
colmenares 10-8月 -99 2500 2600

80。last
功能描述:从dense_rank返回的集合中取出排在最后面的一个值的行(可能多行,因为值可能相等),因此完整的语法需要在开始处加上一个集合函数以从中取出记录
sample:下面例子中dense_rank按部门分区,再按佣金commission_pct排序,first取出佣金最低的对应的所有行,然后前面的max函数从这个集合中取出薪水最低的值;last取出佣金最高的对应的所有行,然后前面的min函数从这个集合中取出薪水最高的值
select last_name, department_id, salary,
min(salary) keep (dense_rank first order by commission_pct)
over (partition by department_id) "worst",
max(salary) keep (dense_rank last order by commission_pct)
over (partition by department_id) "best"
from employees
where department_id in (20,80)
order by department_id, salary;
last_name department_id salary worst best
------------------------- ------------- ---------- ---------- ----------
fay 20 6000 6000 13000
hartstein 20 13000 6000 13000
kumar 80 6100 6100 14000
banda 80 6200 6100 14000
johnson 80 6200 6100 14000
ande 80 6400 6100 14000
lee 80 6800 6100 14000
tuvault 80 7000 6100 14000
sewall 80 7000 6100 14000
marvins 80 7200 6100 14000
bates 80 7300 6100 14000
.
81。last_value
功能描述:返回组中数据窗口的最后一个值。
sample:下面例子计算按部门分区按薪水排序的数据窗口的最后一个值对应的名字,如果薪水的最后一个值有多个,则从多个对应的名字中取缺省排序的最后一个名字
select department_id, last_name, salary, last_value(last_name)
over(partition by department_id order by salary) as highest_sal
from employees
where department_id in(20,30);
department_id last_name salary highest_sal
------------- ------------------------- ---------- ------------
20 fay 6000 fay
20 hartstein 13000 hartstein
30 colmenares 2500 colmenares
30 himuro 2600 himuro
30 tobias 2800 tobias
30 baida 2900 baida
30 khoo 3100 khoo
30 raphaely 11000 raphaely
82。lead
功能描述:lead与lag相反,lead可以访问组中当前行之后的行。offset是一个正整数,其默认值为1,若索引超出窗口的范围,就返回默认值(默认返回的是组中第一行)
sample:下面的例子中每行的"nexthired"返回按hire_date排序的下一行的hire_date值
select last_name, hire_date,
lead(hire_date, 1) over (order by hire_date) as "nexthired"
from employees where department_id = 30;
last_name hire_date nexthired
------------------------- --------- ---------
raphaely 07-dec-94 18-may-95
khoo 18-may-95 24-jul-97
tobias 24-jul-97 24-dec-97
baida 24-dec-97 15-nov-98
himuro 15-nov-98 10-aug-99
colmenares 10-aug-99

83。max
功能描述:在一个组中的数据窗口中查找表达式的最大值。
sample:下面例子中dept_max返回当前行所在部门的最大薪水值
select department_id, last_name, salary,
max(salary) over (partition by department_id) as dept_max
from employees where department_id in (10,20,30);
department_id last_name salary dept_max
------------- ------------------------- ---------- ----------
10 whalen 4400 4400
20 hartstein 13000 13000
20 fay 6000 13000
30 raphaely 11000 11000
30 khoo 3100 11000
30 baida 2900 11000
30 tobias 2800 11000
30 himuro 2600 11000
30 colmenares 2500 11000

84。min
功能描述:在一个组中的数据窗口中查找表达式的最小值。
sample:下面例子中dept_min返回当前行所在部门的最小薪水值
select department_id, last_name, salary,
min(salary) over (partition by department_id) as dept_min
from employees where department_id in (10,20,30);
department_id last_name salary dept_min
------------- ------------------------- ---------- ----------
10 whalen 4400 4400
20 hartstein 13000 6000
20 fay 6000 6000
30 raphaely 11000 2500
30 khoo 3100 2500
30 baida 2900 2500
30 tobias 2800 2500
30 himuro 2600 2500
30 colmenares 2500 2500

85。ntile
功能描述:将一个组分为"表达式"的散列表示,例如,如果表达式=4,则给组中的每一行分配一个数(从1到4),如果组中有20行,则给前5行分配1,给下5行分配2等等。如果组的基数不能由表达式值平均分开,则对这些行进行分配时,组中就没有任何percentile的行数比其它percentile的行数超过一行,最低的percentile是那些拥有额外行的percentile。例如,若表达式=4,行数=21,则percentile=1的有5行,percentile=2的有5行等等。
sample:下例中把6行数据分为4份
select last_name, salary,
ntile(4) over (order by salary desc) as quartile from employees
where department_id = 100;
last_name salary quartile
------------------------- ---------- ----------
greenberg 12000 1
faviet 9000 1
chen 8200 2
urman 7800 2
sciarra 7700 3
popp 6900 4

86。percent_rank
功能描述:和cume_dist(累积分配)函数类似,对于一个组中给定的行来说,在计算那行的序号时,先减1,然后除以n-1(n为组中所有的行数)。该函数总是返回0~1(包括1)之间的数。
sample:下例中如果khoo的salary为2900,则pr值为0.6,因为rank函数对于等值的返回序列值是一样的
select department_id, last_name, salary,
percent_rank()
over (partition by department_id order by salary) as pr
from employees
where department_id < 50
order by department_id,salary;
department_id last_name salary pr
------------- ------------------------- ---------- ----------
10 whalen 4400 0
20 fay 6000 0
20 hartstein 13000 1
30 colmenares 2500 0
30 himuro 2600 0.2
30 tobias 2800 0.4
30 baida 2900 0.6
30 khoo 3100 0.8
30 raphaely 11000 1
40 mavris 6500 0
• 87。percentile_cont
功能描述:返回一个与输入的分布百分比值相对应的数据值,分布百分比的计算方法见函数percent_rank,如果没有正好对应的数据值,就通过下面算法来得到值:
rn = 1+ (p*(n-1)) 其中p是输入的分布百分比值,n是组内的行数
crn = ceil(rn) frn = floor(rn)
if (crn = frn = rn) then
(value of expression from row at rn)
else
(crn - rn) * (value of expression for row at frn) +
(rn - frn) * (value of expression for row at crn)
注意:本函数与percentile_disc的区别在找不到对应的分布值时返回的替代值的计算方法不同
sample:在下例中,对于部门60的percentile_cont值计算如下:
p=0.7 n=5 rn =1+ (p*(n-1)=1+(0.7*(5-1))=3.8 crn = ceil(3.8)=4
frn = floor(3.8)=3
(4 - 3.8)* 4800 + (3.8 - 3) * 6000 = 5760
select last_name, salary, department_id,
percentile_cont(0.7) within group (order by salary)
over (partition by department_id) "percentile_cont",
percent_rank()
over (partition by department_id order by salary) "percent_rank"
from employees where department_id in (30, 60);
last_name salary department_id percentile_cont percent_rank
------------------------- ---------- ------------- --------------- ------------
colmenares 2500 30 3000 0
himuro 2600 30 3000 0.2
tobias 2800 30 3000 0.4
baida 2900 30 3000 0.6
khoo 3100 30 3000 0.8
raphaely 11000 30 3000 1
lorentz 4200 60 5760 0
austin 4800 60 5760 0.25
pataballa 4800 60 5760 0.25
ernst 6000 60 5760 0.75
hunold 9000 60 5760 1

88。percentile_disc
功能描述:返回一个与输入的分布百分比值相对应的数据值,分布百分比的计算方法见函数cume_dist,如果没有正好对应的数据值,就取大于该分布值的下一个值。
注意:本函数与percentile_cont的区别在找不到对应的分布值时返回的替代值的计算方法不同
sample:下例中0.7的分布值在部门30中没有对应的cume_dist值,所以就取下一个分布值0.83333333所对应的salary来替代
select last_name, salary, department_id,
percentile_disc(0.7) within group (order by salary )
over (partition by department_id) "percentile_disc",
cume_dist() over (partition by department_id order by salary) "cume_dist"
from employees
where department_id in (30, 60);
last_name salary department_id percentile_disc cume_dist
------------------------- ---------- ------------- --------------- ----------
colmenares 2500 30 3100 .166666667
himuro 2600 30 3100 .333333333
tobias 2800 30 3100 .5
baida 2900 30 3100 .666666667
khoo 3100 30 3100 .833333333
raphaely 11000 30 3100 1
lorentz 4200 60 6000 .2
austin 4800 60 6000 .6
pataballa 4800 60 6000 .6
ernst 6000 60 6000 .8
hunold 9000 60 6000 1
89。rank
功能描述:根据order by子句中表达式的值,从查询返回的每一行,计算它们与其它行的相对位置。组内的数据按order by子句排序,
然后给每一行赋一个号,从而形成一个序列,该序列从1开始,往后累加。每次order by表达式的值发生变化时,该序列也随之增加。
有同样值的行得到同样的数字序号(认为null时相等的)。然而,如果两行的确得到同样的排序,则序数将随后跳跃。若两行序数为1,
则没有序数2,序列将给组中的下一行分配值3,dense_rank则没有任何跳跃。
sample:下例中计算每个员工按部门分区再按薪水排序,依次出现的序列号(注意与dense_rank函数的区别)
select d.department_id , e.last_name, e.salary, rank()
over (partition by e.department_id order by e.salary) as drank
from employees e, departments d
where e.department_id = d.department_id
and d.department_id in ('60', '90');
department_id last_name salary drank
------------- ------------------------- ---------- ----------
60 lorentz 4200 1
60 austin 4800 2
60 pataballa 4800 2
60 ernst 6000 4
60 hunold 9000 5
90 kochhar 17000 1
90 de haan 17000 1
90 king 24000 3

90。ratio_to_report
功能描述:该函数计算expression/(sum(expression))的值,它给出相对于总数的百分比,即当前行对sum(expression)的贡献。
sample:下例计算每个员工的工资占该类员工总工资的百分比
select last_name, salary, ratio_to_report(salary) over () as rr
from employees
where job_id = 'pu_clerk';
last_name salary rr
------------------------- ---------- ----------
khoo 3100 .223021583
baida 2900 .208633094
tobias 2800 .201438849
himuro 2600 .18705036
colmenares 2500 .179856115
• 91。regr_ (linear regression) functions
功能描述:这些线性回归函数适合最小二乘法回归线,有9个不同的回归函数可使用。
regr_slope:返回斜率,等于covar_pop(expr1, expr2) / var_pop(expr2)
regr_intercept:返回回归线的y截距,等于
avg(expr1) - regr_slope(expr1, expr2) * avg(expr2)
regr_count:返回用于填充回归线的非空数字对的数目
regr_r2:返回回归线的决定系数,计算式为:
if var_pop(expr2) = 0 then return null
if var_pop(expr1) = 0 and var_pop(expr2) != 0 then return 1
if var_pop(expr1) > 0 and var_pop(expr2 != 0 then
return power(corr(expr1,expr),2)
regr_avgx:计算回归线的自变量(expr2)的平均值,去掉了空对(expr1, expr2)后,等于avg(expr2)
regr_avgy:计算回归线的应变量(expr1)的平均值,去掉了空对(expr1, expr2)后,等于avg(expr1)
regr_sxx: 返回值等于regr_count(expr1, expr2) * var_pop(expr2)
regr_syy: 返回值等于regr_count(expr1, expr2) * var_pop(expr1)
regr_sxy: 返回值等于regr_count(expr1, expr2) * covar_pop(expr1, expr2)
(下面的例子都是在sh用户下完成的)
sample 1:下例计算1998年最后三个星期中两种产品(260和270)在周末的销售量中已开发票数量和总数量的累积斜率和回归线的截距
select t.fiscal_month_number "month", t.day_number_in_month "day",
regr_slope(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_month_desc, t.day_number_in_month) as cum_slope,
regr_intercept(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_month_desc, t.day_number_in_month) as cum_icpt
from sales s, times t
where s.time_id = t.time_id
and s.prod_id in (270, 260)
and t.fiscal_year=1998
and t.fiscal_week_number in (50, 51, 52)
and t.day_number_in_week in (6,7)
order by t.fiscal_month_desc, t.day_number_in_month;
month day cum_slope cum_icpt
---------- ---------- ---------- ----------
12 12 -68 1872
12 12 -68 1872
12 13 -20.244898 1254.36735
12 13 -20.244898 1254.36735
12 19 -18.826087 1287
12 20 62.4561404 125.28655
12 20 62.4561404 125.28655
12 20 62.4561404 125.28655
12 20 62.4561404 125.28655
12 26 67.2658228 58.9712313
12 26 67.2658228 58.9712313
12 27 37.5245541 284.958221
12 27 37.5245541 284.958221
12 27 37.5245541 284.958221
sample 2:下例计算1998年4月每天的累积交易数量
select unique t.day_number_in_month,
regr_count(s.amount_sold, s.quantity_sold)
over (partition by t.fiscal_month_number order by t.day_number_in_month)
"regr_count"
from sales s, times t
where s.time_id = t.time_id
and t.fiscal_year = 1998 and t.fiscal_month_number = 4;
day_number_in_month regr_count
------------------- ----------
1 825
2 1650
3 2475
4 3300
26 21450
30 22200
sample 3:下例计算1998年每月销售量中已开发票数量和总数量的累积回归线决定系数
select t.fiscal_month_number,
regr_r2(sum(s.amount_sold), sum(s.quantity_sold))
over (order by t.fiscal_month_number) "regr_r2"
from sales s, times t
where s.time_id = t.time_id
and t.fiscal_year = 1998
group by t.fiscal_month_number
order by t.fiscal_month_number;
fiscal_month_number regr_r2
------------------- ----------
1
2 1
3 .927372984
4 .807019972
5 .932745567
6 .94682861
7 .965342011
8 .955768075
9 .959542618
10 .938618575
11 .880931415
12 .882769189
sample 4:下例计算1998年12月最后两周产品260的销售量中已开发票数量和总数量的累积平均值
select t.day_number_in_month,
regr_avgy(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_month_desc, t.day_number_in_month)
"regr_avgy",
regr_avgx(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_month_desc, t.day_number_in_month)
"regr_avgx"
from sales s, times t
where s.time_id = t.time_id
and s.prod_id = 260
and t.fiscal_month_desc = '1998-12'
and t.fiscal_week_number in (51, 52)
order by t.day_number_in_month;
day_number_in_month regr_avgy regr_avgx
------------------- ---------- ----------
14 882 24.5
14 882 24.5
15 801 22.25
15 801 22.25
16 777.6 21.6
18 642.857143 17.8571429
18 642.857143 17.8571429
20 589.5 16.375
21 544 15.1111111
22 592.363636 16.4545455
22 592.363636 16.4545455
24 553.846154 15.3846154
24 553.846154 15.3846154
26 522 14.5
27 578.4 16.0666667
sample 5:下例计算产品260和270在1998年2月周末销售量中已开发票数量和总数量的累积regr_sxy, regr_sxx, and regr_syy统计值
select t.day_number_in_month,
regr_sxy(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_year, t.fiscal_month_desc) "regr_sxy",
regr_syy(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_year, t.fiscal_month_desc) "regr_syy",
regr_sxx(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_year, t.fiscal_month_desc) "regr_sxx"
from sales s, times t
where s.time_id = t.time_id
and prod_id in (270, 260)
and t.fiscal_month_desc = '1998-02'
and t.day_number_in_week in (6,7)
order by t.day_number_in_month;
day_number_in_month regr_sxy regr_syy regr_sxx
------------------- ---------- ---------- ----------
1 18870.4 2116198.4 258.4
1 18870.4 2116198.4 258.4
1 18870.4 2116198.4 258.4
1 18870.4 2116198.4 258.4
7 18870.4 2116198.4 258.4
8 18870.4 2116198.4 258.4
14 18870.4 2116198.4 258.4
15 18870.4 2116198.4 258.4
21 18870.4 2116198.4 258.4
22 18870.4 2116198.4 258.4
92。row_number
功能描述:返回有序组中一行的偏移量,从而可用于按特定标准排序的行号。
sample:下例返回每个员工再在每个部门中按员工号排序后的顺序号
select department_id, last_name, employee_id, row_number()
over (partition by department_id order by employee_id) as emp_id
from employees
where department_id < 50;
department_id last_name employee_id emp_id
------------- ------------------------- ----------- ----------
10 whalen 200 1
20 hartstein 201 1
20 fay 202 2
30 raphaely 114 1
30 khoo 115 2
30 baida 116 3
30 tobias 117 4
30 himuro 118 5
30 colmenares 119 6
40 mavris 203 1
93。stddev
功能描述:计算当前行关于组的标准偏离。(standard deviation)
sample:下例返回部门30按雇佣日期排序的薪水值的累积标准偏离
select last_name, hire_date,salary,
stddev(salary) over (order by hire_date) "stddev"
from employees
where department_id = 30;
last_name hire_date salary stddev
------------------------- ---------- ---------- ----------
raphaely 07-12月-94 11000 0
khoo 18-5月 -95 3100 5586.14357
tobias 24-7月 -97 2800 4650.0896
baida 24-12月-97 2900 4035.26125
himuro 15-11月-98 2600 3649.2465
colmenares 10-8月 -99 2500 3362.58829
94。stddev_pop
功能描述:该函数计算总体标准偏离,并返回总体变量的平方根,其返回值与var_pop函数的平方根相同。(standard deviation-population)
sample:下例返回部门20、30、60的薪水值的总体标准偏差
select department_id, last_name, salary,
stddev_pop(salary) over (partition by department_id) as pop_std
from employees
where department_id in (20,30,60);
department_id last_name salary pop_std
------------- ------------------------- ---------- ----------
20 hartstein 13000 3500
20 fay 6000 3500
30 raphaely 11000 3069.6091
30 khoo 3100 3069.6091
30 baida 2900 3069.6091
30 colmenares 2500 3069.6091
30 himuro 2600 3069.6091
30 tobias 2800 3069.6091
60 hunold 9000 1722.32401
60 ernst 6000 1722.32401
60 austin 4800 1722.32401
60 pataballa 4800 1722.32401
60 lorentz 4200 1722.32401
95。stddev_samp
功能描述: 该函数计算累积样本标准偏离,并返回总体变量的平方根,其返回值与var_pop函数的平方根相同。(standard deviation-sample)
sample:下例返回部门20、30、60的薪水值的样本标准偏差
select department_id, last_name, hire_date, salary,
stddev_samp(salary) over
(partition by department_id order by hire_date
rows between unbounded preceding and current row) as cum_sdev
from employees
where department_id in (20,30,60);
department_id last_name hire_date salary cum_sdev
------------- ------------------------- ---------- ---------- ----------
20 hartstein 17-2月 -96 13000
20 fay 17-8月 -97 6000 4949.74747
30 raphaely 07-12月-94 11000
30 khoo 18-5月 -95 3100 5586.14357
30 tobias 24-7月 -97 2800 4650.0896
30 baida 24-12月-97 2900 4035.26125
30 himuro 15-11月-98 2600 3649.2465
30 colmenares 10-8月 -99 2500 3362.58829
60 hunold 03-1月 -90 9000
60 ernst 21-5月 -91 6000 2121.32034
60 austin 25-6月 -97 4800 2163.33077
60 pataballa 05-2月 -98 4800 1982.42276
60 lorentz 07-2月 -99 4200 1925.61678

96。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);
manager_id last_name salary l_csum
---------- ------------------------- ---------- ----------
101 whalen 4400 4400
101 mavris 6500 10900
101 baer 10000 20900
101 greenberg 12000 44900
101 higgins 12000 44900
103 lorentz 4200 4200
103 austin 4800 13800
103 pataballa 4800 13800
103 ernst 6000 19800
108 popp 6900 6900
108 sciarra 7700 14600
108 urman 7800 22400
108 chen 8200 30600
108 faviet 9000 39600
97。var_pop
功能描述:(variance population)该函数返回非空集合的总体变量(忽略null),var_pop进行如下计算:
(sum(expr2) - sum(expr)2 / count(expr)) / count(expr)
sample:下例计算1998年每月销售的累积总体和样本变量(本例在sh用户下运行)
select t.calendar_month_desc,
var_pop(sum(s.amount_sold))
over (order by t.calendar_month_desc) "var_pop",
var_samp(sum(s.amount_sold))
over (order by t.calendar_month_desc) "var_samp"
from sales s, times t
where s.time_id = t.time_id and t.calendar_year = 1998
group by t.calendar_month_desc;
calendar var_pop var_samp
-------- ---------- ----------
1998-01 0
1998-02 6.1321e+11 1.2264e+12
1998-03 4.7058e+11 7.0587e+11
1998-04 4.6929e+11 6.2572e+11
1998-05 1.5524e+12 1.9405e+12
1998-06 2.3711e+12 2.8453e+12
1998-07 3.7464e+12 4.3708e+12
1998-08 3.7852e+12 4.3260e+12
1998-09 3.5753e+12 4.0222e+12
1998-10 3.4343e+12 3.8159e+12
1998-11 3.4245e+12 3.7669e+12
1998-12 4.8937e+12 5.3386e+12
• 98。var_samp
功能描述:(variance sample)该函数返回非空集合的样本变量(忽略null),var_pop进行如下计算:
(sum(expr*expr)-sum(expr)*sum(expr)/count(expr))/(count(expr)-1)
sample:下例计算1998年每月销售的累积总体和样本变量
select t.calendar_month_desc,
var_pop(sum(s.amount_sold))
over (order by t.calendar_month_desc) "var_pop",
var_samp(sum(s.amount_sold))
over (order by t.calendar_month_desc) "var_samp"
from sales s, times t
where s.time_id = t.time_id and t.calendar_year = 1998
group by t.calendar_month_desc;
calendar var_pop var_samp
-------- ---------- ----------
1998-01 0
1998-02 6.1321e+11 1.2264e+12
1998-03 4.7058e+11 7.0587e+11
1998-04 4.6929e+11 6.2572e+11
1998-05 1.5524e+12 1.9405e+12
1998-06 2.3711e+12 2.8453e+12
1998-07 3.7464e+12 4.3708e+12
1998-08 3.7852e+12 4.3260e+12
1998-09 3.5753e+12 4.0222e+12
1998-10 3.4343e+12 3.8159e+12
1998-11 3.4245e+12 3.7669e+12
1998-12 4.8937e+12 5.3386e+12
99。variance
功能描述:该函数返回表达式的变量,oracle计算该变量如下:
如果表达式中行数为1,则返回0
如果表达式中行数大于1,则返回var_samp
sample:下例返回部门30按雇佣日期排序的薪水值的累积变化
select last_name, salary, variance(salary)
over (order by hire_date) "variance"
from employees
where department_id = 30;
last_name salary variance
------------------------- ---------- ----------
raphaely 11000 0
khoo 3100 31205000
tobias 2800 21623333.3
baida 2900 16283333.3
himuro 2600 13317000
colmenares 2500 11307000
100。rank
功能描述:根据order by子句中表达式的值,从查询返回的每一行,计算它们与其它行的相对位置。组内的数据按order by子句排序,
然后给每一行赋一个号,从而形成一个序列,该序列从1开始,往后累加。每次order by表达式的值发生变化时,该序列也随之增加。
有同样值的行得到同样的数字序号(认为null时相等的)。然而,如果两行的确得到同样的排序,则序数将随后跳跃。若两行序数为1,
则没有序数2,序列将给组中的下一行分配值3,dense_rank则没有任何跳跃。
sample:下例中计算每个员工按部门分区再按薪水排序,依次出现的序列号(注意与dense_rank函数的区别)
select d.department_id , e.last_name, e.salary, rank()
over (partition by e.department_id order by e.salary) as drank
from employees e, departments d
where e.department_id = d.department_id
and d.department_id in ('60', '90');
department_id last_name salary drank
------------- ------------------------- ---------- ----------
60 lorentz 4200 1
60 austin 4800 2
60 pataballa 4800 2
60 ernst 6000 4
60 hunold 9000 5
90 kochhar 17000 1
90 de haan 17000 1
90 king 24000 3

101。ratio_to_report
功能描述:该函数计算expression/(sum(expression))的值,它给出相对于总数的百分比,即当前行对sum(expression)的贡献。
sample:下例计算每个员工的工资占该类员工总工资的百分比
select last_name, salary, ratio_to_report(salary) over () as rr
from employees
where job_id = 'pu_clerk';
last_name salary rr
------------------------- ---------- ----------
khoo 3100 .223021583
baida 2900 .208633094
tobias 2800 .201438849
himuro 2600 .18705036
colmenares 2500 .179856115
• 102。regr_ (linear regression) functions
功能描述:这些线性回归函数适合最小二乘法回归线,有9个不同的回归函数可使用。
regr_slope:返回斜率,等于covar_pop(expr1, expr2) / var_pop(expr2)
regr_intercept:返回回归线的y截距,等于
avg(expr1) - regr_slope(expr1, expr2) * avg(expr2)
regr_count:返回用于填充回归线的非空数字对的数目
regr_r2:返回回归线的决定系数,计算式为:
if var_pop(expr2) = 0 then return null
if var_pop(expr1) = 0 and var_pop(expr2) != 0 then return 1
if var_pop(expr1) > 0 and var_pop(expr2 != 0 then
return power(corr(expr1,expr),2)
regr_avgx:计算回归线的自变量(expr2)的平均值,去掉了空对(expr1, expr2)后,等于avg(expr2)
regr_avgy:计算回归线的应变量(expr1)的平均值,去掉了空对(expr1, expr2)后,等于avg(expr1)
regr_sxx: 返回值等于regr_count(expr1, expr2) * var_pop(expr2)
regr_syy: 返回值等于regr_count(expr1, expr2) * var_pop(expr1)
regr_sxy: 返回值等于regr_count(expr1, expr2) * covar_pop(expr1, expr2)
(下面的例子都是在sh用户下完成的)
sample 1:下例计算1998年最后三个星期中两种产品(260和270)在周末的销售量中已开发票数量和总数量的累积斜率和回归线的截距
select t.fiscal_month_number "month", t.day_number_in_month "day",
regr_slope(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_month_desc, t.day_number_in_month) as cum_slope,
regr_intercept(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_month_desc, t.day_number_in_month) as cum_icpt
from sales s, times t
where s.time_id = t.time_id
and s.prod_id in (270, 260)
and t.fiscal_year=1998
and t.fiscal_week_number in (50, 51, 52)
and t.day_number_in_week in (6,7)
order by t.fiscal_month_desc, t.day_number_in_month;
month day cum_slope cum_icpt
---------- ---------- ---------- ----------
12 12 -68 1872
12 12 -68 1872
12 13 -20.244898 1254.36735
12 13 -20.244898 1254.36735
12 19 -18.826087 1287
12 20 62.4561404 125.28655
12 20 62.4561404 125.28655
12 20 62.4561404 125.28655
12 20 62.4561404 125.28655
12 26 67.2658228 58.9712313
12 26 67.2658228 58.9712313
12 27 37.5245541 284.958221
12 27 37.5245541 284.958221
12 27 37.5245541 284.958221
sample 2:下例计算1998年4月每天的累积交易数量
select unique t.day_number_in_month,
regr_count(s.amount_sold, s.quantity_sold)
over (partition by t.fiscal_month_number order by t.day_number_in_month)
"regr_count"
from sales s, times t
where s.time_id = t.time_id
and t.fiscal_year = 1998 and t.fiscal_month_number = 4;
day_number_in_month regr_count
------------------- ----------
1 825
2 1650
3 2475
4 3300
.
26 21450
30 22200
sample 3:下例计算1998年每月销售量中已开发票数量和总数量的累积回归线决定系数
select t.fiscal_month_number,
regr_r2(sum(s.amount_sold), sum(s.quantity_sold))
over (order by t.fiscal_month_number) "regr_r2"
from sales s, times t
where s.time_id = t.time_id
and t.fiscal_year = 1998
group by t.fiscal_month_number
order by t.fiscal_month_number;
fiscal_month_number regr_r2
------------------- ----------
1
2 1
3 .927372984
4 .807019972
5 .932745567
6 .94682861
7 .965342011
8 .955768075
9 .959542618
10 .938618575
11 .880931415
12 .882769189
sample 4:下例计算1998年12月最后两周产品260的销售量中已开发票数量和总数量的累积平均值
select t.day_number_in_month,
regr_avgy(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_month_desc, t.day_number_in_month)
"regr_avgy",
regr_avgx(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_month_desc, t.day_number_in_month)
"regr_avgx"
from sales s, times t
where s.time_id = t.time_id
and s.prod_id = 260
and t.fiscal_month_desc = '1998-12'
and t.fiscal_week_number in (51, 52)
order by t.day_number_in_month;
day_number_in_month regr_avgy regr_avgx
------------------- ---------- ----------
14 882 24.5
14 882 24.5
15 801 22.25
15 801 22.25
16 777.6 21.6
18 642.857143 17.8571429
18 642.857143 17.8571429
20 589.5 16.375
21 544 15.1111111
22 592.363636 16.4545455
22 592.363636 16.4545455
24 553.846154 15.3846154
24 553.846154 15.3846154
26 522 14.5
27 578.4 16.0666667
sample 5:下例计算产品260和270在1998年2月周末销售量中已开发票数量和总数量的累积regr_sxy, regr_sxx, and regr_syy统计值
select t.day_number_in_month,
regr_sxy(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_year, t.fiscal_month_desc) "regr_sxy",
regr_syy(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_year, t.fiscal_month_desc) "regr_syy",
regr_sxx(s.amount_sold, s.quantity_sold)
over (order by t.fiscal_year, t.fiscal_month_desc) "regr_sxx"
from sales s, times t
where s.time_id = t.time_id
and prod_id in (270, 260)
and t.fiscal_month_desc = '1998-02'
and t.day_number_in_week in (6,7)
order by t.day_number_in_month;
day_number_in_month regr_sxy regr_syy regr_sxx
------------------- ---------- ---------- ----------
1 18870.4 2116198.4 258.4
1 18870.4 2116198.4 258.4
1 18870.4 2116198.4 258.4
1 18870.4 2116198.4 258.4
7 18870.4 2116198.4 258.4
8 18870.4 2116198.4 258.4
14 18870.4 2116198.4 258.4
15 18870.4 2116198.4 258.4
21 18870.4 2116198.4 258.4
22 18870.4 2116198.4 258.4
• 103。row_number
功能描述:返回有序组中一行的偏移量,从而可用于按特定标准排序的行号。
sample:下例返回每个员工再在每个部门中按员工号排序后的顺序号
select department_id, last_name, employee_id, row_number()
over (partition by department_id order by employee_id) as emp_id
from employees
where department_id < 50;
department_id last_name employee_id emp_id
------------- ------------------------- ----------- ----------
10 whalen 200 1
20 hartstein 201 1
20 fay 202 2
30 raphaely 114 1
30 khoo 115 2
30 baida 116 3
30 tobias 117 4
30 himuro 118 5
30 colmenares 119 6
40 mavris 203 1
104。stddev
功能描述:计算当前行关于组的标准偏离。(standard deviation)
sample:下例返回部门30按雇佣日期排序的薪水值的累积标准偏离
select last_name, hire_date,salary,
stddev(salary) over (order by hire_date) "stddev"
from employees
where department_id = 30;
last_name hire_date salary stddev
------------------------- ---------- ---------- ----------
raphaely 07-12月-94 11000 0
khoo 18-5月 -95 3100 5586.14357
tobias 24-7月 -97 2800 4650.0896
baida 24-12月-97 2900 4035.26125
himuro 15-11月-98 2600 3649.2465
colmenares 10-8月 -99 2500 3362.58829

105。stddev_pop
功能描述:该函数计算总体标准偏离,并返回总体变量的平方根,其返回值与var_pop函数的平方根相同。(standard deviation-population)
sample:下例返回部门20、30、60的薪水值的总体标准偏差
select department_id, last_name, salary,
stddev_pop(salary) over (partition by department_id) as pop_std
from employees
where department_id in (20,30,60);
department_id last_name salary pop_std
------------- ------------------------- ---------- ----------
20 hartstein 13000 3500
20 fay 6000 3500
30 raphaely 11000 3069.6091
30 khoo 3100 3069.6091
30 baida 2900 3069.6091
30 colmenares 2500 3069.6091
30 himuro 2600 3069.6091
30 tobias 2800 3069.6091
60 hunold 9000 1722.32401
60 ernst 6000 1722.32401
60 austin 4800 1722.32401
60 pataballa 4800 1722.32401
60 lorentz 4200 1722.32401
106。stddev_samp
功能描述: 该函数计算累积样本标准偏离,并返回总体变量的平方根,其返回值与var_pop函数的平方根相同。(standard deviation-sample)
sample:下例返回部门20、30、60的薪水值的样本标准偏差
select department_id, last_name, hire_date, salary,
stddev_samp(salary) over
(partition by department_id order by hire_date
rows between unbounded preceding and current row) as cum_sdev
from employees
where department_id in (20,30,60);
department_id last_name hire_date salary cum_sdev
------------- ------------------------- ---------- ---------- ----------
20 hartstein 17-2月 -96 13000
20 fay 17-8月 -97 6000 4949.74747
30 raphaely 07-12月-94 11000
30 khoo 18-5月 -95 3100 5586.14357
30 tobias 24-7月 -97 2800 4650.0896
30 baida 24-12月-97 2900 4035.26125
30 himuro 15-11月-98 2600 3649.2465
30 colmenares 10-8月 -99 2500 3362.58829
60 hunold 03-1月 -90 9000
60 ernst 21-5月 -91 6000 2121.32034
60 austin 25-6月 -97 4800 2163.33077
60 pataballa 05-2月 -98 4800 1982.42276
60 lorentz 07-2月 -99 4200 1925.61678

107。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);
manager_id last_name salary l_csum
---------- ------------------------- ---------- ----------
101 whalen 4400 4400
101 mavris 6500 10900
101 baer 10000 20900
101 greenberg 12000 44900
101 higgins 12000 44900
103 lorentz 4200 4200
103 austin 4800 13800
103 pataballa 4800 13800
103 ernst 6000 19800
108 popp 6900 6900
108 sciarra 7700 14600
108 urman 7800 22400
108 chen 8200 30600
108 faviet 9000 39600
108。var_pop
功能描述:(variance population)该函数返回非空集合的总体变量(忽略null),var_pop进行如下计算:
(sum(expr2) - sum(expr)2 / count(expr)) / count(expr)
sample:下例计算1998年每月销售的累积总体和样本变量(本例在sh用户下运行)
select t.calendar_month_desc,
var_pop(sum(s.amount_sold))
over (order by t.calendar_month_desc) "var_pop",
var_samp(sum(s.amount_sold))
over (order by t.calendar_month_desc) "var_samp"
from sales s, times t
where s.time_id = t.time_id and t.calendar_year = 1998
group by t.calendar_month_desc;
calendar var_pop var_samp
-------- ---------- ----------
1998-01 0
1998-02 6.1321e+11 1.2264e+12
1998-03 4.7058e+11 7.0587e+11
1998-04 4.6929e+11 6.2572e+11
1998-05 1.5524e+12 1.9405e+12
1998-06 2.3711e+12 2.8453e+12
1998-07 3.7464e+12 4.3708e+12
1998-08 3.7852e+12 4.3260e+12
1998-09 3.5753e+12 4.0222e+12
1998-10 3.4343e+12 3.8159e+12
1998-11 3.4245e+12 3.7669e+12
1998-12 4.8937e+12 5.3386e+12
109。var_samp
功能描述:(variance sample)该函数返回非空集合的样本变量(忽略null),var_pop进行如下计算:
(sum(expr*expr)-sum(expr)*sum(expr)/count(expr))/(count(expr)-1)
sample:下例计算1998年每月销售的累积总体和样本变量
select t.calendar_month_desc,
var_pop(sum(s.amount_sold))
over (order by t.calendar_month_desc) "var_pop",
var_samp(sum(s.amount_sold))
over (order by t.calendar_month_desc) "var_samp"
from sales s, times t
where s.time_id = t.time_id and t.calendar_year = 1998
group by t.calendar_month_desc;
calendar var_pop var_samp
-------- ---------- ----------
1998-01 0
1998-02 6.1321e+11 1.2264e+12
1998-03 4.7058e+11 7.0587e+11
1998-04 4.6929e+11 6.2572e+11
1998-05 1.5524e+12 1.9405e+12
1998-06 2.3711e+12 2.8453e+12
1998-07 3.7464e+12 4.3708e+12
1998-08 3.7852e+12 4.3260e+12
1998-09 3.5753e+12 4.0222e+12
1998-10 3.4343e+12 3.8159e+12
1998-11 3.4245e+12 3.7669e+12
1998-12 4.8937e+12 5.3386e+12
110。variance
功能描述:该函数返回表达式的变量,oracle计算该变量如下:
如果表达式中行数为1,则返回0
如果表达式中行数大于1,则返回var_samp
sample:下例返回部门30按雇佣日期排序的薪水值的累积变化
select last_name, salary, variance(salary)
over (order by hire_date) "variance"
from employees
where department_id = 30;
last_name salary variance
------------------------- ---------- ----------
raphaely 11000 0
khoo 3100 31205000
tobias 2800 21623333.3
baida 2900 16283333.3
himuro 2600 13317000
colmenares 2500 11307000