PostgreSQL 常用函数

2019-04-10  本文已影响0人  越过山丘xyz

数字函数

select abs(-20.5); -- 20.5
select ceil(14.2); -- 15
select floor(14.8); -- 14
select least(11, 4, 5);
select mod(9, 4); -- 1
select round(11.2); -- 11
select round(11.8); -- 12

聚集函数

数据准备:

create table table9(
  id int4,
  num int4
);
insert into table9 (id, num) values (1, 12);
insert into table9 (id, num) values (1, 15);
insert into table9 (id, num) values (2, 11);
insert into table9 (id, num) values (3, 17);
insert into table9 (id, num) values (3, 14);
select count(*) from table9; -- 5
select id, avg(num) from table9 group by id;
select id, max(num) from table9 group by id;
select id, min(num) from table9 group by id;
select id, sum(num) from table9 group by id;

字符函数

数据准备:

create table table8(
  id int4,
  name text,
  line text
);
insert into table8 (id, name, line) values (1001, 'aladdin', 'xAddcwWf345ww');
select char_length(line) from table8; -- 13
select concat(id, name, '&&') from table8; -- 1001aladdin&&
select concat_ws('_', id, name, line) from table8; -- 1001_aladdin_xAddcwWf345ww
-- 从 3 开始,截取 2 个
select substring(line, 3, 2) from table8;
select split_part('aladdin.im', '.', 1); -- aladdin
select upper(line) from table8;
select lower(line) from table8; -- xaddcwwf345ww
select ltrim('  xxdd  ');
select rtrim('  xx  ');
select position('Add' in line) from table8;
select repeat(name, 3) from table8; -- aladdinaladdinaladdin
-- 将所有w替换成*
select replace(line, 'w', '*') from table8; -- xAddc*Wf345**
select left(line, 5) from table8;
select right(line, 3) from table8; -- 5ww
-- 字符不满 10 个,用 # 补满
select rpad(name, 10, '#') from table8;

日期函数

select extract(year from now()); -- 2019
-- week 一年中第几周
-- doy 一年中第几天
select now() + interval '1 year'; -- 2020-04-10 09:43:35...
select age('2018-5-23'::date, '2015-5-23'::date); -- 3 years 0 mons 0 days 0 hours 0 mins 0.00 secs

聚合函数

-- 第一个参数为合并字段,第二个参数为分隔符
-- 可以用于分组合并
select id, string_agg(name, ',') from table5 group by id; 
select id, array_agg(name) from table5 group by id;
select id, array_to_string(array_agg(name), ',') from table5 group by id;
select array_to_json(arr_int) from table6;

转化函数

create table public.table3(
msg text
);
insert into public.table3(msg) values ('ALADDIN 2019');
-- translate 函数
select 
     translate(msg, '0123456789ABCD', '##########%%%%') 
from table3; -- %L%%%IN ####
select 
     replace(msg, 'ALADDIN', 'HELLO') 
from table3; -- HELLO 2019
-- 将 null -> 0
select coalesce(t2.sal, 0) from table2 t2;

窗口函数

窗口函数不会将结果集进行分组计算输出一行,而是将计算后的结果集输出到结果集上,可以简化 SQL 代码。
一般情况下,窗口函数能做的事情,都是可以通过复杂的 SQL 语句来实现。

create table student_score(
stu_id int4,
subject text,
score double precision
);
insert into student_info(id, name) values (1001, 'aladdin'), (1002, 'bilib'), (1003, 'chrome');
insert into
student_score(stu_id, subject, score)
values
       (1001, 'chinese', 90.0), (1001, 'math', 100.0), (1001, 'english', 80.0),
       (1002, 'chinese', 100.0), (1002, 'math', 80.0), (1002, 'english', 60.0),
       (1003, 'chinese', 60), (1003, 'math', 90), (1003, 'english', 100.0);
-- 统计学个各科成绩和各科的平均成绩
select
     ss.subject, si.name, ss.score, avg(ss.score) over(partition by ss.subject)
from
   student_info si left join student_score ss on si.id = ss.stu_id;
select 
     si.*, ss.subject, ss.score, row_number() over (partition by ss.subject order by ss.score) 
from 
   student_info si left join student_score ss on si.id = ss.stu_id;
rank() 的排序结果可能是:1, 1, 3, 4, 4, 6
dense_rank() 的排序结果是:1, 1, 2, 3, 3, 4
select
     si.*, ss.subject, ss.score, lag(ss.score, 1, '100.0') over(partition by ss.subject order by score)
     -- lag(field, offset, default value),如果向上偏 offset = -X
from
   student_info si left join student_score ss on si.id = ss.stu_id;
select
     ss.subject ,first_value(ss.score) over (partition by subject order by score)
from
   student_score ss;
select
     ss.subject ,nth_value(ss.score, 2) over (partition by subject order by score)
from
   student_score ss;
上一篇 下一篇

猜你喜欢

热点阅读