user aggregate function ( calculate the average value of each index of an array column )

Поиск
Список
Период
Сортировка
От Myoung-Ah KANG
Тема user aggregate function ( calculate the average value of each index of an array column )
Дата
Msg-id 004701cdc35c$822ae830$8680b890$@fr
обсуждение исходный текст
Ответы Re: user aggregate function ( calculate the average value of each index of an array column )  (Vincent Veyron <vv.lists@wanadoo.fr>)
Список pgsql-general

Hi,

 

I have a table with several lines as following;

 

-          Create table mytable (type number ,  values  integer [2]) ;

 

-          Insert into mytable values (1,  ‘{ 10, 0 }’ );

-          Insert into mytable values (1,  ‘{ 20, 30 }’ );

-          Insert into mytable values (2,  ‘{30,  60}’ );

 

(In fact, the array size is very big (ex. values [10000]) but the size is fix.   In order to simplify the example, I used an array integer [2]).

 

 

I would like to obtain the average value of each index of values column.

 

Is it possible to create an aggregate function which can works as following ? :

(Suppose that avg_mytable is the aggregation function name.)

 

Ex1)  Select  avg_mytable (values)  from mytable ;

 

avg_mytable (values) 

------------------------          

{ 20,  30}

 

 

(- Explication of the results: 20 because (10+20+30)/3 , 30 because (0+30+60)/3)

 

 

Ex2)  Select type, avg_mytable (values)  from mytable  Group by type ;

 

Type  |  avg_mytable (values) 

---------------------------------------------

1        |  { 15, 15}

2        |  { 30, 60}

 

 

I searched in the documentation for “array functions” but I could not find functions useful for me...

 

Thank you so much,

 

           Lea

В списке pgsql-general по дате отправления:

Предыдущее
От: Dmitriy Tyugaev
Дата:
Сообщение: Re: FATAL: index contains unexpected zero page at block
Следующее
От: Harry
Дата:
Сообщение: PG_TERMINATE_BACKEND not working.