Search Postgresql Archives

Re: Sum 2 tables based on key from other table

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.


-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux