3.postgresql--时间函数

select extract(year from timestamp '2023-01-01 13:00:32');
select extract(month from timestamp '2023-01-01 13:00:32');

select date_trunc('day',current_timestamp);
select date_trunc('week',current_timestamp);
select date_trunc('month',current_timestamp);
select date_trunc('year',current_timestamp);

select now() - interval '1 week';

select now() - interval '1 month';

select now() - interval '1 year';

3.postgresql--时间函数_第1张图片

CREATE OR REPLACE FUNCTION load_data_to_tbl_operlog(row_num int) RETURNS INTEGER AS
$BODY$
declare
	max_operlog_id bigint;
BEGIN
drop SEQUENCE if exists tbl_operlog_id_seq;
select max(operlog_id) INTO max_operlog_id  from tbl_operlog;
max_operlog_id :=COALESCE(max_operlog_id ,0)+1;
execute 'create SEQUENCE if not exists tbl_operlog_id_seq   start with '||max_operlog_id ;
insert into tbl_operlog 
select 
	nextval('tbl_operlog_id_seq'), 
	n||'imos_name', 
	now(), 
	'127.0.0.1' as ip_address,
	random()*10 as service_type,
	n||'oper_object' as oper_object,
	random()*10 as oper_type,
	random()*10 as oper_result,
	n||'description',
	n||'failure_cause'
from  generate_series(1,row_num) n;
          RETURN 0;
END;
$BODY$
LANGUAGE 'plpgsql' ;

select generate_series(3,5);
3.postgresql--时间函数_第2张图片

select generate_series(3,5),generate_series(4,6) 步长一样
3.postgresql--时间函数_第3张图片

select generate_series (date_trunc('h', CURRENT_TIMESTAMP - interval '23 h'),date_trunc('h', 			    
CURRENT_TIMESTAMP),'1 h' ) as date_series;

select date_trunc('h', CURRENT_TIMESTAMP - interval '23 h');

select date_trunc('h', CURRENT_TIMESTAMP);

3.postgresql--时间函数_第4张图片

select date_series ,
to_char(date_series, 'yyyyMMddhh24miss') as dayid,
to_char(date_series, 'hh24:mi') as "timeStr",
(to_char(date_series, 'hh24:mi') || '~' || to_char(date_series + interval '1 h', 'hh24:mi')) as time_frame

from
(
select generate_series (date_trunc('h', CURRENT_TIMESTAMP - interval '23 h'),date_trunc('h', 			    
CURRENT_TIMESTAMP),'1 h' ) as date_series) tab

3.postgresql--时间函数_第5张图片

当前日期划分为24小时

select date_series ,
to_char(date_series, 'yyyyMMddhh24miss') as dayid,
to_char(date_series, 'hh24:mi') as "timeStr",
(to_char(date_series, 'hh24:mi') || '~' || to_char(date_series + interval '1 h', 'hh24:mi')) as time_frame

from
(
select generate_series (date_trunc('h', CURRENT_TIMESTAMP - interval '23 h'),date_trunc('h', 			    
CURRENT_TIMESTAMP),'1 h' ) as date_series) tab
where date_trunc('day', date_series) = date_trunc('day', CURRENT_TIMESTAMP) 

3.postgresql--时间函数_第6张图片

--  当前天往前推29天,封装一个月的日期
select * from (select generate_series(date_trunc('day',CURRENT_DATE) -'29 d'::interval,
date_trunc('day',CURRENT_DATE),
'1 d'::interval
)::date dayid) t where dayid<=date_trunc('day',CURRENT_DATE)

--  当前时间往前推四周。封装一个月的日期
select dayid || '~' ||
CASE WHEN  date_trunc('day', CURRENT_TIMESTAMP(0)) > date_trunc('day',dayid::TIMESTAMP)+'6 day'::interval 
THEN to_char(date_trunc('day', dayid::TIMESTAMP) +'6 day'::interval,'yyyy-MM-dd') 
ELSE to_char(date_trunc('day', CURRENT_TIMESTAMP(0)),'yyyy-MM-dd') END AS dayid
from (select generate_series(date_trunc('week',CURRENT_DATE)::date -'3 week'::interval,
date_trunc('week',CURRENT_DATE)::date,
'1 week'::interval
)::date dayid) t where dayid<=date_trunc('week','2022-06-15'::date)	


-- 当前月往前推11个月,封装一年的日期
select to_char(dayid::date,'yyyy-MM') as dayid
from generate_series(date_trunc('month',CURRENT_DATE)::date - '11 month'::interval,
date_trunc('month', CURRENT_DATE)::date,
'1 month'::interval
) dayid where dayid::date<=date_trunc('month',CURRENT_DATE)

你可能感兴趣的:(数据库,postgresql,数据库)