Re: Sum 2 tables based on key from other table

From: David Johnston <polobo(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Sum 2 tables based on key from other table
Date: 2013-11-18 03:52:31
Message-ID: 1384746751988-5778820.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hengky Lie wrote
> 1. I want to select all productID from Table A where supplierID='XXX'.
>
> 2. Based on list from Step.1 : sum the initialstock from Table B
>
> 3. Based on list from Step 1 : Sum (in-out) from Table C where date
> <'BEGINNING DATE'
>
> 4. Based on list from Step 1 : Sum (in) and sum(out) from Table C where
> date between 'BEGINNING DATE' and 'ENDING DATE'

You have three questions so you'll likely need three queries. You can
combine them for final output if desired. You can use multiple direct
sub-queries or use CTEs/WITH.

Note that table B seems pointless. All stock should initially be zero and
the first IN record in Table C establishes the initial balance. That said
I've used a similar schema before so having a starting balance column may
have merit. But why not put in on Table A instead?

David J.

--
View this message in context: http://postgresql.1045698.n5.nabble.com/Sum-2-tables-based-on-key-from-other-table-tp5778813p5778820.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2013-11-18 03:59:36 Re: What does this error message mean?
Previous Message Ken Tanzer 2013-11-18 03:50:17 Re: What does this error message mean?