Re: max and sum function

Re: max and sum function

am 23.03.2003 04:47:36 von ryanne cruz

hi list.=20
=20
does anyone here knows of a function that is similar to the sum function bu=
t=20
involves more than one column? for example, i have a table with columns id,=
=20
col1, col2 and col3. i want to get the max value from the three columns gru=
ped=20
by an id.=20
=20
what i came up with is something like this:=20
=20
max1=3Dselect max(one) from (select sum(col1) as one from table group by id=
);=20
max2=3Dselect max(two) from (select sum(col2) as two from table group by id=
);=20
max3=3Dselect max(three) from (select sum(col3) as three from table group b=
y=20
id);=20
=20
then i compare the 3 max values and get the maximum of the 3.
=20
is there an easier way to do this?=20
=20
thanks.=20
=20
ryanne
=20


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Re: max and sum function

am 24.03.2003 07:09:06 von Waruna Geekiyanage

SELECT MAX(aa.res) from ((select sum(col1) as res from table group by id)
UNION
(select sum(col2) as res from table group by id)
UNION
(select sum(col3) as res from table group by id)) AS aa;
----- Original Message -----
From: "ryanne cruz"
To:
Sent: Sunday, 23 March, 2003 9:47 AM
Subject: Re: [PHP] max and sum function


hi list.

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);


then i compare the 3 max values and get the maximum of the 3.

is there an easier way to do this?

thanks.

ryanne



---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Re: max and sum function

am 26.03.2003 15:26:19 von Greg Stark

"Waruna Geekiyanage" 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


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org