Problem adding columns

From: "Oscar Alberto Chavarria Marin" <cyberbuzzard(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Problem adding columns
Date: 2007-02-12 03:16:36
Message-ID: 716841580702111916o17c05a9end1c74ec8d3df6447@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

I have two tables, from one of which I wish to obtain the sum of several
columns (return1,....return7) and insert the result into the other.

Here are the tables layout:

CREATE TABLE investments
(
investment_id serial NOT NULL,
client_id integer,
client_number integer,
investment_date date,
investment_amount integer,
return1 integer,
return2 integer,
return3 integer,
return4 integer,
return5 integer,
return6 integer,
return7 integer
)

CREATE TABLE totalreturns
(
client_name character varying(128),
initial_invest numeric(8),
total_reimburse numeric(8),
difference numeric(7)
)

And the function to perform the aforementioned calculation:

CREATE FUNCTION
add_reimburse(integer,integer,integer,integer,integer,integer,integer)
RETURNS integer AS $$
DECLARE
sumyields investments%rowtype;
yields integer;
client_count integer;
counter integer;
BEGIN
SELECT count(*) INTO client_count FROM investments;
FOR counter IN 1 TO client_count
LOOP
yields:=
sumyields.return1+sumyields.return2+sumyields.return3+sumyields.return4+sumyields.return5+sumyields.return6+sumyields.return7
;
INSERT INTO totalreturns(total_reimburse) VALUES(yields);
yields:=0;
END LOOP;
return counter;
END;
$$ language plpgsql;

The code is not resulting in any calculations inserted on the "recipient"
table totalreturns.

Thank you in advance for any suggestions.

--
Regards

Oscar Alberto Chavarria
Mobile: +506 814-0247

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Fuhr 2007-02-12 04:25:06 Re: Problem adding columns
Previous Message Michael Fuhr 2007-02-12 02:42:45 Re: partial indexed not being used.