将整数转换为数字,然后记录日志

时间:2014-09-12 19:45:13

标签: sql postgresql casting amazon-redshift

我有一张带有2个时间戳的红移表,存储为整数。我想约会两个时间戳的差异,从秒转换为几天,然后记录结果的日志。

e.g:

select cast(t1 - t2 as numeric)/86400 as diff from mytable limit 5

哪个收益率:

      diff
1 0.000011
2 1.379166
3 1.365104
4 1.357002
5 17.356759

但是,当我添加日志功能时,语句失败:

select log(cast(t1 - t2 as numeric)/86400) as diff from mytable limit 5

的产率:

Function "log(numeric,numeric)" not supported.

这是令人惊讶的,因为日志函数在同一个表中的数字和整数列上运行得很好。在拍摄日志之前,我需要在演员表演后做一些魔术吗?或者我应该投射不同的数据类型?

奇怪的是,这句话很有用:

select log(cast(1410551376-1310551376 as numeric)/86400) as diff

得到以下特性:

      diff
1 3.063486

/ edit:如果删除显式强制转换,我也会得到同样的错误:

select log((t1 - t2)/86400.0) as diff from mytable limit 5

2 个答案:

答案 0 :(得分:2)

Postgres长期支持numeric作为log()的论据(RedShift基于Postgres)。也许曾几何时,它只允许加倍,所以这应该有效:

select log(cast(t1 - t2 as double precision)/86400.0) as diff
from mytable
limit 5;

答案 1 :(得分:0)

你默认为整数运算,改为强制浮点

select log(cast(t1 - t2 as numeric)/86400.0) as diff from mytable limit 5

另外,请确保t1大于t2