Teradata:日期差异的平均值截断小数

发布于 2025-01-11 00:50:12 字数 1665 浏览 4 评论 0原文

Teradata:我有 Start_TimestampEnd_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2025-01-18 00:50:12

您的 ) 位于错误的位置。但我怀疑它在 Teradata 上引起了问题,可能您只是没有注意到客户端(Studio?)根本没有提交它,因为它无效。

这按预期工作 - 将 DEC(2,2) (两个有效数字)更改为 DEC(6,2)(6 个有效数字)以避免溢出错误:

AVG(CAST((End_Timestamp - Start_Timestamp DAY(4)) AS DECIMAL(6,2)))

即使使用最大天数(4),您仍然可能会收到间隔溢出错误,但您的逻辑也可以使用日期来计算:

Avg(Cast(End_Timestamp AS DATE) - Cast(Start_Timestamp AS DATE))

最后,由于它基于时间戳,您可能需要更详细的间隔,例如

Avg(End_Timestamp - Start_Timestamp DAY(4) TO SECOND(0))
Avg(End_Timestamp - Start_Timestamp DAY(4) TO MINUTE)

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) to DEC(6,2)(6 significant digits) to avoid an overflow error:

AVG(CAST((End_Timestamp - Start_Timestamp DAY(4)) AS DECIMAL(6,2)))

Even with the maximum DAY(4) you might still get an Interval Overflow error, but your logic can also be calculated using dates:

Avg(Cast(End_Timestamp AS DATE) - Cast(Start_Timestamp AS DATE))

Finally, as it's based on Timestamps you might want more detailed Intervals like

Avg(End_Timestamp - Start_Timestamp DAY(4) TO SECOND(0))
Avg(End_Timestamp - Start_Timestamp DAY(4) TO MINUTE)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文