[SQL] conditional aggregates

2010-12-08 Thread Marcin Krawczyk
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


Re: [SQL] conditional aggregates

2010-12-08 Thread Pavel Stehule
Hello

use a CASE statement

http://www.postgresql.org/docs/7.4/static/functions-conditional.html

Regards

Pavel Stehule

2010/12/8 Marcin Krawczyk jankes...@gmail.com:
 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


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql


Re: [SQL] conditional aggregates

2010-12-08 Thread Marcin Krawczyk
Yeah I know but I'm having problems creating sfunc fuction for the
aggregate.


regards
mk


2010/12/8 Pavel Stehule pavel.steh...@gmail.com

 Hello

 use a CASE statement

 http://www.postgresql.org/docs/7.4/static/functions-conditional.html

 Regards

 Pavel Stehule

 2010/12/8 Marcin Krawczyk jankes...@gmail.com:
  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
 



Re: [SQL] conditional aggregates

2010-12-08 Thread Marc Mamin
something like ?

 

Select min (case when X  0 then X end) 

 

 

HTH,

 

Marc Mamin

 

From: pgsql-sql-ow...@postgresql.org
[mailto:pgsql-sql-ow...@postgresql.org] On Behalf Of Marcin Krawczyk
Sent: Mittwoch, 8. Dezember 2010 14:20
To: Pavel Stehule
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] conditional aggregates

 

Yeah I know but I'm having problems creating sfunc fuction for the
aggregate.

 


regards
mk



2010/12/8 Pavel Stehule pavel.steh...@gmail.com

Hello

use a CASE statement

http://www.postgresql.org/docs/7.4/static/functions-conditional.html

Regards

Pavel Stehule

2010/12/8 Marcin Krawczyk jankes...@gmail.com:

 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


 



Re: [SQL] conditional aggregates

2010-12-08 Thread Marcin Krawczyk
Thanks, it working.


pozdrowienia
mk


2010/12/8 Marc Mamin m.ma...@intershop.de

  something like ?



 Select min (case when X  0 then X end)





 HTH,



 Marc Mamin



 *From:* pgsql-sql-ow...@postgresql.org [mailto:
 pgsql-sql-ow...@postgresql.org] *On Behalf Of *Marcin Krawczyk
 *Sent:* Mittwoch, 8. Dezember 2010 14:20
 *To:* Pavel Stehule
 *Cc:* pgsql-sql@postgresql.org
 *Subject:* Re: [SQL] conditional aggregates



 Yeah I know but I'm having problems creating sfunc fuction for the
 aggregate.




 regards
 mk

  2010/12/8 Pavel Stehule pavel.steh...@gmail.com

 Hello

 use a CASE statement

 http://www.postgresql.org/docs/7.4/static/functions-conditional.html

 Regards

 Pavel Stehule

 2010/12/8 Marcin Krawczyk jankes...@gmail.com:

  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