110个oracle常用函数总结(8)
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 Baida Tobias Himuro Colmenares
REGR_ (Linear Regression) Functions功能描述 这些线性回归函数适合最小二乘法回归线 有 个不同的回归函数可使用
REGR_SLOPE 返回斜率 等于COVAR_POP(expr expr ) / VAR_POP(expr ) REGR_INTERCEPT 返回回归线的y截距 等于AVG(expr ) REGR_SLOPE(expr expr ) * AVG(expr )
REGR_COUNT 返回用于填充回归线的非空数字对的数目
REGR_R 返回回归线的决定系数 计算式为
If VAR_POP(expr ) = then return NULL
If VAR_POP(expr ) = and VAR_POP(expr ) != then return If VAR_POP(expr ) > and VAR_POP(expr != then return POWER(CORR(expr expr) ) REGR_AVGX 计算回归线的自变量(expr )的平均值 去掉了空对(expr expr )后 等于AVG(expr ) REGR_AVGY 计算回归线的应变量(expr )的平均值 去掉了空对(expr expr )后 等于AVG(expr ) REGR_SXX 返回值等于REGR_COUNT(expr expr ) * VAR_POP(expr ) REGR_SYY 返回值等于REGR_COUNT(expr expr ) * VAR_POP(expr ) REGR_SXY: 返回值等于REGR_COUNT(expr expr ) * COVAR_POP(expr expr )
(下面的例子都是在SH用户下完成的)
SAMPLE 下例计算 年最后三个星期中两种产品( 和 )在周末的销售量中已开发票数量和总数量的累积斜率和回归线的截距
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 ( ) AND t fiscal_year= AND t fiscal_week_number IN ( ) AND t day_number_in_week IN ( ) ORDER BY t fiscal_month_desc t day_number_in_month;
Month Day CUM_SLOPE CUM_ICPT
SAMPLE 下例计算 年 月每天的累积交易数量
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 = AND t fiscal_month_number = ;
DAY_NUMBER_IN_MONTH Regr_Count
SAMPLE 下例计算 年每月销售量中已开发票数量和总数量的累积回归线决定系数
SELECT t fiscal_month_number REGR_R (SUM(s amount_sold) SUM(s quantity_sold)) OVER (ORDER BY t fiscal_month_number) Regr_R FROM sales s times t WHERE s time_id = t time_id AND t fiscal_year = GROUP BY t fiscal_month_number ORDER BY t fiscal_month_number;
FISCAL_MONTH_NUMBER Regr_R
SAMPLE 下例计算 年 月最后两周产品 的销售量中已开发票数量和总数量的累积平均值
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 = AND t fiscal_month_desc = AND t fiscal_week_number IN ( ) ORDER BY t day_number_in_month;
DAY_NUMBER_IN_MONTH Regr_AvgY Regr_AvgX
SAMPLE 下例计算产品 和 在 年 月周末销售量中已开发票数量和总数量的累积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 ( ) AND t fiscal_month_desc = AND t day_number_in_week IN ( ) ORDER BY t day_number_in_month;
DAY_NUMBER_IN_MONTH Regr_sxy Regr_syy Regr_sxx
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 < ;
DEPARTMENT_ID LAST_NAME EMPLOYEE_ID EMP_ID
Whalen
Hartstein
Fay
Raphaely
Khoo
Baida
Tobias
Himuro
Colmenares
Mavris
STDDEV功能描述 计算当前行关于组的标准偏离 (Standard Deviation)
SAMPLE 下例返回部门 按雇佣日期排序的薪水值的累积标准偏离
SELECT last_name hire_date salary
STDDEV(salary) OVER (ORDER BY hire_date) StdDev
FROM employees
WHERE department_id = ;
LAST_NAME HIRE_DATE SALARY StdDev
Raphaely 月
Khoo 月 Tobias 月 Baida 月 Himuro 月 Colmenares 月
STDDEV_POP功能描述 该函数计算总体标准偏离 并返回总体变量的平方根 其返回值与VAR_POP函数的平方根相同 (Standard Deviation-Population)
SAMPLE 下例返回部门 的薪水值的总体标准偏差
SELECT department_id last_name salary
STDDEV_POP(salary) OVER (PARTITION BY department_id) AS pop_std
FROM employees
WHERE department_id in ( );
DEPARTMENT_ID LAST_NAME SALARY POP_STD
Hartstein
Fay
Raphaely Khoo Baida Colmenares Himuro Tobias Hunold Ernst Austin Pataballa Lorentz
STDDEV_SAMP功能描述 该函数计算累积样本标准偏离 并返回总体变量的平方根 其返回值与VAR_POP函数的平方根相同 (Standard Deviation-Sample)
SAMPLE 下例返回部门 的薪水值的样本标准偏差
SELECT department_id last_name hire_date salary
STDDEV_SAMP(salary) OVER
(PARTITION BY department_id ORDER BY hire_date
ROWS BEEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cum_sdev
FROM employees
WHERE department_id in ( );
DEPARTMENT_ID LAST_NAME HIRE_DATE SALARY CUM_SDEV
Hartstein 月
Fay 月
Raphaely 月
Khoo 月 Tobias 月 Baida 月 Himuro 月 Colmenares 月
Hunold 月
Ernst 月 Austin 月 Pataballa 月 Lorentz 月
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 ( );
MANAGER_ID LAST_NAME SALARY L_CSUM
Whalen
Mavris
Baer
Greenberg
Higgins
Lorentz
Austin
Pataballa
Ernst
Popp
Sciarra
Urman
Chen
Faviet
VAR_POP功能描述 (Variance Population)该函数返回非空集合的总体变量(忽略null) VAR_POP进行如下计算
(SUM(expr ) SUM(expr) / COUNT(expr)) / COUNT(expr)
SAMPLE 下例计算 年每月销售的累积总体和样本变量(本例在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 = GROUP BY t calendar_month_desc;
CALENDAR Var_Pop Var_Samp
E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+
VAR_SAMP功能描述 (Variance Sample)该函数返回非空集合的样本变量(忽略null) VAR_POP进行如下计算
(SUM(expr*expr) SUM(expr)*SUM(expr)/COUNT(expr))/(COUNT(expr) )
SAMPLE 下例计算 年每月销售的累积总体和样本变量
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 = GROUP BY t calendar_month_desc;
CALENDAR Var_Pop Var_Samp
E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+ E+
VARIANCE功能描述 该函数返回表达式的变量 Oracle计算该变量如下 如果表达式中行数为 则返回 如果表达式中行数大于 则返回VAR_SAMP
SAMPLE 下例返回部门 按雇佣日期排序的薪水值的累积变化
SELECT last_name salary VARIANCE(salary)
OVER (ORDER BY hire_date) Variance
FROM employees
WHERE department_id = ;
LAST_NAME SALARY Variance
Raphaely
Khoo
Tobias Baida
Himuro
lishixinzhi/Article/program/Oracle/201311/17896