conditional aggregates

From: Marcin Krawczyk <jankes(dot)mk(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: conditional aggregates
Date: 2010-12-08 12:49:50
Message-ID: AANLkTinG4=xMPmwoYHpPtt9RqtMABt=pvjYCGRniKzBx@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi list,

Can anyone advise me on creating an aggregate that would take additional
parameter as a condition ? For example, say I have a table like this

id;value
1;45
2;13
3;0
4;90

I'd like to do something like this

SELECT min_some_cond_aggregate(value,0) FROM table

to get the minimal value from table greater than 0, in this case 13.

I can't do SELECT min(value) FROM table WHERE value > 0 as this will mess my
other computations. My current solution involves a function operating on the
output of array_accum from the docs, but I'm looking for more elegant
solution.

Is this possible at all ? I'm running 8.1.

regards
mk

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Pavel Stehule 2010-12-08 13:07:20 Re: conditional aggregates
Previous Message - 2010-12-08 07:27:59 mybatis 3 RETURNING id problem with insert.