1 2 3

postgresql 教程

时间函数

一、获取系统时间函数

1.1 获取当前完整时间

select now();

》2021-12-10 11:13:39.56

current_timestamp 同 now() 函数等效。

1.2 获取当前日期

select current_date;

》2021-12-10

1.3 获取当前时间

select current_time;

》11:24:09.98352

select substr(current_time,1,8);

》11:24:48

1.4时区

with和without time zone两者有什么区别

1.区别

1)名字上看一个是带时区的,另一个是不带时区的,查出来的时间是一样的,只是一个带时区标志,一个不带而已,时区的基准是格林威治时间UTC。
2)这对于数据的显示上来说,区别就是时间数据的末尾带不带时区标志,即+/-时区,比如中国(prc),时区是东八区,带时区标志的话就是+08。

+08:表示 时区与全球统一时间 UTC 偏移量为 8 小时

without time zone

不带时区

select  now()::timestamp without time zone

结果:2021-08-11 18:26:34.751754

select '2021-12-10 12:00:00'::timestamp with time zone

结果:2021-12-10 12:00:00+08

with time zone

带时区

select  now()::timestamp with time zone

结果:2021-08-11 18:27:53.889618+08

at time zone 'GMT-8'

AT TIME ZONE 构造允许把时间戳转换成不同的时区与timezone(zonetimestamp) 函数等效

指定分区类型

select now()::timestamp at time zone 'GMT-8'

结果:2021-08-11 18:28:46.212463+08

select now()::timestamp at time zone 'HKT'

结果为:2021-08-11 18:30:44.73845+08

1.5获取当前日期+时间

select current_timestamp

》2021-12-10 11:54:10.755707

二、时间字段的截取 

在开发过程中,经常要取日期的年,月,日,小时等值,PostgreSQL 提供一个非常便利的EXTRACT函数。

extract函数格式:

extract (field from source)

field 表示取的时间对象,source 表示取的日期来源,类型为 timestamp、time 或 interval。

source必须是timestamp、time、interval类型的值表达式。field是一个标识符或字符串,是从源数据中的抽取的域。

2.1 取年份

select extract(year from now());

》2021

2.2 取月份/日

select extract(month from now());

》12

select extract(day from timestamp '2021-12-10');

timestamp 是为了指定后面的日期为时间类型

》10

2.3 查看今天是一年中的第几天

select extract(doy from now());

》334

2.4 查看现在距1970-01-01 00:00:00 UTC 的秒数

即将timestamp 时间格式转换为秒数

(1)精确到秒

select floor(extract(epoch from now()));

》:"1639107955"

(2)精确到秒的小数

select extract(epoch from now());结果:"1574826646.79929"

(3)精确到毫秒:

select floor(extract(epoch from((current_timestamp - timestamp '1970-01-01 00:00:00')*1000)));

2.5时间间隔

 interval [fields][(p)]

select interval '1 year 2 months 3 days 4 hours 5 minutes 6 seconds';

结果:

1 year 2 mons 3 days 04:05:06

间隔一小时是多少秒

 select extract(epoch from interval '+1 hours');

 》3600

date_part函数

date_part函数是仿照在传统的Ingres函数。等效于 SQL 标准函数extract:

date_part('field',source)

SELECT date_part('hour', INTERVAL '4 hours 3 minutes');

Result:4

date_part(‘epoch’, end_time - begin_time)

将时间差转换为秒,然后转换数据类型为数字类型除以60,得到分钟数,::是转换的意思

select date_part('epoch', now() - '2021-08-11 17:00:00')::NUMERIC / 60

2.6 date_trunc截断日期函数,完成定时时间语法

select date_trunc('month',now()) 

》当月1号:2021-12-01 00:00:00

select date_trunc('month',now()) +interval '12 h'

》当月1号,再间隔12小时2021-12-01 12:00:00

select date_trunc('day',now()) + interval '9 h'

》当天9点

select date_trunc('h',now()) + interval '30 minute'

》没hh:30  2021-12-10 12:30:00

 

三、时间戳转时间

SELECT TO_TIMESTAMP(extract(epoch from now()))

》2021-12-10 12:46:58.509472+08

四、时间的计算

两年后

select now() + interval '2 years';

结果:2023-08-11 17:14:54.27425+08

一个月后

select now() + interval '1 month';

三周前

select now() - interval '3 week';

1天前

select now() - interval '1 day';

十分钟后

select now() + '10 min';

计算两个时间差

使用 age(timestamp, timestamp)

select age(now(), timestamp '1989-02-05');

当前时间可以省略

select age(timestamp '2007-09-15');  

timestamp是获取指定时间的年月日 时分秒格式

select timestamp '1989-02-05';

时间差秒数:

语法:

extract(epoch FROM (timeatmp1-timestamp2))

例如:

SELECT
extract(epoch FROM (now() - (to_timestamp(xapp_recently_login_in_time,'yyyy-MM-dd hh24:mi:ss')) ))as gap_time,
to_timestamp(xapp_recently_login_in_time,'yyyy-MM-dd hh24:mi:ss')as new_xapp_recently_login_in_time
,* FROM dws_trfc_soul_seller_recently_login_intime
where buyer_id is not null

将日期转成字符串

to_char(time,'YYYY-MM-DD hh24:mi:ss') as time1,

to_char(time,'YYYY-MM-DD') as time2,

to_char(time,'YYYY-MM-DD hh:mi:ss') as time3

 

换行符和拼接符

'>'||scene_type||'异常类目长总数 '||corp_cnt,E'\n'

拼接使用‘||’,被拼接的是非字段而是符号或者字符,需要单引号括一下,换行符在pgsql中是“E‘\n’”,需要在换行符前加逗号

如果是用chr(10),那么char(10)前都必须用||连接

行转列

case when

select data_dt
,work_we_chat_id
,corp_code
,corp_name
,NVL(zhike_indic_name,'')as zhike_indic_name
,NVL(zhike_indic_value,'')as zhike_indic_value
from (SELECT
substr(data_dt,1,10) as data_dt,work_we_chat_id,corp_code,corp_name
,case when indic_code='deal_by_plt' then indic_name end as zhike_indic_name
,case when indic_code='deal_by_plt' then indic_value end as zhike_indic_value
FROM edw_svc_cs_event_stat_for_test
where length(indic_value)>0
and indic_value>0.0
and indic_code in ('deal_by_plt')
order by work_we_chat_id)t

string_agg

例如:string_agg('>'||scene_type||'异常类目长总数 '||corp_cnt,E'\n') as pinjie 

如果scene_type有多个值,即多行记录,拼接完cnt值后会自动换行取下一个类型的值

达到一个商家打印出多个类型值

同行专列一个效果

 

posted @ 2021-08-26 22:18  teacher-程  阅读(984)  评论(0编辑  收藏  举报
levels of contents