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?