postgres 常用函数

函数:string || string 
说明:String concatenation 字符串连接操作
例子:'Post' || 'greSQL' = PostgreSQL

函数:lower(string)
说明:Convert string to lower case 转换字符串为小写
例子:bit_length('jose') = 32

函数:position(substring in string)
说明:Location of specified substring 子串在一字符串中的位置
例子:position('om' in 'Thomas') = 3

函数:substring(string [from int] [for int])
说明:Extract substring 截取任意长度的子字符串
例子:

SUBSTRING('breakfast',5) AS col1, // kfast

SUBSTRING('breakfast',5,3) AS col2, //kfa

函数:array_cat(anyarray, anyarray)

连接两个数组,返回新数组

示例:array_cat(ARRAY[1, 2], ARRAY[3, 4])     结果:{1, 2, 3, 4}

         array_cat(ARRAY[[1, 2]], ARRAY[3, 4])   结果:{{1, 2}, {3, 4}}

         array_cat(ARRAY[[1, 2]], ARRAY[[3, 4]])   结果:{{1, 2}, {3, 4}}

函数:array_prepend(anyelement, anyarray)

在数组的开头插入一个元素

示例: array_prepend(7, ARRAY[8, 9])   结果:{7, 8, 9}

array_remove(anyarray, anyelement)

函数:移除数组中为指定值的元素,只支持一维数组

示例: array_remove(ARRAY[1, 2, 3], 2)             结果: {1, 3}

          array_remove(ARRAY[1, 2, 3, 2, 1, 2], 2)   结果: {1, 3. 1}

函数:array_replace(anyarray, anyelement, anyelement)

把数组中等于指定值元素的值用另一个指定值替代

示例:  array_replace(ARRAY[1, 4, 3], 4, 2)  结果:{1,2,3}

函数: array_to_string(anyarray, text)

使用指定的分隔符(第二个参数) 将数组元素连接为字符串

示例:  array_to_string(ARRAY[1,2,3], ',')  结果:'1,2,3'

函数:string_to_array(text, text)

用指定的分隔符分隔的字符串转成数组

示例:string_to_array('1,2,3', ',')   结果:{1, 2, 3}

函数:unnest(anyarray)

把数组变成多行返回

示例:select unnest(ARRAY[1,2,3,4,5])  结果: 

postgres 常用函数_第1张图片

窗口函数 :

窗口函数求和   

select * from
 (
select row_number() over(partition by project_id order by create_time desc,warn_status desc) as index ,sum(warn_time) over(partition by project_id)  as warn_time1,* from xmgl_dzjc_warning
) as t order by t.create_time desc

窗口函数分类排序序号 

 select type,name,price,row_number() over(PARTITION by type order by price asc) as idx from products ;

参考:

PostgreSQL>窗口函数的用法

postgresql 常用函数汇总

你可能感兴趣的:(#,postgres,postgres,函数)