Re: [SQL] joins with aggregates

From: George Moga <george(at)flex(dot)ro>
To: SQL PostgreSQL <pgsql-sql(at)postgreSQL(dot)org>
Subject: Re: [SQL] joins with aggregates
Date: 1998-12-11 15:55:59
Message-ID: 3671408F.7DC2011@flex.ro
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Remigiusz Soko³owski wrote:

> Hi!
> I'm wonder if it is possible to make a query which joins data from two
> tables and also aggregates some columns of one of them
> I mean something like
> SELECT p1.id_prod, p1.name_prod, d1.id_prod, sum(d1.quant_detal) FROM
> prod p1, detal d1 WHERE p1.id_prod=d1.id_prod
> GROUP BY p1.id_prod;
> TIA
> Rem

Try:

Create a new file (named ex: test.pl) with:

CREATE FUNCTION "calc_sum" ("int4") RETURNS "float8" AS '

declare
var float8;

begin
select sum(quant_detal) into var from detal where id_prod = $1;
return var;
end;

' LANGUAGE 'plpgsql';

or use the file who came with this mail.

Load the file in psql: \i test.pl and...:

SELECT p1.id_prod, p1.name_prod, d1.id_prod, calc_sum(p1.id_prod)
FROM prod p1, detal d1
WHERE p1.id_prod=d1.id_prod
GROUP BY p1.id_prod;

I create the function in PostgreSQL 6.4 on Red Hat Linux 5.1.
I don't know how this work on other versions.

I use id_prod as int4 and quant_detal as float8.

Best,
George Moga,
george(at)flex(dot)ro
george(at)cicnet(dot)ro
Braila, ROMANIA.

Attachment Content-Type Size
test.pl application/x-perl 197 bytes

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bruce Momjian 1998-12-13 03:41:09 Re: [SQL] char type seems the same as char(1)
Previous Message Remigiusz Sokoowski 1998-12-11 14:42:05 joins with aggregates