首页 > 数据库 > Oracle > 正文

Oracle 9i 分析函数参考手册

2020-03-09 22:55:08
字体:
来源:转载
供稿:网友
,欢迎访问网页设计爱好者web开发。        oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是对于每个组返回多行,而聚合函数对于每个组只返回一行。下面例子中使用的表来自oracle自带的hr用户下的表,如果没有安装该用户,可以在sys用户下运行$oracle_home/demo/schema/human_resources/hr_main.sql来创建。        少数几个例子需要访问sh用户下的表,如果没有安装该用户,可以在sys用户下运行$oracle_home/demo/schema/sales_history/sh_main.sql来创建。        如果未指明缺省是在hr用户下运行例子。        开窗函数的的理解:        开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下:over(order by salary) 按照salary排序进行累计,order by是个默认的开窗函数over(partition by deptno)按照部门分区over(order by salary range between 50 preceding and 150 following)每行对应的数据窗口是之前行幅度值不超过50,之后行幅度值不超过150over(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章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...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 twhere s.time_id = t.time_id and calendar_year = 1998group by t.calendar_month_numberorder 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 .903542662covar_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 pwhere category_id = 29order 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..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 pwhere category_id = 29order 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..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          3markle                          2200        107          3          2philtanker                      2200        107          3          2landry                          2400        107          5          8gee                             2400        107          5          8colmenares                      2500        107         11         10patel                           2500        107         11         10..cume_dist 功能描述:计算一行在组中的相对位置,cume_dist总是返回大于0、小于或等于1的数,该数表示该行在n行中的位置。例如,在一个3行的组中,返回的累计分布值为1/3、2/3、3/3sample:下例中计算每个工种的员工按薪水排序依次累积出现的分布百分比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         .2pu_clerk   himuro                          2600         .4pu_clerk   tobias                          2800         .6pu_clerk   baida                           2900         .8pu_clerk   khoo                            3100          1pu_man     raphaely                       11000          1dense_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 dwhere 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          2first 功能描述:从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      13000hartstein                            20      13000       6000      13000kumar                                80       6100       6100      14000banda                                80       6200       6100      14000johnson                              80       6200       6100      14000ande                                 80       6400       6100      14000lee                                  80       6800       6100      14000tuvault                              80       7000       6100      14000sewall                               80       7000       6100      14000marvins                              80       7200       6100      14000bates                                80       7300       6100      14000...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 colmenareslag 功能描述:可以访问结果集中的其它行而不用进行自连接。它允许去处理游标,就好像游标是一个数组一样。在给定组中可参考当前行之前的行,这样就可以从组中与当前行一起选择以前的行。offset是一个正整数,其默认值为1,若索引超出窗口的范围,就返回默认值(默认返回的是组中第一行),其相反的函数是leadsample:下面的例子中列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 employeeswhere job_id = 'pu_clerk';last_name                 hire_date      salary   prev_sal------------------------- ---------- ---------- ----------khoo                      18-5月 -95       3100          0tobias                    24-7月 -97       2800       3100baida                     24-12月-97       2900       2800himuro                    15-11月-98       2600       2900colmenares                10-8月 -99       2500       2600last 功能描述:从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      13000hartstein                            20      13000       6000      13000kumar                                80       6100       6100      14000banda                                80       6200       6100      14000johnson                              80       6200       6100      14000ande                                 80       6400       6100      14000lee                                  80       6800       6100      14000tuvault                              80       7000       6100      14000sewall                               80       7000       6100      14000marvins                              80       7200       6100      14000bates                                80       7300       6100      14000...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 raphaelylead 功能描述: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-95khoo                      18-may-95 24-jul-97tobias                    24-jul-97 24-dec-97baida                     24-dec-97 15-nov-98himuro                    15-nov-98 10-aug-99colmenares                10-aug-99max 功能描述:在一个组中的数据窗口中查找表达式的最大值。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      11000min 功能描述:在一个组中的数据窗口中查找表达式的最小值。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       2500ntile 功能描述:将一个组分为"表达式"的散列表示,例如,如果表达式=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 employeeswhere department_id = 100;last_name                     salary   quartile------------------------- ---------- ----------greenberg                      12000          1faviet                          9000          1chen                            8200          2urman                           7800          2sciarra                         7700          3popp                            6900          4percent_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 employeeswhere 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          0percentile_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 = 5760select 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            0himuro                          2600            30            3000          0.2tobias                          2800            30            3000          0.4baida                           2900            30            3000          0.6khoo                            3100            30            3000          0.8raphaely                       11000            30            3000            1lorentz                         4200            60            5760            0austin                          4800            60            5760         0.25pataballa                       4800            60            5760         0.25ernst                           6000            60            5760         0.75hunold                          9000            60            5760            1percentile_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 .166666667himuro                          2600            30            3100 .333333333tobias                          2800            30            3100         .5baida                           2900            30            3100 .666666667khoo                            3100            30            3100 .833333333raphaely                       11000            30            3100          1lorentz                         4200            60            6000         .2austin                          4800            60            6000         .6pataballa                       4800            60            6000         .6ernst                           6000            60            6000         .8hunold                          9000            60            6000          1rank 功能描述:根据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 dwhere 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          3ratio_to_report 功能描述:该函数计算expression/(sum(expression))的值,它给出相对于总数的百分比,即当前行对sum(expression)的贡献。sample:下例计算每个员工的工资占该类员工总工资的百分比select last_name, salary, ratio_to_report(salary) over () as rr  from employeeswhere job_id = 'pu_clerk';last_name                     salary         rr------------------------- ---------- ----------khoo                            3100 .223021583baida                           2900 .208633094tobias                          2800 .201438849himuro                          2600  .18705036colmenares                      2500 .179856115regr_ (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 twhere 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.958221sample 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 twhere 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      22200sample 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 .882769189sample 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.0666667sample 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 twhere 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.4row_number 功能描述:返回有序组中一行的偏移量,从而可用于按特定标准排序的行号。sample:下例返回每个员工再在每个部门中按员工号排序后的顺序号select department_id, last_name, employee_id, row_number()       over (partition by department_id order by employee_id) as emp_id  from employeeswhere 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          1stddev 功能描述:计算当前行关于组的标准偏离。(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          0khoo                      18-5月 -95       3100 5586.14357tobias                    24-7月 -97       2800  4650.0896baida                     24-12月-97       2900 4035.26125himuro                    15-11月-98       2600  3649.2465colmenares                10-8月 -99       2500 3362.58829stddev_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 employeeswhere 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.32401stddev_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 employeeswhere 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.61678sum 功能描述:该函数计算组中表达式的累积和。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              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 twhere s.time_id = t.time_id and t.calendar_year = 1998group by t.calendar_month_desc;calendar    var_pop   var_samp-------- ---------- ----------1998-01           01998-02  6.1321e+11 1.2264e+121998-03  4.7058e+11 7.0587e+111998-04  4.6929e+11 6.2572e+111998-05  1.5524e+12 1.9405e+121998-06  2.3711e+12 2.8453e+121998-07  3.7464e+12 4.3708e+121998-08  3.7852e+12 4.3260e+121998-09  3.5753e+12 4.0222e+121998-10  3.4343e+12 3.8159e+121998-11  3.4245e+12 3.7669e+121998-12  4.8937e+12 5.3386e+12var_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 twhere s.time_id = t.time_id and t.calendar_year = 1998group by t.calendar_month_desc;calendar    var_pop   var_samp-------- ---------- ----------1998-01           01998-02  6.1321e+11 1.2264e+121998-03  4.7058e+11 7.0587e+111998-04  4.6929e+11 6.2572e+111998-05  1.5524e+12 1.9405e+121998-06  2.3711e+12 2.8453e+121998-07  3.7464e+12 4.3708e+121998-08  3.7852e+12 4.3260e+121998-09  3.5753e+12 4.0222e+121998-10  3.4343e+12 3.8159e+121998-11  3.4245e+12 3.7669e+121998-12  4.8937e+12 5.3386e+12variance 功能描述:该函数返回表达式的变量,oracle计算该变量如下:          如果表达式中行数为1,则返回0          如果表达式中行数大于1,则返回var_sampsample:下例返回部门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          0khoo                            3100   31205000tobias                          2800 21623333.3baida                           2900 16283333.3himuro                          2600   13317000colmenares                      2500   11307000
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表