110个Oracle 常用函数的总结(88~95)来源: 发布时间:星期三, 2008年12月17日 浏览:14次 评论:0
88PERCENTILE_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 89RANK 功能描述:根据ORDER BY子句中表达式值从查询返回每行计算它们和其它行相对位置组内数据按ORDER BY子句排序 然后给每行赋个号从而形成个序列该序列从1开始往后累加每次ORDER BY表达式值发生变化时该序列也随的增加 有同样值行得到同样数字序号(认为null时相等)然而如果两行确得到同样排序则序数将随后跳跃若两行序数为1 则没有序数2序列将给组中下行分配值3DENSE_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 90RATIO_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 91REGR_ (Linear Regression) Functions 功能描述:这些线性回归适合最小 2乘法回归线有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 NULL If VAR_POP(expr1) = 0 and VAR_POP(expr2) != 0 then 1 If VAR_POP(expr1) > 0 and VAR_POP(expr2 != 0 then 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年最后 3个星期中两种产品(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 92ROW_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 93STDDEV 功能描述:计算当前行有关组标准偏离(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 94STDDEV_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 0
相关文章读者评论
发表评论 |