Problem adding columns - Mailing list pgsql-novice

From Oscar Alberto Chavarria Marin
Subject Problem adding columns
Date
Msg-id 716841580702111916o17c05a9end1c74ec8d3df6447@mail.gmail.com
Whole thread Raw
Responses Re: Problem adding columns  (Michael Fuhr <mike@fuhr.org>)
List 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

pgsql-novice by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: partial indexed not being used.
Next
From: Michael Fuhr
Date:
Subject: Re: Problem adding columns