/*===================================================================
*Author:
*Date:
*Description:ORACLE Function
*==================================================================*/
SQL中的單記錄函數
返回與指定的字符對應的十進制數;
SQL> select ascii(A) A
A A ZERO SPACE
給出整數
SQL> select chr(
ZH C
趙 A
連接兩個字符串;
SQL> select concat(
高乾競電話
返回字符串並將字符串的第一個字母變為大寫;
SQL> select initcap(smith) upp from dual;
UPP
Smith
在一個字符串中搜索指定的字符
C
C
I 搜索的開始位置
J 出現的位置
SQL> select instr(oracle traning
INSTRING
返回字符串的長度;
SQL> select name
NAME LENGTH(NAME) ADDR LENGTH(ADDR) SAL LENGTH(TO_CHAR(SAL))
高乾競
返回字符串
SQL> select lower(AaBbCcDd)AaBbCcDd from dual;
AABBCCDD
aabbccdd
返回字符串
SQL> select upper(AaBbCcDd) upper from dual;
UPPER
AABBCCDD
RPAD 在列的右邊粘貼字符
LPAD 在列的左邊粘貼字符
SQL> select lpad(rpad(gao
LPAD(RPAD(GAO
*******gao*******
不夠字符則用*來填滿
LTRIM 刪除左邊出現的字符串
RTRIM 刪除右邊出現的字符串
SQL> select ltrim(rtrim( gao qian jing
LTRIM(RTRIM(
gao qian jing
取子字符串
SQL> select substr(
SUBSTR(
string 希望被替換的字符或變量
s
s
SQL> select replace(he love you
REPLACE(H
i love you
返回一個與給定的字符串讀音相同的字符串
SQL> create table table
SQL> insert into table
SQL> insert into table
SQL> insert into table
SQL> select xm from table
XM
weather
wether
LEADING 剪掉前面的字符
TRAILING 剪掉後面的字符
如果不指定
返回指定值的絕對值
SQL> select abs(
ABS(
給出反余弦的值
SQL> select acos(
ACOS(
給出反正弦的值
SQL> select asin(
ASIN(
返回一個數字的反正切值
SQL> select atan(
ATAN(
返回大於或等於給出數字的最小整數
SQL> select ceil(
CEIL(
返回一個給定數字的余弦
SQL> select cos(
COS(
返回一個數字反余弦值
SQL> select cosh(
COSH(
返回一個數字e的n次方根
SQL> select exp(
EXP(
對給定的數字取整數
SQL> select floor(
FLOOR(
返回一個數字的對數值
SQL> select ln(
LN(
返回一個以n
SQL> select log(
LOG(
返回一個n
SQL> select mod(
MOD(
返回n
SQL> select power(
POWER(
按照指定的精度進行捨入
SQL> select round(
ROUND(
取數字n的符號
SQL> select sign(
SIGN(
返回一個數字的正弦值
SQL> select sin(
SIN(
返回雙曲正弦的值
SQL> select sin(
SIN(
返回數字n的根
SQL> select sqrt(
SQRT(
返回數字的正切值
SQL> select tan(
TAN(
返回數字n的雙曲正切值
SQL> select tanh(
TANH(
按照指定的精度截取一個數
SQL> select trunc(
TRUNC
增加或減去月份
SQL> select to_char(add_months(to_date(
TO_CHA
SQL> select to_char(add_months(to_date(
TO_CHA
返回日期的最後一天
SQL> select to_char(sysdate
TO_CHAR(SY TO_CHAR((S
SQL> select last_day(sysdate) from dual;
LAST_DAY(S
給出date
SQL> select months_between(
MON_BETWEEN
SQL>selectmonths_between(to_date(
MON_BETW
給出在this時區=other時區的日期和時間
SQL> select to_char(sysdate
BJ_TIME LOS_ANGLES
給出日期date和星期x之後計算下一個星期的日期
SQL> select next_day(
NEXT_DAY
用來得到系統的當前日期
SQL> select to_char(sysdate
TO_CHAR(SYSDATE
trunc(date
SQL> select to_char(trunc(sysdate
HH HHMM
將字符數據類型轉換為ROWID類型
SQL> select rowid
ROWID ROWIDTOCHAR(ROWID) ENAME
AAAAfKAACAAAAEqAAA AAAAfKAACAAAAEqAAA SMITH
AAAAfKAACAAAAEqAAB AAAAfKAACAAAAEqAAB ALLEN
AAAAfKAACAAAAEqAAC AAAAfKAACAAAAEqAAC WARD
AAAAfKAACAAAAEqAAD AAAAfKAACAAAAEqAAD JONES
將源字符串 sset從一個語言字符集轉換到另一個目的dset字符集
SQL> select convert(strutz
conver
strutz
將一個十六進制構成的字符串轉換為二進制
將一個二進制構成的字符串轉換為十六進制
將ROWID數據類型轉換為字符類型
SQL> select to_char(sysdate
TO_CHAR(SYSDATE
將字符串轉化為ORACLE中的一個日期
將字符串中的單字節字符轉化為多字節字符
SQL> select to_multi_byte(高) from dual;
TO
高
將給出的字符轉換為數字
SQL> select to_number(
YEAR
指定一個外部二進制文件
SQL>insert into file_tb
將x字段或變量的源source轉換為desc
SQL> select sid
SID SERIAL# USERNAME CMD
DUMP函數以fmt指定的內部數字格式返回一個VARCHAR
SQL> col global_name for a
SQL> col dump_string for a
SQL> set lin
SQL> select global_name
GLOBAL_NAME DUMP_STRING
ORACLE
這兩個函數都是用來對大數據類型字段進行初始化操作的函數
返回一組表達式中的最大值
SQL> select greatest(AA
GR
AC
SQL> select greatest(啊
GR
天
返回一組表達式中的最小值
SQL> select least(啊
LE
啊
返回標識當前用戶的唯一整數
SQL> show user
USER 為
SQL> select username
USERNAME USER_ID
GAO
返回當前用戶的名字
SQL> select user from dual;
USER
GAO
返回當前用戶環境的信息
ENTRYID
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)
ENTRYID
返回會話人口標志
SQL> select userenv(entryid) from dual;
USERENV(ENTRYID)
INSTANCE
返回當前INSTANCE的標志
SQL> select userenv(instance) from dual;
USERENV(INSTANCE)
LANGUAGE
返回當前環境變量
SQL> select userenv(language) from dual;
USERENV(LANGUAGE)
SIMPLIFIED CHINESE_CHINA
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)
VSIZE(USER) USER
all表示對所有的值求平均值
SQLWKS> create table table
語句已處理
SQLWKS> insert into table
SQLWKS> insert into table
SQLWKS> insert into table
SQLWKS> commit;
SQL> select avg(distinct sal) from gao
AVG(DISTINCTSAL)
SQL> select avg(all sal) from gao
AVG(ALLSAL)
求最大值
SQL> select max(distinct sal) from scott
MAX(DISTINCTSAL)
求最小值
SQL> select min(all sal) from gao
MIN(ALLSAL)
求標准差
SQL> select stddev(sal) from scott
STDDEV(SAL)
SQL> select stddev(distinct sal) from scott
STDDEV(DISTINCTSAL)
求協方差
SQL> select variance(sal) from scott
VARIANCE(SAL)
主要用來對一組數進行統計
SQL> select deptno
DEPTNO COUNT(*) SUM(SAL)
對分組統計再加限制條件
SQL> select deptno
DEPTNO COUNT(*) SUM(SAL)
SQL> select deptno
DEPTNO COUNT(*) SUM(SAL)
用於對查詢到的結果進行排序輸出
SQL> select deptno
DEPTNO ENAME SAL
select (case when DUMMY=
case的第
case col when
when
else
這種用法跟decode一樣沒什麼區別
case的第
case when score <
when score >=
when score >=
else
NVL(expr
NVL
NULLIF (expr
Oracle分析函數參考手冊
=============================================
作者: xsb([url])[/url]
發表於:
分類: DW&BI
出處
Oracle從
而聚合函數對於每個組只返回一行
常用的分析函數如下所列:
row_number() over(partition by
rank() over(partition by
dense_rank() over(partition by
count() over(partition by
max() over(partition by
min() over(partition by
sum() over(partition by
avg() over(partition by
first_value() over(partition by
last_value() over(partition by
lag() over(partition by
lead() over(partition by
下面例子中使用的表來自Oracle自帶的HR用戶下的表
$ORACLE_HOME/demo/schema/human_resources/hr_main
除本文內容外
ROLLUP與CUBE [url][/url]
分析函數使用例子介紹
本文如果未指明
開窗函數的的理解
開窗函數指定了分析函數工作的數據窗口大小
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)
主要參考資料
ohwww
續
功能描述
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
功能描述
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
功能描述
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
功能描述
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
功能描述
返回表達式非空賦值的計數
相同的數據後出現的行數
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
功能描述
在一個
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
功能描述
SAMPLE
SELECT d
OVER (PARTITION BY e
FROM employees e
WHERE e
AND d
DEPARTMENT_ID LAST_NAME SALARY DRANK
功能描述
SAMPLE
SELECT last_name
MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct)
OVER (PARTITION BY department_id)
MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct)
OVER (PARTITION BY department_id)
FROM employees
WHERE department_id in (
ORDER BY department_id
LAST_NAME DEPARTMENT_ID SALARY Worst Best
Fay
Hartstein
Kumar
Banda
Johnson
Ande
Lee
Tuvault
Sewall
Marvins
Bates
功能描述
SAMPLE
SELECT department_id
OVER (PARTITION BY department_id ORDER BY salary ASC ) AS lowest_sal
FROM employees
WHERE department_id in(
DEPARTMENT_ID LAST_NAME SALARY LOWEST_SAL
功能描述
SAMPLE
SELECT last_name
LAG(salary
FROM employees
WHERE job_id =
LAST_NAME HIRE_DATE SALARY PREV_SAL
Khoo
Tobias
Baida
Himuro
Colmenares
功能描述
SAMPLE
SELECT last_name
MIN(salary) KEEP (DENSE_RANK FIRST ORDER BY commission_pct)
OVER (PARTITION BY department_id)
MAX(salary) KEEP (DENSE_RANK LAST ORDER BY commission_pct)
OVER (PARTITION BY department_id)
FROM employees
WHERE department_id in (
ORDER BY department_id
LAST_NAME DEPARTMENT_ID SALARY Worst Best
Fay
Hartstein
Kumar
Banda
Johnson
Ande
Lee
Tuvault
Sewall
Marvins
Bates
功能描述
SAMPLE
SELECT department_id
OVER(PARTITION BY department_id ORDER BY salary) AS highest_sal
FROM employees
WHERE department_id in(
DEPARTMENT_ID LAST_NAME SALARY HIGHEST_SAL
功能描述
SAMPLE
SELECT last_name
LEAD(hire_date
FROM employees WHERE department_id =
LAST_NAME HIRE_DATE NextHired
Raphaely
Khoo
Tobias
Baida
Himuro
Colmenares
功能描述
SAMPLE
SELECT department_id
MAX(salary) OVER (PARTITION BY department_id) AS dept_max
FROM employees WHERE department_id in (
DEPARTMENT_ID LAST_NAME SALARY DEPT_MAX
功能描述
SAMPLE
SELECT department_id
MIN(salary) OVER (PARTITION BY department_id) AS dept_min
FROM employees WHERE department_id in (
DEPARTMENT_ID LAST_NAME SALARY DEPT_MIN
功能描述
SAMPLE
SELECT last_name
NTILE(
WHERE department_id =
LAST_NAME SALARY QUARTILE
Greenberg
Faviet
Chen
Urman
Sciarra
Popp
功能描述
SAMPLE
SELECT department_id
PERCENT_RANK()
OVER (PARTITION BY department_id ORDER BY salary) AS pr
FROM employees
WHERE department_id <
ORDER BY department_id
DEPARTMENT_ID LAST_NAME SALARY PR
功能描述
RN =
CRN = CEIL(RN) FRN = FLOOR(RN)
if (CRN = FRN = RN) then
(value of expression from row at RN)
else
(CRN
(RN
注意
SAMPLE
P=
FRN = FLOOR(
(
SELECT last_name
PERCENTILE_CONT(
OVER (PARTITION BY department_id)
PERCENT_RANK()
OVER (PARTITION BY department_id ORDER BY salary)
FROM employees WHERE department_id IN (
LAST_NAME SALARY DEPARTMENT_ID Percentile_Cont Percent_Rank
Colmenares
Himuro
Tobias
Baida
Khoo
Raphaely
Lorentz
Austin
Pataballa
Ernst
Hunold
功能描述
注意
SAMPLE
SELECT last_name
PERCENTILE_DISC(
OVER (PARTITION BY department_id)
CUME_DIST() OVER (PARTITION BY department_id ORDER BY salary)
FROM employees
WHERE department_id in (
LAST_NAME SALARY DEPARTMENT_ID Percentile_Disc Cume_Dist
Colmenares
Himuro
Tobias
Baida
Khoo
Raphaely
Lorentz
Austin
Pataballa
Ernst
Hunold
功能描述
然後給每一行賦一個號
有同樣值的行得到同樣的數字序號(認為null時相等的)
則沒有序數
SAMPLE
SELECT d
OVER (PARTITION BY e
FROM employees e
WHERE e
AND d
DEPARTMENT_ID LAST_NAME SALARY DRANK
功能描述
SAMPLE
SELECT last_name
FROM employees
WHERE job_id =
LAST_NAME SALARY RR
Khoo
Baida
Tobias
Himuro
Colmenares
功能描述
REGR_SLOPE
REGR_INTERCEPT
AVG(expr
REGR_COUNT
REGR_R
If VAR_POP(expr
If VAR_POP(expr
If VAR_POP(expr
return POWER(CORR(expr
REGR_AVGX
REGR_AVGY
REGR_SXX
REGR_SYY
REGR_SXY: 返回值等於REGR_COUNT(expr
(下面的例子都是在SH用戶下完成的)
SAMPLE
SELECT t
REGR_SLOPE(s
OVER (ORDER BY t
REGR_INTERCEPT(s
OVER (ORDER BY t
FROM sales s
WHERE s
AND s
AND t
AND t
AND t
ORDER BY t
Month Day CUM_SLOPE CUM_ICPT
SAMPLE
SELECT UNIQUE t
REGR_COUNT(s
OVER (PARTITION BY t
FROM sales s
WHERE s
AND t
DAY_NUMBER_IN_MONTH Regr_Count
SAMPLE
SELECT t
REGR_R
OVER (ORDER BY t
FROM sales s
WHERE s
AND t
GROUP BY t
ORDER BY t
FISCAL_MONTH_NUMBER Regr_R
SAMPLE
SELECT t
REGR_AVGY(s
OVER (ORDER BY t
REGR_AVGX(s
OVER (ORDER BY t
FROM sales s
WHERE s
AND s
AND t
AND t
ORDER BY t
DAY_NUMBER_IN_MONTH Regr_AvgY Regr_AvgX
SAMPLE
SELECT t
REGR_SXY(s
OVER (ORDER BY t
REGR_SYY(s
OVER (ORDER BY t
REGR_SXX(s
OVER (ORDER BY t
FROM sales s
WHERE s
AND prod_id IN (
AND t
AND t
ORDER BY t
DAY_NUMBER_IN_MONTH Regr_sxy Regr_syy Regr_sxx
功能描述
SAMPLE
SELECT department_id
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
功能描述
SAMPLE
SELECT last_name
STDDEV(salary) OVER (ORDER BY hire_date)
FROM employees
WHERE department_id =
LAST_NAME HIRE_DATE SALARY StdDev
Raphaely
Khoo
Tobias
Baida
Himuro
Colmenares
功能描述
SAMPLE
SELECT department_id
STDDEV_POP(salary) OVER (PARTITION BY department_id) AS pop_std
FROM employees
WHERE department_id in (
DEPARTMENT_ID LAST_NAME SALARY POP_STD
功能描述
SAMPLE
SELECT department_id
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 (
DEPARTMENT_ID LAST_NAME HIRE_DATE SALARY CUM_SDEV
功能描述
SAMPLE
SELECT manager_id
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
功能描述
(SUM(expr
SAMPLE
SELECT t
VAR_POP(SUM(s
OVER (ORDER BY t
VAR_SAMP(SUM(s
OVER (ORDER BY t
FROM sales s
WHERE s
GROUP BY t
CALENDAR Var_Pop Var_Samp
From:http://tw.wingwit.com/Article/program/Oracle/201311/18274.html