Oracle從
下面例子中使用的表來自Oracle自帶的HR用戶下的表
少數幾個例子需要訪問SH用戶下的表
如果未指明缺省是在HR用戶下運行例子
開窗函數的的理解
開窗函數指定了分析函數工作的數據窗口大小
over(order by salary) 按照salary排序進行累計
over(partition by deptno)按照部門分區
over(order by salary range between
每行對應的數據窗口是之前行幅度值不超過
over(order by salary rows between
每行對應的數據窗口是之前
over(order by salary rows between unbounded preceding and unbounded following)
每行對應的數據窗口是從第一行到最後一行
over(order by salary range between unbounded preceding and unbounded following)
主要參考資料
AVG
功能描述
SAMPLE
SELECT manager_id
AVG(salary) OVER (PARTITION BY manager_id ORDER BY hire_date
ROWS BETWEEN
FROM employees;
MANAGER_ID LAST_NAME HIRE_DATE SALARY C_MAVG
CORR
功能描述
COVAR_POP(expr
SAMPLE
SELECT t
CORR (SUM(s
OVER (ORDER BY t
FROM sales s
WHERE s
GROUP BY t
ORDER BY t
CALENDAR_MONTH_NUMBER CUM_CORR
COVAR_POP
功能描述
SAMPLE
SELECT product_id
COVAR_POP(list_price
OVER (ORDER BY product_id
COVAR_SAMP(list_price
OVER (ORDER BY product_id
FROM product_information p
WHERE category_id =
ORDER BY product_id
PRODUCT_ID SUPPLIER_ID CUM_COVP CUM_COVS
COVAR_SAMP
功能描述
SAMPLE
SELECT product_id
COVAR_POP(list_price
OVER (ORDER BY product_id
COVAR_SAMP(list_price
OVER (ORDER BY product_id
FROM product_information p
WHERE category_id =
ORDER BY product_id
PRODUCT_ID SUPPLIER_ID CUM_COVP CUM_COVS
COUNT
功能描述
SAMPLE
例如
SELECT last_name
COUNT(*) OVER (ORDER BY salary) AS cnt
COUNT(*) OVER (ORDER BY salary RANGE BETWEEN
AND
LAST_NAME SALARY CNT
Olson
Markle
Philtanker
Landry
Gee
Colmenares
Patel
CUME_DIST
功能描述
SAMPLE
SELECT job_id
OVER (PARTITION BY job_id ORDER BY salary) AS cume_dist
FROM employees WHERE job_id LIKE
JOB_ID LAST_NAME SALARY CUME_DIST
PU_CLERK Colmenares
PU_CLERK Himuro
PU_CLERK Tobias
PU_CLERK Baida
PU_CLERK Khoo
PU_MAN Raphaely
DENSE_RANK
功能描述
From:http://tw.wingwit.com/Article/program/Oracle/201311/17275.html