我有一个timestamp
列,用于跟踪外部传感器何时记录读数。此列转到microsecond
,但我想将数据截断为decisecond
以进行分组。
此truncate function效果很好,但只允许解析millisecond
或microsecond
。我只需要将我的数据修剪为decisecond
以解决我的收集过程中的错误。
有没有办法将timestamp
列修剪为decisecond
?
答案 0 :(得分:0)
将Now()
(当前时间)替换为您选择的列:
select
-- current time
now(),
-- current time truncated to minutes
date_trunc('minute', Now()),
-- seconds from timestamp, including decimal part
extract(seconds from Now()),
-- seconds truncated to first decimal digit (results in seconds and decisecond)
trunc((extract(seconds from Now()))::numeric, 1),
-- add time truncated to minutes and seconds truncated to first decimal digit
date_trunc('minute', Now())
+ interval '1 second' * trunc((extract(seconds from Now()))::numeric, 1)
请注意,由于numeric
返回extract()
而double precision
仅接受trunc(x, y)
(请参阅documentation)
对于名为mytime
的列,这将是:
date_trunc('minute', mytime)
+ interval '1 second' * trunc((extract(seconds from mytime))::numeric, 1)
另一种选择是截断epoch
:
select
-- current time
now(),
-- epoch - number of seconds since epoch beginning, including decimal part
extract(epoch from Now()),
-- truncated to first decimal digit
trunc((extract(epoch from Now()))::numeric, 1),
-- converted back to timestamp
to_timestamp(trunc((extract(epoch from Now()))::numeric, 1))
对于名为mytime
的列,这将是:
to_timestamp(trunc((extract(epoch from mytime))::numeric, 1))