Re: Sum 2 tables based on key from other table - Mailing list pgsql-general

From David Johnston
Subject Re: Sum 2 tables based on key from other table
Date
Msg-id 1384746751988-5778820.post@n5.nabble.com
Whole thread Raw
In response to Sum 2 tables based on key from other table  (Hengky Liwandouw <hengkyliwandouw@gmail.com>)
Responses Re: Sum 2 tables based on key from other table
List 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.


pgsql-general by date:

Previous
From: Ken Tanzer
Date:
Subject: Re: What does this error message mean?
Next
From: Tom Lane
Date:
Subject: Re: What does this error message mean?