常用操作
1.字符串分割
函數:split_part(String text,delimiter text,field int)
描述:根據delimiter分割string返回生成的第field個子字符串(1爲基數)
例子:split_part(‘abc|def|ghi’,’|’,2) --def
2.字符串替換
函數:replace(String text,from text,to text)
描述:把字符串String中出現的所有子字符串from替換成子字符串to
例子:replace(‘abcdefabcdef’,‘cd’,‘XX’) --abXXefabXXef
3.獲取當前時間
函數:current_date
描述:當前的日期
例子:SELECT CURRENT_DATE ---- 2019-11-11 date類型
4.獲取兩個時間點相隔幾天
方法一:SELECT to_timestamp(‘2019-11-11’,‘yyyy-mm-dd’) - to_timestamp(‘2019-10-11’,‘yyyy-mm-dd’) interval_day ---- “31 days” interval 類型
方法二:SELECT extract(day from to_timestamp(‘2019-11-11’,‘yyyy-mm-dd’) - to_timestamp(‘2019-10-11’,‘yyyy-mm-dd’)) interval_day ---- 31 double類型
5.時間加減
select ‘2019-11-11 10:00:00’::timestamp + interval ‘10 days 2 hours 10 seconds’ ---- “2019-11-21 12:00:10” timestamp without time zone 類型
6.序列號生成
函數:generate_series(s,e,i)
描述:序列號生成從s生成到e,中間間隔i(默認爲0)
例子:SELECT sum(num) from generate_series(1,11,2) num ; ---- 36
實例1 某列相同的數改爲不同作爲主鍵 (手機號碼原來是相同的,通過序列函數相減,轉成主鍵)
SELECT user_number::numeric - bb
FROM (
select “用戶手機號” user_number,row_number() over () rn from bdrpt.user LIMIT 500) aa
,generate_series(1,100) bb
WHERE rn = bb ;
7.多行值拼接
函數:string_agg(str,’|’ order by str)
描述:將str先排序再按照分隔符拼接起來
例子:SELECT id,string_agg(str,’|’ order by str) from (values(‘1’,‘word’),(‘1’,‘database’),(‘2’,‘greenplun’)) t(id,str) group by id; ----
id string_agg
1 database|word
2 greenplun
8.字符串拆分成多行
函數:regexp_split_to_table(str,E’\|’)
描述:將拼接好的str重新拆分
例子:SELECT id,regexp_split_to_table(str,E’\|’) from (values(‘1’,‘word|database|redis’),(‘1’,‘database’),(‘2’,‘greenplun’)) t(id,str) ; ----
id regexp_split_to_table
1 word
1 database
1 redis
1 database
2 greenplun
9.字符串加密
函數:md5 ,hashbpchar
描述:hash算法加密精確度md5 (128位),hashbpchar(32位)
例子:SELECT md5(‘helloworld’) ---- fc5e038d38a57032085441e7fe7010b0 字符串類型
SELECT hashbpchar(‘helloworld’) ---- 252807993 integer類型
10.字符串正則匹配
與like相似,支持正則語法。
例如:匹配’b’或者’d’的模糊匹配。
select ‘abc’ SIMILAR TO ‘%(b|d)%’ ---- t
11.字符串正則截取
解析URL
select substring(url,E’\w+://([\w.]+)’) as host
,split_part(url,’?’,1) as url
,substring(url,E’member[_]?[i|I]d=(\w+)’) as member_id
,regexp_split_to_array(split_part(url,’?’,2),’&’) as paras
from (values(‘https://www.baidu.com/s?ie=UTF-8&wd=greenplum’)) t(url)
12.字段間比較大小
select bill_fee,bill_fee_zd
,greatest(bill_fee,bill_fee_zd)
,least(bill_fee,bill_fee_zd)
from test
where bill_fee<>bill_fee_zd
13.有序集聚集函數
函數:percentile_cont(fraction) WITHIN GROUP (ORDER BY sort_expression)
描述: 連續百分率,返回一個對應於排序中指定分數的值,如有必要就在相鄰的輸入項之間插值.如中位數如果結果條數是奇書取中間數,如果是偶數是中間兩個數相加除以二
函數:percentile_disc(fraction) WITHIN GROUP (ORDER BY sort_expression)
描述: 離散百分率,返回第一個在排序中位置等於或者超過指定分數的輸入值
註釋:fraction要求0-1,sort_expression支持表達式
例子:
select percentile_cont(0.5) WITHIN GROUP (ORDER BY fee*1000)
,percentile_disc(0.5) WITHIN GROUP (ORDER BY fee*1000)
from (values(1),(3),(4),(8)) col(fee)
14.分組函數
函數:GROUPING(args…)
描述:如果對應的表達式被包含在分組集生成的結果行的分組條件中,那麼每位是0, 如果不是,則爲1。
例子:
select city_code,net_type_code
,GROUPING(city_code,net_type_code) rn
,sum(bill_fee)
from test
GROUP BY ROLLUP(city_code,net_type_code)
order by city_code,net_type_code
函數:GROUPING SETS、CUBE和ROLLUP
例子1:
select city_code,net_type_code,flag_prod_structure_change
,sum(bill_fee)
from anrpt.rpt_busi_user_income_analyse_info_m_hb_1_prt_p_201807
group by grouping sets((city_code,net_type_code),(city_code,flag_prod_structure_change),(city_code,net_type_code,flag_prod_structure_change))
order by city_code,net_type_code,flag_prod_structure_change
例子2:
select city_code,net_type_code,flag_prod_structure_change
,sum(bill_fee)
from anrpt.rpt_busi_user_income_analyse_info_m_hb_1_prt_p_201807
group by cube (city_code,net_type_code,flag_prod_structure_change)
order by city_code,net_type_code,flag_prod_structure_change
例子3:
select city_code,net_type_code,flag_prod_structure_change
,sum(bill_fee)
from anrpt.rpt_busi_user_income_analyse_info_m_hb_1_prt_p_201807
group by rollup(city_code,net_type_code,flag_prod_structure_change)
order by city_code,net_type_code,flag_prod_structure_change
15.窗口函數
函數:row_number()
描述:當前行在其分區中的行號,從1計
函數:rank()
描述:帶間隙的當前行排名; 與該行的第一個同等行的row_number相同
函數:dense_rank()
描述:不帶間隙的當前行排名; 這個函數計數同等組
函數:percent_rank()
描述:當前行的相對排名: (rank- 1) / (總行數 - 1)
例子:
select id,fee
,row_number() over (partition by id order by fee) row_number
,rank() over (partition by id order by fee) rank
,dense_rank() over (partition by id order by fee) dense_rank
,percent_rank() over (partition by id order by fee) percent_rank
from (values(1,3),(1,4),(1,4),(1,5),(2,8)) col(id,fee)