【gp数据库】你可能不知道却超级实用的函数

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)

在这里插入图片描述

上一篇:【R】linux上安装R及使用shell调用加传参

下一篇:【gp数据库】统计常用窗口函数详解

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章