前言
在项目遇到一个需求是查询统计今天、昨天、本周、本月、上月、今年、去年的时间数据,最近一个月的,最近一年的月份数据,使用的是pgsql数据库:
获取当前系统时间
select now(); select current_timestamp; 结果:2020-11-04 16:09:53.247825+08
获取当前日期或时间
select current_date; 结果:2020-11-04
select current_time; 结果:16:14:08.501182+08
查询昨天的数据
select distinct count(id) from 表名 where coalesce(l.join_date,l.sys_createdate) >= current_date - 1; # 这里的coalesce函数,语法:coalesce(expr1,expr2,expr3...) # 如果第一个字段存在就用第一个进行表达式判断; # 如果第一个不存在为null则用第二个进行表达式判断; # 如果都没有null则返回null
查询某个时间的周一
select ( date '2020-10-23' - interval '1 day' - ( extract ( dow from ( date'2020-10-23' - interval '1 day' ) ) - 1 || ' day' ) :: interval ) :: date; # 减1 是因为得到的是以周一是星期的开始
查询本周的数据
select( date ( now() ) - ( extract ( dow from date ( now() ) ) - 1 || ' day' ) :: interval ) :: date startasy, ( date ( now() ) - ( extract ( dow from date ( now() ) ) - 1 || ' day' ) :: interval ) :: date + 6 endday from 表名 limit 1;
本周最后一天
select to_char(current_date +cast(-1*(to_number(to_char(current_date,'d'),'99')-2) + 6 ||' days' as interval),'yyyy-mm-dd'); # 加6天就表示是周日 不加就是本周的第一天
本月,方式一
select to_char((select now() as timestamp),'mm');
本月,方式二
select * from 表名 where time >= date_trunc( 'month', now() );
获取上月
select to_char((select now() - interval '1 month'),'mm');
获取今年
select to_char((select now() as timestamp),'yyyy')
获取去年
select to_char((select now() - interval '1 years'),'yyyy')
(补充)获取过去12个月或者今年月份的数据
with recursive t (n) as ( select date(to_char( to_date('2022-08', 'yyyy-mm-dd') - interval '11 month', 'yyyy-mm-dd' )) union all select n + 1 from t where n < date( to_char( to_date('2022-08', 'yyyy-mm-dd'), 'yyyy-mm-dd' ) ) ) select to_char( n, 'yyyy-mm' ) as month from t group by month order by month # 只要月份修改成2022-12就表示查询今年内的所有月份
(补充)获取过去一个月内的所有天的数据
with recursive t ( n ) as ( select date ( to_char( now( ) - interval '30 day', 'yyyy-mm-dd' ) ) union all select n + 1 from t where n < date ( to_char( now( ), 'yyyy-mm-dd' ) ) ) select to_char( n, 'yyyy-mm-dd' ) as days from t group by days order by days
(补充)获取上个月每天的数据(1号-31号)
select generate_series ( date_trunc( 'month', current_date - interval '1 month'), date_trunc( 'month', current_date) - interval '1 day', '1 d' :: interval ) :: date days
(补充)获取本月每天的数据(1号-31号)
select generate_series ( date_trunc( 'month', current_date), date_trunc( 'month', current_date) + '1 month -1d', '1 d' :: interval ) :: date days
(补充)获取某一天24小时的时刻
select to_char(t,'yyyy-mm-dd hh24') as day from generate_series('2022-01-01 00:00:00'::date,'2022-01-01 23:00:00', '1 hours') as t order by day asc;
或者这样写(方便传参):获取某一天24小时的时刻
select to_char( t, 'yyyy-mm-dd hh24' ) as hour from generate_series ( to_date( '2022-01-01', 'yyyy-mm-dd hh24:mi:ss' ), to_timestamp( concat ( '2022-01-01', ' 23:00:00' ), 'yyyy-mm-dd hh24:mi:ss' ), '1 hours' ) as t order by hour asc;
同理,可获取某个月的所有天数据
select to_char( t, 'yyyy-mm-dd' ) as day from generate_series ( to_date('2022-11', 'yyyy-mm'), (date_trunc('month', to_date('2022-11', 'yyyy-mm')) + interval '1 month - 1 day')::date, '1 days' ) as t
同理,可获取某年的所有月份数据(上面写过相似的方法)
select to_char( t, 'yyyy-mm' ) as month from generate_series ( to_date( concat ( '2022', '-01-01' ), 'yyyy-mm-dd' ), to_date( concat ( '2022', '-12-31' ), 'yyyy-mm-dd' ), '1 month' ) as t order by month asc
获取时间之间的秒差
select round(date_part('epoch', timestamp '2022-08-15 17:00:10' - timestamp '2022-08-15 17:00:00')); 结果:10
获取时间之间的分钟差
select round(date_part('epoch', timestamp '2022-08-15 17:10:10' - timestamp '2022-08-15 17:00:00')/60); 结果:10
获取时间之间的小时差
select round(date_part('epoch', timestamp '2022-08-15 19:10:10' - timestamp '2022-08-15 17:00:00')/60/60); 结果:2
总结
到此这篇关于postgresql查询今天、昨天、本周、本月、上月、今年、去年的时间以及计算时间之差的文章就介绍到这了,更多相关pgsql查询时间内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论