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

From Hengky Liwandouw
Subject Help : Sum 2 tables based on key from other table
Date
Msg-id 10C2D01A-9009-458D-9116-2942A2949A9F@gmail.com
Whole thread Raw
Responses Re: Help : Sum 2 tables based on key from other table
Re: Help : Sum 2 tables based on key from other table
List pgsql-general
Dear Friends,

Please help for the select command, as i had tried many times and always can not display the result as what i want.

I am looking for the solution on google but still can not found the right answer to solve the problem.

I have 3 tables :

Table A
    ProductID
    ProductName
    SupplierID

Table B
    ProductID
    InitialStock

Table C
    ProductID
    Date
    In
    Out

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'

So the result will look like this :

ProductID  ProductName  SumofIntialStock  sum(in-Out)<beginningdate       SumofIN  SumofOut
xxxx           xxxxxxxxxxxxx              99                               99                                 99
    99 
xxxx           xxxxxxxxxxxxx              99                               99                                 99
    99 
xxxx           xxxxxxxxxxxxx              99                               99                                 99
    99 
xxxx           xxxxxxxxxxxxx              99                               99                                 99
    99 


What command to get result like this ? i have tried crosstab function but not success too :(

Thanks in advance




pgsql-general by date:

Previous
From: David Johnston
Date:
Subject: Re: Sum 2 tables based on key from other table
Next
From: David Johnston
Date:
Subject: Primary Key Index Bloat?