我希望在几分钟内在Netezza中获得时差。
我有两列:Start(2014-06-01 07:45:04)
和finish(2014-06-01 07:46:58)
个事件
我尝试过使用DATEDIFF(SECOND,start,finish)
,但是Netezza发现错误。
我缺少一个功能吗?
Select R.*,S.*,CM.URL,DATEDIFF(SECOND,R.EVENT_DTM,S.MIN) From CTE_1 R
JOIN CTE_2 S
on S.VISIT_KEY = R.VISIT_KEY
JOIN ADMIN.VW_DIM_CME_METADATA CM on CM.CONTENT_METADATA_KEY = R.CONTENT_METADATA_KEY
答案 0 :(得分:4)
在IBM论坛上进行了一些搜索,发现这有效。如果有人有更好的方式,请发布它。感谢
Select
R.*,S.*,CM.URL,
EXTRACT(EPOCH from S.MIN - R.EVENT_DTM)DIFF
From CTE_1 R
JOIN CTE_2 S
on S.VISIT_KEY = R.VISIT_KEY
JOIN ADMIN.VW_DIM_CME_METADATA CM on CM.CONTENT_METADATA_KEY = R.CONTENT_METADATA_KEY
答案 1 :(得分:2)
Alankar的替代方案:
Select
R.*,S.*,CM.URL,
EXTRACT(SECOND from S.MIN - R.EVENT_DTM)DIFF
From CTE_1 R
JOIN CTE_2 S
on S.VISIT_KEY = R.VISIT_KEY
JOIN ADMIN.VW_DIM_CME_METADATA CM on CM.CONTENT_METADATA_KEY = R.CONTENT_METADATA_KEY
答案 2 :(得分:2)
https://www-304.ibm.com/connections/forums/html/topic?id=1f1f9bec-9ed4-42f4-9242-c099a8120523建议
提取(来自(timestamp2 - timestamp1)的纪元)
其中一条评论表明另一种选择:
如果您安装Netezza SQL Extension Toolkit,您只需使用即可 seconds_between函数,例如seconds_between(timestamp2, timestamp1)。
答案 3 :(得分:2)
我建议使用Netezza SQL Extension Toolkit:
select seconds_between('1996-02-27 06:12:33','1996-02-27 06:55:22');
答案 4 :(得分:1)
如果您希望两个时间戳之间的符号结果为秒,我建议使用如下表达式。
TESTDB.ADMIN(ADMIN)=> select * from time_diff;
COL1 | COL2
---------------------+---------------------
2014-06-24 21:28:33 | 2014-06-22 21:30:58
(1 row)
TESTDB.ADMIN(ADMIN)=> select extract(epoch from col1) - extract(epoch from col2) diff from time_diff;
DIFF
--------
172655
(1 row)
TESTDB.ADMIN(ADMIN)=> select extract(epoch from col2) - extract(epoch from col1) diff from time_diff;
DIFF
---------
-172655
(1 row)
如果您对签名结果不感兴趣,那么使用mc110提到的SQL Extension Toolkit提供的seconds_between函数效果很好,或者您可以简单地将ABS函数应用于第一个解决方案。
如果您在当前数据库中安装了SQL Extension Toolkit,则不需要我在此处使用的SQLEXT ..限定符。我碰巧将工具包安装在一个单独的数据库中,我称之为SQLEXT。
TESTDB.ADMIN(ADMIN)=> select abs(extract(epoch from col2) - extract(epoch from col1)) diff from time_diff;
DIFF
--------
172655
(1 row)
TESTDB.ADMIN(ADMIN)=> TESTDB.sqlext..seconds_between(col1,col2) diff from time_diff;
DIFF
--------
172655
(1 row)
我建议不要使用"秒"结果你可能不会成为你想要的。同样地使用单个提取物" epoch"减去两个值的值也可能无法满足您的期望。
TESTDB.ADMIN(ADMIN)=> select extract(seconds from col1 - col2) diff from time_diff;
DIFF
------
35
(1 row)
TESTDB.ADMIN(ADMIN)=> select extract(epoch from col1 - col2) diff from time_diff;
DIFF
--------
172655
(1 row)
TESTDB.ADMIN(ADMIN)=> select extract(epoch from col2 - col1) diff from time_diff;
DIFF
---------
-172654
(1 row)