gp常用函数
1.字符串函数
函数 | 返回类型 | 描述 | 例子 | 结果 |
---|---|---|---|---|
string||string | text | 字符串连接 | ||
length(string) | int | string中字符的数目 | length(‘jose’) | 4 |
position(substring in string) | int | 指定的子字符串的位置 | position(‘om’in’Tomas’) | 3 |
substring(string[from int][for int]) | text | 抽取子字符串 | substring(‘Thomas’from 2 for 3) | hom |
trim([leading|trailing|both][characters]from string) | text | 从字符串string的开头/结尾/两边删除只包含characters中字符(默认是空白)的最长的字符串 | trim(both ‘x’ from ‘xTomxx’) | Tom |
lower(string) | text | 把字符串转化为小写 | ||
upper(string) | text | 把字符串转化为大写 | ||
overlay(string placing string from int [for int]) | text | 替换子字符串 | overlay(‘Txxxxas’ placing ‘hom’ from 2 for 4) | Thomas |
replace(string text,from text,to text) | text | 把字符串string中出现的所有子字符串from替换成子字符串to | replace(‘abcdefabcdef’,’cd,’XX’) | abXXefabXXef |
split_part(string text, delimiter text,filed int) | text | 根据delimiter分隔string返回生成的第field个子字符串(1开始) | split_part(‘abc|def|ghi’,’|’,2) | def |
string_to_array(text , ',') | text[] | 根据 , 分割字符串 并且返回数组 |
2.时间函数
函数 | 返回类型 | 描述 | 例子 | 结果 | |
---|---|---|---|---|---|
age(timestamp,timestamp) | interval | 减去参数后的”符号化”结果 | age(timestamp’2001-04-10’,timestamp’1957-06-13) | 43 years 9 mons 27 das | |
age(timestamp) | interval | 从current_date减去参数中的日期 | age(timestam’1957-06-13) | - | |
current_date | date | 当前的日期 | - | - | |
current_time | time with time zone | 当日时间 | - | - | |
current_timestamp | timestamp with time zone | 当前事务开始时的事件戳 | - | - | |
date_part(text,timestamp) | double precision | 获取子域(等效于extract) | date_part(‘hour’,timestamp’2001-02-16 20:38:40) | 20 | |
date_trunc(text,timestamp) | timestamp | 截断成指定的精度 | date_trunc(‘hour’,timestamp ‘2001-02-16 20:38:40’) | 2001/2/16 20:00 | |
extract(field from timestamp) | double precision | 获取子域 |
field:day 月份里面的日期 year/month/ 年份/月份 isdow 星期一:1 星期日:7 doy 一年中的第几天 hour/minute/second 第几小时/分/秒 quarter 季度(1-4)
|
(同date_part) | |
now() | timestampe with time zone | 当前事务开始的时间戳 | - | - |
3.窗口函数
函数 | 返回类型 | 描述 | 例子 | 结果 |
---|---|---|---|---|
聚合函数 | ||||
sum(x) over() | 对x列每个region进行累加 | |||
count(x) over | int | 对x列每个region进行计数 | ||
分组排序 | ||||
row_number() over(x) | 对每个region排序 | partition by region||city_name order by create_time | ||
rank() over(x) | 对每个region排名,留空位 | 1,1,3 | ||
dense_rank() over(x) | ||||
分组查询 | ||||
first_value(x) | 与x相同 | |||
ntile(x) over(y) |
|
1.依据创建时间数据5等分 2.依据创建时间数据把每个region2等分 | ||
偏移分析 | ||||
lag(exp_str,offset,defval) over() |
exp_str 字段名 ,offset 偏移量 ,defval 默认值 前n行 | |||
lead() over() | 后n行 |
4. 序列函数
函数 | 返回类型 | 描述 | 例子 | 结果 |
---|---|---|---|---|
generate_series(start,stop.step) | int | 生成一个数值序列,从start 到 stop,步进为step | ||
generate_series(start, stop, step_interval) | timestamp or timestamp with time zone | 生成一个数值序列,从start 到 stop,步进为step |
5.数组函数
函数 | 返回类型 | 描述 | 例子 | 结果 |
---|---|---|---|---|
string_to_array(text , ',') | text[] | 根据 , 分割字符串 并且返回数组 | ||
array_to_string (anyarray, text [, text]) | text | 根据 , 分割字符串 并且返回字符串 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)