Re: Using values in an array in a subquery

Поиск
Список
Период
Сортировка
Искать
От
Michael Fuhr
Тема
Re: Using values in an array in a subquery
Дата
Msg-id
20051110181513.GA71649@winnie.fuhr.org
Ответ на
Список
Дерево обсуждения
Using values in an array in a subquery David Orme <d.orme@imperial.ac.uk>
Re: Using values in an array in a subquery Michael Fuhr <mike@fuhr.org>
Re: Using values in an array in a subquery David Orme <d.orme@imperial.ac.uk>
On Wed, Nov 09, 2005 at 05:39:02PM +0000, David Orme wrote:
> I want to be able to aggregate a value for each row based on the  
> values of var for the rows with node values appearing in the members  
> array.  If the aggregate was sum, then I'm looking to get something  
> like this:
> 
> node | sum
> -----+-----
>    1 |
>    2 |
>    3 |
>    4 |
>   -3 |   3
>   -2 |   8
>   -1 |  10

Is this what you're looking for?  It works for me in 7.4 and later,
at least with your test data.

SELECT a.node, sum(b.var)
FROM array_test AS a
LEFT OUTER JOIN array_test AS b ON b.node = ANY(a.members)
GROUP BY a.node;

 node | sum 
------+-----
   -1 |  10
   -2 |   8
   -3 |   3
    4 |    
    3 |    
    2 |    
    1 |    
(7 rows)

-- 
Michael Fuhr
В списке pgsql-novice по дате отправления
От: Helge Elvik
Дата:
Сообщение: Building libpq on Windows
От: David Orme
Дата:
FAQ