Re: Sum 2 tables based on key from other table

Поиск
Список
Период
Сортировка
От David Johnston
Тема Re: Sum 2 tables based on key from other table
Дата
Msg-id 1384746751988-5778820.post@n5.nabble.com
обсуждение исходный текст
Ответ на Sum 2 tables based on key from other table  (Hengky Liwandouw <hengkyliwandouw@gmail.com>)
Ответы Re: Sum 2 tables based on key from other table  (Hengky Liwandouw <hengkyliwandouw@gmail.com>)
Список 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 по дате отправления:

Предыдущее
От: Ken Tanzer
Дата:
Сообщение: Re: What does this error message mean?
Следующее
От: Tom Lane
Дата:
Сообщение: Re: What does this error message mean?