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

news/2025/3/29 9:08:05/

常用操作

    • 1.字符串分割
    • 2.字符串替换
    • 3.获取当前时间
    • 4.获取两个时间点相隔几天
    • 5.时间加减
    • 6.序列号生成
    • 7.多行值拼接
    • 8.字符串拆分成多行
    • 9.字符串加密
    • 10.字符串正则匹配
    • 11.字符串正则截取
    • 12.字段间比较大小
    • 13.有序集聚集函数
    • 14.分组函数
    • 15.窗口函数

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数据库】统计常用窗口函数详解


http://www.ppmy.cn/news/862694.html

相关文章

WGAN-GP 学习笔记

今天看到paperweekly上有人分享了一个WGAN-GP的实现&#xff0c;是以MNIST为数据集&#xff0c;代码简洁&#xff0c;结构清晰。我最近也在看GAN的相关内容&#xff0c;就下载下来做个参考。 代码地址&#xff1a;https://github.com/bojone/gan/ 对于这个基于tensorflow实现…

GP数据库安装以及使用

编 写 潘永雷 时 间 2017年04月13日 说明 本文档用于指导centos下安装gp greenplum-db-4.3.9.1-build-1-rhel5-x86_64.bin,不同环境可能略有不同,在文档所对应的环境下经验证安装成功 1、机器环境 1.1 系统信息 1 2 3 4 [gpadmin@sdw1 ~]$ uname -a Linux sdw1 2.6.32-642…

搭建GP服务

转&#xff1a;http://www.cnblogs.com/potential/archive/2012/10/27/2742355.html 在开始之前想说的话&#xff1a;以下是本人在摸索GP服务时的一点总结&#xff0c;因为很多人问到&#xff0c;所以在此小小的总结一下。由于时间仓促&#xff0c;所以不是很详细&#xff0c;看…

GP(greenplum)4 for RH Linux 6 安装详细过程

本文源自&#xff1a;https://www.cnblogs.com/liuyungao/p/5689588.html 1、准备 这里准备了4台服务器&#xff0c;1台做master&#xff0c;1台做standby&#xff0c;4台都做存储&#xff0c;为了保密真实的IP地址和主机名都换成“艺名”了。 OS:Red Hat Enterprise Linux Se…

GP6创建tablespace 和GP4的差别

目录 一、GP4中的tablespace与filespace 1.1 gp_persistent_tablespace_node 1.2 master上查看filespace 1.3 segment上查看filespace

03 gp 集群搭建

前言 呵呵 最近有一系列环境搭建的相关需求 记录一下 gp 三个节点 : 192.168.110.72, 192.168.110.73, 192.168.110.75 72 为 master, 73 为 slave01, 75 为 slave02 三台机器都做了 trusted shell gp 单节点 docker 搭建 参照 Docker Hub gp 搭建步骤 三节点集群 72, 73…

GP 常用数学函数

1.1 绝对值函数 select abs(-15.2); 结果 15.2 1.2 开立方根 select cbrt(64.0); 结果4 1.3 向上取整 select ceil(2.8)&#xff1b; 结果3 1.4 取商函数 select div(10,3)&#xff1b; 结果3 1.5 向下取整 select floor(-6.8)&#xff1b; 结果-7 1.6 取余数 selec…

GP常用命令

一、####集群启动 su - gpadmin gpstart GP服务启停 su - gpadmin gpstart #正常启动 gpstop #正常关闭 gpstop -M fast #快速关闭 gpstop –r #重启 gpstop –u #重新加载配置文件 二、登录数据库 psql -d ***** #进入某个数据库 postgres# \l # 查询数据库 postgres# \i te…