赞
踩
在项目遇到一个需求是查询统计今天、昨天、本周、本月、上月、今年、去年的时间数据,最近一个月的,最近一年的月份数据,使用的是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')
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
SELECT
generate_series (
date_trunc( 'month', CURRENT_DATE - interval '1 month'),
date_trunc( 'month', CURRENT_DATE) - interval '1 day',
'1 d' :: INTERVAL
) :: DATE days
SELECT
generate_series (
date_trunc( 'month', CURRENT_DATE),
date_trunc( 'month', CURRENT_DATE) + '1 month -1d',
'1 d' :: INTERVAL
) :: DATE days
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;
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。