SQL常用函數(多平台對比)


SQL常用函數(多平台對比)

一、數值型函數

(一)Teradata

abs(-10)  --絕對值;結果:10

ceiling(11.999)  --向上取整;返回不小於11.999的最小整數,結果:12
floor(11.999)  --向下取整;返回不大於11.999的最大整數,結果:11

exp(2)  --e的指數;結果:e的2次冪
log(100)  --底數為10,100的對數;結果:2
ln(4)  --4自然對數

sqrt(16)  --平方根;結果:4
power(3,2)  --求冪;結果:9

102 mod 10  --取余;結果:2

round(1.235,2)  --四舍五入;結果:保留2位小數,1.24
trunc(1.235,2)  --截斷數字;結果:1.23
random(1,100)  --返回一個隨機數;結果:1-100之間的一個隨機整數
sign(-10)  --返回數值符號,結果為 -1、1或0;結果:-1

zeroifnull(null)  --將null轉換成0

--三角函數
sin(x)  --正弦
asin(x)  --反正弦
cos(x)  --余弦
acos(x)  --反余弦
tan(x)  --正切
atan(x)  --反正切

--雙曲函數
sinh(x)  --雙曲正弦
asinh(x)  --反雙曲正弦
cosh(x)  --雙曲余弦
acosh(x)  --反雙曲余弦
tanh(x)  --雙曲正切
atanh(x)  --反雙曲正切

(二)Oracle

abs(-10)  --絕對值;結果:10

ceil(11.999)  --向上取整;返回不小於11.999的最小整數,結果:12
floor(11.999)  --向下取整;返回不大於11.999的最大整數,結果:11

exp(2)  --e的指數;結果:e的2次冪
log(100,10)  --底數為10,100對數;結果:2
ln(4)  --自然對數

sqrt(16)  --平方根;結果:4
power(3,2)  --求冪;結果:9

mod(102,10)  --取余;結果:2

round(1.235,2)  --四舍五入;結果:保留2位小數,1.24
trunc(1.235,2)  --截斷數字;結果:1.23
sign(-10)  --返回數值符號,結果為 -1、1或0;結果:-1

--三角函數
sin(x)  --正弦
asin(x)  --反正弦
cos(x)  --余弦
acos(x)  --反余弦
tan(x)  --正切
atan(x)  --反正切

(三)Mysql

--待填坑

(四)Hive

abs(-10)  --絕對值;結果:10

ceil(11.999)  --向上取整;返回不小於11.999的最小整數,結果:12
ceiling(11.999)  --向上取整;返回不小於11.999的最小整數,結果:12
floor(11.999)  --向下取整;返回不大於11.999的最大整數,結果:11

exp(2)  --e的指數;結果:e的2次冪
log10(100)  --底數為10,100的對數;結果:2
log2(4)  --底數為2,4的對數;結果:2
log(2,4)  --底數為2,4的對數;結果:2
ln(4)  --4的自然對數

sqrt(16)  --平方根;結果:4
pow(3,2)  --求冪;結果:9

pmod(102,10)  --取余;結果:2

round(1.235,2)  --四舍五入;結果:保留2位小數,1.24
rand()  --返回一個0-1之間的隨機數;結果:0-1之間的一個隨機數

sign(-10)  --返回數值符號,結果為 -1、1或0;結果:-1
positive(10)  --取原數;結果:10
positive(-10)  --取原數;結果:-10
negative(10)  --取反;結果:-10
negative(-10)  --取反;結果:10

--三角函數
sin(x)  --正弦
asin(x)  --反正弦
cos(x)  --余弦
acos(x)  --反余弦
tan(x)  --正切
atan(x)  --反正切

二、文本函數

(一)Teradata

'str_1'||'str_2'  --拼接;結果:'str_1str_2'
lpad('a',10,'#')  --把“#”不斷添加到字符串左側,直到字符串滿足10位長度。結果:#########a
rpad('a',10,'#')  --結果:a#########

ascii('y')  --將字符串轉換為ascii碼;結果:121
chr(121)  --將ascii碼轉換為字符串;結果:y

initcap('hello world')  --將首字母變為大寫;結果:'Hello World'
lower('Abc')  --將字符串全部轉換成小寫字母;結果:abc
upper('abc')  --將字符串全部轉換成大寫字母;結果:ABC

instr('str', 'r')  --搜索在字符串'str'中是否出現'r',返回0或1;結果:1
length('abc')  --返回字符串長度;結果:3

index('str_r','r')  --定位函數;結果:在'str_r'中搜尋r的位置,返回3
position('r' in 'str')  --定位函數;結果:'r'在'str'中的位置,返回3

substr('str',1,2)  --截取函數;結果:從第1個字符開始,截取2個字符,即:'tr'
substring('str' from 1 for 2)  --同上

strtok(待分割字符串, 分隔符, 返回第幾個)
strtok('http://www.teradata.com/', '/.', 3)  --結果:'teradata'
strtok_split_to_table  --類似strtok,返回一個table

reverse('abc')  --反轉函數;結果:'cba'

oreplace('a####b###c', '#', '*')  --將字符串中所有的#替換成*
otranslate('s***tr st***r str***', 'str', 'new')  --能夠跨字節替換;結果:'n***ew ne***w new***'
otranslate('s***tr st***r str***', 'str', 'ne')  --能夠跨字節替換;結果:'n***e ne*** ne***'

trim(both '*' from '**str**')  --去除字符串兩側的空格;結果:'str'
trim('  str  ')  --去除字符串兩側的空格;結果:'str
ltrim('  str  ')  --去除字符串左側空格;結果:'str  '
ltrim('  aaastr', 'a')  --去除字符串左側的空格;結果:'aaastr',因為a沒有左頂格,所以去不掉
ltrim('aaastr', 'a')  --去除字符串左側的a;結果:'str'
rtrim('  str  ')  --去除字符串右側空格;結果:'  str'
rtrim('   str***  ', '*')  --去除字符串右側的'*';結果:'   str***',*沒有右頂格,去不掉
rtrim('   str***', '*')  --去除字符串右側的'*';結果:'   str'

ngram(字符串1, 字符串2, 匹配粒度)
ngram('abc', '*ab*bc*', 1)  --對比'abc'與'*ab*bc*'在長度粒度為1的情況下,有幾個能匹配上;'a'、'b'、'c' 結果:3
ngram('abc', '*ab*bc*', 2)  --對比'abc'與'*ab*bc*'在長度粒度為2的情況下,有幾個能匹配上;'ab'、'bc' 結果:2
ngram('abc', '*ab*bc*', 3)  --對比'abc'與'*ab*bc*'在長度粒度為3的情況下,有幾個能匹配上;'abc' 結果:0

nvp(目標字符串, 標簽字符串, 標簽分隔符, 值分隔符, 當有同一個標簽名出現時匹配第幾個標簽的值)
nvp('name1:n1**name2=n2_1&name2#n2_2=name3%n3', 'name1', '** & =', ': = # %', 1)  --結果:n1
nvp('name1:n1**name2=n2_1&name2#n2_2=name3%n3', 'name1', '** & =', ': = # %', 2)  --結果:null
nvp('name1:n1**name2=n2_1&name2#n2_2=name3%n3', 'name2', '** & =', ': = # %', 1)  --結果:n2_1
nvp('name1:n1**name2=n2_1&name2#n2_2=name3%n3', 'name2', '** & =', ': = # %', 2)  --結果:n2_2

(二)Oracle

'str_1'||'str_2'  --拼接;結果:'str_1str_2'
concat('str1','str2')  --拼接;結果:'str1str2'
lpad('a',10,'#')  --把“#”不斷添加到字符串左側,直到字符串滿足10位長度。結果:#########a
rpad('a',10,'#')  --結果:a#########

ascii('y')  --將字符串轉換為ascii碼;結果:121
chr(121)  --將ascii碼轉換為字符串;結果:y

initcap('hello world')  --將首字母變為大寫;結果:'Hello World'
lower('Abc')  --將字符串全部轉換成小寫字母;結果:abc
upper('abc')  --將字符串全部轉換成大寫字母;結果:ABC

instr('重慶某某軟件公司', '某', 1, 2)  --(全角算1個字符)在字符串'重慶某某軟件公司'中從第1個字節開始搜索'某',返回第2次出現的位置;結果:4
instrb('重慶某某軟件公司', 1, 2)  --(全角算2個字符)在字符串'重慶某某軟件公司'中從第1個字節開始搜索'某',返回第2次出現的位置;結果:7
length('重慶')  --返回字符串長度;結果:2
lengthb('重慶')  --返回字符串長度;結果:4

substr('重慶某某軟件公司',3,2)  --(全角算1個字符)截取函數;結果:從第3個字符開始,截取2個字符,即:'某某'
substrb('重慶某某軟件公司',3,2)  --(全角算2個字符)截取函數;結果:從第3個字符開始,截取2個字符,即:'慶某'

replace('a####b###c', '#', '*')  --將字符串中所有的#替換成*
translate('s***tr st***r str***', 'str', 'new')  --能夠跨字節替換;結果:'n***ew ne***w new***'
translate('s***tr st***r str***', 'str', 'ne')  --能夠跨字節替換;結果:'n***e ne*** ne***'

trim(both '*' from '**str**')  --去除字符串兩側的空格;結果:'str'
trim('  str  ')  --去除字符串兩側的空格;結果:'str
ltrim('  str  ')  --去除字符串左側空格;結果:'str  '
ltrim('  aaastr', 'a')  --去除字符串左側的空格;結果:'aaastr',因為a沒有左頂格,所以去不掉
ltrim('aaastr', 'a')  --去除字符串左側的a;結果:'str'
rtrim('  str  ')  --去除字符串右側空格;結果:'  str'
rtrim('   str***  ', '*')  --去除字符串右側的'*';結果:'   str***',*沒有右頂格,去不掉
rtrim('   str***', '*')  --去除字符串右側的'*';結果:'   str'

(三)Mysql

--待填坑

(四)Hive

'str_1'||'str_2'  --拼接;結果:'str_1str_2'
concat('str1','str2')  --拼接;結果:'str1str2'
lpad('a',10,'#')  --把“#”不斷添加到字符串左側,直到字符串滿足10位長度。結果:#########a
rpad('a',10,'#')  --結果:a#########

initcap('hello world')  --將首字母變為大寫;結果:'Hello World'
lower('Abc')  --將字符串全部轉換成小寫字母;結果:abc
upper('abc')  --將字符串全部轉換成大寫字母;結果:ABC

instr('str', 'r')  --搜索在字符串'str'中是否出現'r',返回0或1;結果:1
length('abc')  --返回字符串長度;結果:3

locate('str_r','r')  --定位函數;結果:在'str_r'中搜尋r的位置,返回3

substr('str',1,2)  --截取函數;結果:從第1個字符開始,截取2個字符,即:'tr'
substring('str',1,2)  --同上

replace('a####b###c', '#', '*')  --將字符串中所有的#替換成*
repeat('str',2)  --將'str'重復2遍;結果:'strstr'

trim(both '*' from '**str**')  --去除字符串兩側的空格;結果:'str'
trim('  str  ')  --去除字符串兩側的空格;結果:'str
ltrim('  str  ')  --去除字符串左側空格;結果:'str  '
ltrim('  aaastr', 'a')  --去除字符串左側的空格;結果:'aaastr',因為a沒有左頂格,所以去不掉
ltrim('aaastr', 'a')  --去除字符串左側的a;結果:'str'
rtrim('  str  ')  --去除字符串右側空格;結果:'  str'
rtrim('   str***  ', '*')  --去除字符串右側的'*';結果:'   str***',*沒有右頂格,去不掉
rtrim('   str***', '*')  --去除字符串右側的'*';結果:'   str'

regexp_extract('foothebar', 'foo(.*?)(bar)', 2)  --正則匹配;結果:'bar'
regexp_replace('foobar', 'oo|ar', '')  --正則替換;結果:'fb'

三、聚合函數

(一)Teradata

avg()  --平均數
count()  --計數
sum()  --求和
max()  --最大值
min()  --最小值

(二)Oracle

avg()  --平均數
count()  --計數
sum()  --求和
max()  --最大值
min()  --最小值

(三)Mysql

avg()  --平均數
count()  --計數
sum()  --求和
max()  --最大值
min()  --最小值

(四)Hive

avg()  --平均數
count()  --計數
sum()  --求和
max()  --最大值
min()  --最小值

四、轉換函數

(一)Teradata

coalesce(arg1,arg2)  --如果arg1為null,則返回arg2,否則返回arg1

cast(arg1 as integer)  --將arg1轉化成integer格式
cast(arg1 as date format'yyyy-mm-dd')  --將arg1轉化成日期'yyyy-mm-dd'格式

date'2020-01-01'  --結果:2020-01-01
time'18:01:15'  --結果:18:01:15
timestamp'2020-01-01 16:01:01'  --結果:2020-01-01 16:01:01.0
to_date('20200101', 'yyyymmdd')  --結果:2020-01-01
to_timestamp('January 15, 1989, 11:00 A.M.', 'Month dd, YYYY, HH: MI A.M.')  --結果:1989-01-15 11:00:00.000000
to_number('123')  --結果:123
to_char(123)  --結果:'123'

(二)Oracle

--待填坑

(三)Mysql

--待填坑

(四)Hive

--待填坑

五、時間函數

(一)Teradata

current_date  --當前日期,2020-09-19
current_time  --當前時間,09:44:05+00:00
current_timestamp  --當前時間戳,2020-09-19 09:44:33.160000+00:00

last_day(date'2020-01-01')  --定位2020-01-01所在月份的最后一天;結果:2020-01-31
next_day(date'2020-01-01','monday')  --定位2020-01-01后的第一個星期一
months_between(date'2020-02-28', date'2020-01-01')  --返回2020-01-01和2020-02-28之間的月份數,可能為小數;結果:1.8709...
add_months(date'2020-01-31', 1)  --在2020-01-31的基礎上加上1個月
oadd_months(date'2020-01-31', 1)  --在2020-01-31的基礎上加上1個月

trunc(date'2020-02-13', 'd')  --截斷日期,返回目標日期的指定截斷點上,'D'表示截斷到目標日期所在周的第一天,'MM'表示所在月初第一天,'Y'表示所在年初第一天;結果:2020-02-09
trunc(date'2020-02-13', 'mm')  --結果:2020-02-01

round(date'2020-02-13', 'mm')  --四舍五入時間,如果時間靠近月末,就取下月初日期,如果靠近月初,就取月初時間;結果:2020-02-01
round(date'2020-02-18', 'mm')  --結果:2020-03-01

extract(day from date'2020-01-03')  --時間抽取函數,從目標時間中抽取日,結果:3
extract(month from date'2020-01-03')  --時間抽取函數,從目標時間中抽取月,結果:1
extract(year from date'2020-01-03')  --時間抽取函數,從目標時間中抽取年,結果:2020

(二)Oracle

--待填坑

(三)Mysql

--待填坑

(四)Hive

--待填坑

六、窗口函數

(一)Teradata

--累加函數
csum(累加列, 排序列 <asc or desc>) <group by 聚合列>

--累積分布函數
cume_dist() over(<partition by 分區列> order by 累積列 <asc or desc> <nulls first or nulls last> <reset when condition>)

--移動平均
mavg(求平均列, 窗口步長, 排序列 <asc or desc>) <group by 聚合列>

--移動求和
msum(求和列, 窗口步長, 排序列 <asc or desc>) <group by 聚合列>

--移動差分
mdiff(差分列, 窗口步長, 排序列 <asc or desc>) <group by 聚合列>

--排序函數
rank(排序列 <asc or desc>)  --相同數值,排序相同,排序斷開 
rank() over(<partition by 分區列> order by 累積排序列 <asc or desc> <nulls first or nulls last> <reset when condition>)  --相同數值,排序相同,排序斷開
dense_rank() over(<partition by 分區列> order by 累積排序列 <asc or desc> <nulls first or nulls last> <reset when condition>)  --相同數值,排序相同,排序連續
row_number() over(<partition by 分區列> order by 累積排序列 <asc or desc> <nulls first or nulls last> <reset when condition>)  --相同數值,排序不同,排序連續
percent_rank() over(<partition by 分區列> order by 累積排序列 <asc or desc> <nulls first or nulls last> <reset when condition>)  --排序比例,即(rank排序數值-1)/(記錄數-1)

sel * from 目標表 qualify row_number() <其他窗口函數也可> over(...) = 1;  --從表中查詢排序為1的記錄

--分位插值函數
percentile_cont(分位值[0-1]) within group (order by 目標排序列 <asc or desc> <nulls first or nulls last>)  --返回位於該分位點應該有的值
percentile_disc(分位值[0-1]) within group (order by 目標排序列 <asc or desc> <nulls first or nulls last>)  --返回位於該分位點應該有的值
median(目標列)  --類似percentile_cont(0.5)

--分類函數
qauntile(4, 目標列 <asc or desc>)  --將目標列分為4類,然后按照類大小排序,返回排序值

--線性回歸函數
mlinreg(預測列, 回歸窗口, 排序列[自變量])  --返回預測值

(二)Oracle

--待填坑

(三)Mysql

--待填坑

(四)Hive

--待填坑

七、統計相關函數(聚合)

(一)Teradata

--方差(分母不減1)
var_pop(col)
--方差(分母減1)
var_samp(col)

--標准差(分母不減1)
stddev_pop(col)
--標准差(分母減1)
stddev_samp(col)

--協方差(分母不減1)
covar_pop(col_1, col_2)
--協方差(分母減1)
covar_samp(col_1, col_2)

--相關系數
corr(col1, col2)

--峰度
kurtosis(col)
--偏度
skew(col)

--一元線性回歸自變量平均值函數
regr_avgx(因變量列, 自變量列)
--一元線性回歸因變量平均值函數
regr_avgy(因變量列, 自變量列)
--一元線性回歸斜率函數
regr_slope(因變量列, 自變量列)  --返回參數值
--一元線性回歸截距函數
regr_intercept(因變量列, 自變量列)  --返回截距值
--R^2
regr_r2(因變量列, 自變量列)
--sum(x**2) - sum(x)*(sum(x)/n)
regr_sxx(因變量列, 自變量列)
--sum(x*y) - sum(x)*(sum(y)/n)
regr_sxy(因變量列, 自變量列)
--sum(y**2) - sum(y)*(sum(y)/n)
regr_syy(因變量列, 自變量列)

(二)Oracle

--待填坑

(三)Mysql

--待填坑

(四)Hive

--同Teradata

八、比較函數(非聚合)

(一)Teradata

greatest(1,3,4,6,7)  --最大值;結果:7
least(1,3,4,6,7)  --最小值;結果:1

(二)Oracle

--待填坑

(三)Mysql

--待填坑

(四)Hive

greatest(1,3,4,6,7)  --最大值;結果:7
least(1,3,4,6,7)  --最小值;結果:1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM