Sum data from two tables with different number of rows

Perd

There are 3 Tables (SorMaster, SorDetail, and InvWarehouse):

SorMaster:

+------------+
| SalesOrder |
+------------+
|    100     |
|    101     |
|    102     |
+------------+

SorDetail:

+------------+------------+---------------+
| SalesOrder | MStockCode | MBackOrderQty |
+------------+------------+---------------+
|    100     |    PN-1    |       4       |
|    100     |    PN-2    |       9       |
|    100     |    PN-3    |       1       |
|    100     |    PN-4    |       6       |
|    101     |    PN-1    |       6       |
|    101     |    PN-3    |       2       |
|    102     |    PN-2    |      19       |
|    102     |    PN-3    |      14       |
|    102     |    PN-4    |       6       |
|    102     |    PN-5    |       4       |
+------------+------------+---------------+

InvWarehouse:

+------------+-----------+-----------+
| MStockCode | Warehouse | QtyOnHand |
+------------+-----------+-----------+
|    PN-1    |     A     |     1     |
|    PN-2    |     B     |     9     |
|    PN-3    |     A     |     0     |
|    PN-4    |     B     |     1     |
|    PN-1    |     A     |     0     |
|    PN-3    |     B     |     5     |
|    PN-2    |     A     |     9     |
|    PN-3    |     B     |     4     |
|    PN-4    |     A     |     6     |
|    PN-5    |     B     |     0     |
+------------+-----------+-----------+

Desired Results:

+------------+-----------------+--------------+
| MStockCode | SumBackOrderQty | SumQtyOnHand |
+------------+-----------------+--------------+
|    PN-1    |       10        |      10      |
|    PN-2    |       28        |       1      |
|    PN-3    |       17        |       5      |
|    PN-4    |       12        |      13      |
|    PN-5    |       11        |       6      |
+------------+-----------------+--------------+

I have been going around in circles with no end in sight. Seems like it should be simple but just can't wrap my head around it. The SumBackOrderQty obviously getting counted twice as the SumQtyOnHand is evaluated. To this point I have been doing the calculations in the PHP instead of the select statement but would like to clean things up a bit where possible.

Current query statement is:

SELECT  SorDetail.MStockCode,
    SUM(SorDetail.MBackOrderQty) AS 'SumMBackOrderQty',
    SUM(InvWarehouse.QtyOnHand) AS 'SumQtyOnHand'

FROM    SysproCompanyJ.dbo.SorMaster SorMaster,
    SysproCompanyJ.dbo.SorDetail SorDetail LEFT OUTER JOIN SysproCompanyJ.dbo.InvWarehouse InvWarehouse
    ON SorDetail.MStockCode = InvWarehouse.StockCode


WHERE   SorMaster.SalesOrder = SorDetail.SalesOrder
    AND SorMaster.ActiveFlag != 'N'
    AND SorDetail.MBackOrderQty > '0'
    AND SorDetail.MPrice > '0'

GROUP BY SorDetail.MStockCode

ORDER BY    SorDetail.MStockCode ASC
fspinnenhirn

Without providing the complete picture, in terms of your RDBMS, database schema, a description of the problem you're trying to solve and sample data that matches the aforementioned, the following is just an illustration of what a solution based on Barmar's comment could look like:

SELECT SD.MStockCode, 
       SD.SumBackOrderQty, 
       IW.SumQtyOnHand
FROM (SELECT MStockCode, 
             SUM(MBackOrderQty) AS `SumBackOrderQty`
      FROM SorDetail
           JOIN SorMaster ON SorDetail.SalesOrder=SorMaster.SalesOrder
      WHERE SorMaster.ActiveFlag != 'N'
            AND SorDetail.MBackOrderQty > 0
            AND SorDetail.MPrice > 0
      GROUP BY MStockCode) AS SD
     LEFT JOIN (SELECT MStockCode, 
                       SUM(QtyOnHand) AS `SumQtyOnHand`
                FROM InvWarehouse
                GROUP BY MStockCode) AS IW ON SD.MStockCode=IW.MStockCode
ORDER BY SD.MStockCode;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sum data from two tables with different number of rows

From Dev

How can I sum two rows from two different tables?

From Dev

sum of grouped rows of two different tables

From Dev

sum of grouped rows of two different tables

From Dev

Matching data from two tables with unequal number of rows

From Dev

Get the total number of rows from two different tables with the same id drops me more rows

From Dev

MYSQL sum from two different tables group by

From Dev

Query to sum from two different tables

From Dev

Combine rows from two tables with different columns?

From Dev

Search data from two different tables that two tables in different databases

From Dev

SQL select rows from two different tables with different column names

From Dev

Combine data from two different junction tables

From Dev

How to fetch data from two different tables

From Dev

Combine data from two different junction tables

From Dev

SQL: How to to SUM two values from different tables

From Dev

How can I sum two fileds from different tables in PIG?

From Dev

Select rows at same index from two different tables

From Dev

Count all rows from all tables in two databases on different servers

From Dev

Combine two data frames with different number of rows in R

From Dev

Find total number of rows using data from 3 tables, MySQL

From Dev

Calculate the sum from different tables

From Dev

Sum and Grouping from Different Tables

From Dev

Sum and Grouping from Different Tables

From Dev

Identify, and compare, two rows in two different tables

From Dev

Request from two tables with SUM

From Dev

sql sum from two tables

From Dev

Request from two tables with SUM

From Dev

Retrieve data from two different tables and output into a column value

From Dev

Insert data in two different tables from single Controller in Yii

Related Related

  1. 1

    Sum data from two tables with different number of rows

  2. 2

    How can I sum two rows from two different tables?

  3. 3

    sum of grouped rows of two different tables

  4. 4

    sum of grouped rows of two different tables

  5. 5

    Matching data from two tables with unequal number of rows

  6. 6

    Get the total number of rows from two different tables with the same id drops me more rows

  7. 7

    MYSQL sum from two different tables group by

  8. 8

    Query to sum from two different tables

  9. 9

    Combine rows from two tables with different columns?

  10. 10

    Search data from two different tables that two tables in different databases

  11. 11

    SQL select rows from two different tables with different column names

  12. 12

    Combine data from two different junction tables

  13. 13

    How to fetch data from two different tables

  14. 14

    Combine data from two different junction tables

  15. 15

    SQL: How to to SUM two values from different tables

  16. 16

    How can I sum two fileds from different tables in PIG?

  17. 17

    Select rows at same index from two different tables

  18. 18

    Count all rows from all tables in two databases on different servers

  19. 19

    Combine two data frames with different number of rows in R

  20. 20

    Find total number of rows using data from 3 tables, MySQL

  21. 21

    Calculate the sum from different tables

  22. 22

    Sum and Grouping from Different Tables

  23. 23

    Sum and Grouping from Different Tables

  24. 24

    Identify, and compare, two rows in two different tables

  25. 25

    Request from two tables with SUM

  26. 26

    sql sum from two tables

  27. 27

    Request from two tables with SUM

  28. 28

    Retrieve data from two different tables and output into a column value

  29. 29

    Insert data in two different tables from single Controller in Yii

HotTag

Archive