简体   繁体   English

ANSI DATETIME上的无效操作(在Teradata中将一个时间戳减去另一个时间戳)

[英]Invalid Operation On An ANSI DATETIME (Subtracting one timestamp from another in Teradata)

I would like to create a WHERE condition to return results where only 1 day has passed between two timestamps. 我想创建一个WHERE条件以返回两个时间戳之间仅经过1天的结果。 I tried this: 我尝试了这个:

SELECT * FROM RDMAVWSANDBOX.VwNIMEventFct
INNER JOIN VwNIMUserDim ON VwNIMUserDim.NIM_USER_ID = VwNIMEventFct.NIM_USER_ID
INNER JOIN rdmatblsandbox.TmpNIMSalesForceDB ON TmpNIMSalesForceDB.EMAIL = VwNIMUserDim.USER_EMAIL_ADDRESS
WHERE (CONTRACT_EFFECTIVE_DATE - EVENT_TIMESTAMP) =1

But the result was an error message "Invalid Operation On An ANSI DATETIME value". 但是结果是一条错误消息“ ANSI DATETIME值上的无效操作”。

I guess that, looking at the code now, Teradata has no way of knowing whether the "1" in "= 1" is a day, hour or year. 我猜想,现在看一下代码,Teradata无法知道“ = 1”中的“ 1”是日,小时还是年。

How would I select data where only 1 day has passed between CONTRACT_EFFECTIVE_DATE and EVENT_TIMESTAMP? 在CONTRACT_EFFECTIVE_DATE和EVENT_TIMESTAMP之间只有1天的时间,我该如何选择数据?

Same again for 2 days, and 3 days etc? 同样的2天和3天等吗?

If both columns are DATEs you can use =1 which means one day. 如果两列均为DATE,则可以使用=1 ,表示一天。

For Timestamps you need to tell what kind of interval you want: 对于时间戳,您需要告诉您想要哪种间隔:

WHERE (CONTRACT_EFFECTIVE_DATE - EVENT_TIMESTAMP) DAY = INTERVAL '1' DAY

But i'm not shure if this is what you really want, what's your definition of 1 day ? 但是我不确定这是否是您真正想要的,您对1天的定义是什么?

Edit: 编辑:

Based on your comment the best way should be: 根据您的评论,最好的方法应该是:

WHERE CAST(CONTRACT_EFFECTIVE_DATE AS DATE) - CAST(EVENT_TIMESTAMP AS DATE) = 1

This avoids dealing with INTERVAL arithmetic :-) 这样可以避免处理INTERVAL算法:-)

Not sure about Teradata, but I think most versions of SQL have built-in date math functions. 不确定Teradata,但我认为大多数SQL版本都具有内置的日期数学函数。 In MSSQL for instance you could do this: 例如,在MSSQL中,您可以执行以下操作:

...
WHERE DATEDIFF(DAY, CONTRACT_EFFECTIVE_DATE, EVENT_TIMESTAMP) = 1

Or if you wanted to make sure 24 hours had passed you could do: 或者,如果您想确保已过24小时,则可以执行以下操作:

...
WHERE DATEDIFF(HOUR, CONTRACT_EFFECTIVE_DATE, EVENT_TIMESTAMP) = 1

Other SQL's have their own versions of this, and you may have to use 'D' or 'DD' instead of 'DAY' or something (and maybe 'HH' instead of 'HOUR' likewise). 其他SQL都有自己的版本,您可能必须使用'D'或'DD'而不是'DAY'或其他名称(也许也可以使用'HH'而不是'HOUR')。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM