pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Greg Stark <gsstark@mit.edu>
To: Waruna Geekiyanage <waruna@nirmani.com>
Cc: pgsql-php@postgresql.org
Subject: Re: max and sum function
Date: 26 Mar 2003 09:26:19 -0500
Message-ID: <87y932kxv8.fsf@stark.dyndns.tv> (raw)
In-Reply-To: <002201c2f1cc$0c58be00$0c00000a@ibm1>
References: <1048391256.3e7d2e58a80b9@mail.up.edu.ph>
	<002201c2f1cc$0c58be00$0c00000a@ibm1>

"Waruna Geekiyanage" <waruna@nirmani.com> writes:

> does anyone here knows of a function that is similar to the sum function but
> involves more than one column? for example, i have a table with columns id,
> col1, col2 and col3. i want to get the max value from the three columns
> gruped
> by an id.
> 
> what i came up with is something like this:
> 
> max1=select max(one) from (select sum(col1) as one from table group by id);
> max2=select max(two) from (select sum(col2) as two from table group by id);
> max3=select max(three) from (select sum(col3) as three from table group by
> id);

select int4larger(int4larger(max(one),max(two)),max(three)) from table group by id;

I'm a bit stumped why it seems I have to use "int4larger" and just "larger"
doesn't work like with most functions. There must be a right way to do it
without hard coding the datatype.

-- 
greg




view thread (3+ messages)

Message-ID: <87y932kxv8.fsf@stark.dyndns.tv>
Permalink:  ../87y932kxv8.fsf@stark.dyndns.tv/
Also on:    postgresql.org/message-id/87y932kxv8.fsf@stark.dyndns.tv

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: gsstark@mit.edu, waruna@nirmani.com
  Subject: Re: max and sum function
  In-Reply-To: <87y932kxv8.fsf@stark.dyndns.tv>
  References: <1048391256.3e7d2e58a80b9@mail.up.edu.ph> <002201c2f1cc$0c58be00$0c00000a@ibm1> <87y932kxv8.fsf@stark.dyndns.tv>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox