Teradata:日期差异的平均值截断小数
Teradata:我有 Start_Timestamp
和 End_Timestamp
,我需要找到每个 ID< 的平均
Avg_Duration
/代码>。
数据集如下所示:
--------------------------------------------------------------------
|ID | Start_Timestamp |End_Timestamp |
--------------------------------------------------------------------
|111 |2021-08-25 19:37:51.327000 |2021-08-26 16:25:51.129600 |
--------------------------------------------------------------------
|111 |2021-07-16 06:17:23.124000 |2021-07-19 13:16:53.185350 |
--------------------------------------------------------------------
|111 |2021-06-22 10:11:21.754400 |2021-02-25 18:48:13.614650 |
--------------------------------------------------------------------
我运行了以下代码:
SELECT ID, AVG((End_Timestamp - Start_Timestamp) DAY ) AS Avg_Duration
FROM Table_A
Group By ID;
结果如下所示:
-----------------------
|ID | Aug_Duration|
-----------------------
|111 | 2|
-----------------------
我预计 Avg_Duration
为 (1+3+3)/3=2.33
。
我知道 整数除法
被截断。因此,在取平均值将整数转换为小数之前,我CAST
时间戳差异。我的代码是:
SELECT ID, AVG(CAST((End_Timestamp - Start_Timestamp) DAY AS DECIMAL(2,2))) AS Avg_Duration
FROM Table_A
Group By ID;
我期望 2.33
,但是,现在 TeraData 窗口无响应/空闲。它既不运行,也没有收到任何错误消息。
有人可以告诉我我在哪里犯了错误,以及如何获得带有 2 位小数而不是被截断的 Avg_Duration
吗?
Teradata: I have Start_Timestamp
and End_Timestamp
and I need to find the average Avg_Duration
for every ID
.
Dataset looks like this:
--------------------------------------------------------------------
|ID | Start_Timestamp |End_Timestamp |
--------------------------------------------------------------------
|111 |2021-08-25 19:37:51.327000 |2021-08-26 16:25:51.129600 |
--------------------------------------------------------------------
|111 |2021-07-16 06:17:23.124000 |2021-07-19 13:16:53.185350 |
--------------------------------------------------------------------
|111 |2021-06-22 10:11:21.754400 |2021-02-25 18:48:13.614650 |
--------------------------------------------------------------------
I ran the following code:
SELECT ID, AVG((End_Timestamp - Start_Timestamp) DAY ) AS Avg_Duration
FROM Table_A
Group By ID;
The result looks like this:
-----------------------
|ID | Aug_Duration|
-----------------------
|111 | 2|
-----------------------
I expected Avg_Duration
to be (1+3+3)/3=2.33
.
I am aware that integer division
gets truncated. So, I CAST
the Timestamp difference before I took average to convert Integer into Decimal. My code for that is:
SELECT ID, AVG(CAST((End_Timestamp - Start_Timestamp) DAY AS DECIMAL(2,2))) AS Avg_Duration
FROM Table_A
Group By ID;
I expected 2.33
, but, now TeraData window is unresponsive/idle. It neither runs nor I get any error message.
Can someone tell me where am I making a mistake and how can I get Avg_Duration
with 2 decimal places and not a truncated one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
)
位于错误的位置。但我怀疑它在 Teradata 上引起了问题,可能您只是没有注意到客户端(Studio?)根本没有提交它,因为它无效。这按预期工作 - 将
DEC(2,2)
(两个有效数字)更改为DEC(6,2)
(6 个有效数字)以避免溢出错误:即使使用最大天数(4),您仍然可能会收到间隔溢出错误,但您的逻辑也可以使用日期来计算:
最后,由于它基于时间戳,您可能需要更详细的间隔,例如
You had a
)
in the wrong place. But I doubt it caused an issue on Teradata, probably you just didn't notice that the client (Studio?) simply didn't submit it as it was invalid.This works as expected - after changing
DEC(2,2)
(two significant digits) toDEC(6,2)
(6 significant digits) to avoid an overflow error:Even with the maximum DAY(4) you might still get an Interval Overflow error, but your logic can also be calculated using dates:
Finally, as it's based on Timestamps you might want more detailed Intervals like