0
点赞
收藏
分享

微信扫一扫

pgsql中的常用函数总结

滚过红尘说红尘 2022-02-08 阅读 83
linqsqlp2p

ps: over()里头的分组以及排序的执行晚于 where 、group by、  order by 的执行

1、 这样的结果是先按照stationcode分组,再按照时间进行排序。第一列为序列号

select row_number() over(partition by stationcode ORDER BY time),stationcode,time from dcsrealtime


2、按照stationcode进行分组后再按照time排序,然后筛选出rank为1的记录

select * from (select row_number() OVER(partition by stationcode ORDER BY time) rank,* from dcsrealtime rank) temp where temp.rank='1'

3、查询表占用大小

    查询占用字节:select  pg_relation_size('tablename');
    查询占用MB:select pg_size_pretty(pg_relation_size('tablename'));
    清空指定表:TRUNCATE TABLE tablename

4、将时间戳转为指定格式

select to_char(to_timestamp(updatetime/1000),'yyyy-MM-dd HH:MI:SS') from dangersource

5、空值替换函数(mysql使用ifnull,pgsql使用coalesce)

select level,coalesce(level,'蓝') from alarms

6、count中添加条件

select count(字段名 LIKE '%true%' or null) as 字段名 from tablename

7、查询表中:指定字段有重复的记录

select * from organization where code in (select code from organization GROUP BY code HAVING count(code)>1) ORDER BY name
 

8、字符连接函数

-- pgsql的三种字符链接函数,将多列的字段连接成一条数据
-- arrary_agg ()  string_agg( )  xmlagg()  分别来处理数组,字符串和xml文档。 

-- array_agg字符链接后是json格式
SELECT array_agg(name ORDER BY name DESC) FROM monitoritem GROUP BY dangersourceid;    
-- string_agg可以指定分隔符
SELECT string_agg(name, ',' ORDER BY name) FROM monitoritem GROUP BY dangersourceid;

-- ps:mysql的字符链接函数
select d.name,wm_concat(m."name") as roleName from dangersource d join monitoritem m on d.id = m.dangersourceid

举报

相关推荐

0 条评论