postgreSql 常用查询总结
1. 日期格式转化(参考)
select beg_time, end_time, extract(epoch from to_timestamp(end_time,'yyyy-mm-dd-HH24-MI-SS-US'))-extract(epoch from to_timestamp(beg_time,'yyyy-mm-dd-HH24-MI-SS-US')) from cdb_all_iu_data where beg_time > '2017-09-21'
注:beg_time, end_time以TEXT形式存储,求时间差时转化为时间戳再相减得到结果(s)
2. select * from (中间结果) t
select count(*) from ( select chkid, count(*) from abc_table GROUP BY chkid) t
3.通过查询进行更新的操作
UPDATE tb1 SET c1=b.c1 , c2=b.c2 FROM b WHERE tb1.c3 = b.c3;
4. PostreSQL取出每组第一条(最高)记录
SELECT DISTINCT ON (customer) id, customer, total FROM purchases ORDER BY customer, total DESC, id;
DISTINCT ON ( expression [, …] )把记录根据[, …]的值进行分组,分组之后仅返回每一组的第一行。需要注意的是,如果你不指定ORDER BY子句,返回的第一条的不确定的。