Sql 将午夜时间戳重命名为前一天的24小时
下面的查询将时间戳分解为日期、Sql 将午夜时间戳重命名为前一天的24小时,sql,postgresql,case,Sql,Postgresql,Case,下面的查询将时间戳分解为日期、forecast\u day和小时、forecast\u hour 我的问题是处理2016-02-01 00:00:00的时间戳,以返回前一天2016-01-31的forecast\u day,以及24小时的forecast\u hour。对于此类时间戳,下面的代码仅返回2016-02-01的forecast\u day和forecast\u hour0 select a.lat, a.lon, date_trunc('day', a.foretime -
forecast\u day
和小时、forecast\u hour
我的问题是处理2016-02-01 00:00:00的时间戳,以返回前一天2016-01-31的forecast\u day
,以及24小时的forecast\u hour
。对于此类时间戳,下面的代码仅返回2016-02-01的forecast\u day
和forecast\u hour
0
select
a.lat, a.lon,
date_trunc('day', a.foretime - interval '5 hours')::date as forecast_day,
extract(hour from a.foretime - interval '5 hours') as forecast_hour,
f.windspeed as forecast,
a.as_of - interval '5 hours' as latest_as_of
from weather.forecast f
join (select
foretime,
max(as_of) as as_of,
lat, lon
from weather.forecast
where date_trunc('day', foretime - interval '5 hours')::date - as_of >= interval '9 hours'
group by foretime, lat, lon) a using (foretime, as_of, lat, lon)
您将使用案例
。或技巧:
select . . .
(case when extract(hour from a.foretime - interval '5 hours') = 0
then date_trunc('day', a.foretime - interval '5 hours')::date - interval '1' day
else date_trunc('day', a.foretime - interval '5 hours')::date
end) as forecast_day,
(case when extract(hour from a.foretime - interval '5 hours') = 0
then 24
else extract(hour from a.foretime - interval '5 hours')
end) as forecast_hour,
您将使用案例
。或技巧:
select . . .
(case when extract(hour from a.foretime - interval '5 hours') = 0
then date_trunc('day', a.foretime - interval '5 hours')::date - interval '1' day
else date_trunc('day', a.foretime - interval '5 hours')::date
end) as forecast_day,
(case when extract(hour from a.foretime - interval '5 hours') = 0
then 24
else extract(hour from a.foretime - interval '5 hours')
end) as forecast_hour,
23:59:59是一天中的最后一秒。24:00:00不存在。0:00:00是第二天。时间就是这样被普遍知道的。如果希望0:00:00表示前一天的最后一秒,则需要使用If/case语句将0:00:00时间管理为第1天。但为什么?!?0:00:00是每天的0-1秒!看起来你想做的是在0:00:01开始一天,但到那时整整一秒钟已经过去了@好东西。谢谢你的信息。23:59:59是一天中的最后一秒。24:00:00不存在。0:00:00是第二天。时间就是这样被普遍知道的。如果希望0:00:00表示前一天的最后一秒,则需要使用If/case语句将0:00:00时间管理为第1天。但为什么?!?0:00:00是每天的0-1秒!看起来你想做的是在0:00:01开始一天,但到那时整整一秒钟已经过去了@好东西。谢谢你的信息。