Thread: averaging interval values

averaging interval values

From
"praveen vejandla"
Date:
Hi All,

I have a table with one varchar field that contains time 
intervals.

Is there any way to average or sum those intervals in a table ?


Table name : test

field name : t2 (varchar(40))

it contains all intervals as below

t2

1:01:01
0:25:15
7:09:01
8:09:05

Is there any way to average or sum those above values using a 
function?

i could not type cast the varchar(40) to interval type.

How to change the datatype of field from varchar to interval?




Thanks and Regards,
Praveen



Re: averaging interval values

From
Christoph Haller
Date:
>
> 1:01:01
> 0:25:15
> 7:09:01
> 8:09:05
>
> Is there any way to average or sum those above values using a
> function?
>
> i could not type cast the varchar(40) to interval type.
>
> How to change the datatype of field from varchar to interval?
>

Try

SELECT CAST(CAST (t2 AS TEXT) AS INTERVAL) FROM test;
SELECT AVG(CAST(CAST (t2 AS TEXT) AS INTERVAL)) FROM test;
SELECT SUM(CAST(CAST (t2 AS TEXT) AS INTERVAL)) FROM test;

It works fine on my machine, I'm using PostgreSQL 7.2.1.

Regards, Christoph