X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 4DC60D93E3 for ; Wed, 2 Nov 2005 15:40:05 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 18781-05 for ; Wed, 2 Nov 2005 19:40:00 +0000 (GMT) Received: from continuum.cm.nu (continuum.cm.nu [216.113.193.225]) by svr1.postgresql.org (Postfix) with ESMTP id 91422D9220 for ; Wed, 2 Nov 2005 15:40:00 -0400 (AST) Received: from shane by continuum.cm.nu with local (Exim 4.50) id 1EXOSn-0005zl-Lp for pgsql-sql@postgresql.org; Wed, 02 Nov 2005 11:40:01 -0800 Date: Wed, 2 Nov 2005 11:40:01 -0800 From: Shane To: pgsql-sql@postgresql.org Subject: Designing a stock portfolio database Message-ID: <20051102194001.GA21986@cm.nu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-No-Archive: yes User-Agent: Mutt/1.5.9i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[AWL=0.000] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200511/31 X-Sequence-Number: 23283 Hello, I am attempting to design a database capable of displaying a stock portfolio. I have run into some early problems though suggesting that I might have some design problems. I have two simple transaction tables as follows: create table stock_transactions( id serial not null, date timestamp(0) without time zone, symbol varchar(12) not null, currency char(3) not null, shares int not null, -- negative for a sell or a short multiplier int not null default 1, -- of contracts for -- options price numeric not null, commission numeric not null default 0, notes text, primary key(id) ); create table cash_transactions( id serial not null, date timestamp(0) without time zone not null, currency char(3) not null, amount numeric not null, -- may be negative div_symbol varchar(12) default null, -- for stock dividends notes text, primary key(id) ); Finally, I have a query to display the portfolio's current holdings. create view stock_holdings as SELECT t.symbol, t.currency, sum(t.shares) AS shares, t.multiplier, sum(t.price * t.shares::numeric * t.multiplier::numeric) AS price_paid, sum(t.commission) AS commission, COALESCE(( SELECT sum(cash_transactions.amount) AS sum FROM cash_transactions WHERE cash_transactions.div_symbol::text = t.symbol::text), 0::numeric) AS dividends FROM stock_transactions t GROUP BY t.symbol, t.currency, t.multiplier; The above query however has a problem. If the user trades out of a stock and then back in, the price_paid isn't right as it's calculating the previous purchase as well. Should a portfolio DB instead have a real stock_holdings table and have functions update it as new transactions are inserted or is there a better way to write the view so as include closed positions in the output? Also, I haven't found many oss projects which do stock portfolios in SQL but did look at beancounter which keeps a similar transaction log in the db but does all the view stuff in the code. Are there any other portfolio pgsql projects out there I could look at? Best regards, Shane -- http://www.cm.nu/~shane/