从具有不同行数的两个表中汇总数据

时间:2015-04-14 01:45:20

标签: sql

有3个表(SorMaster,SorDetail和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     |
+------------+-----------+-----------+

期望的结果:

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

我一直在四处走动,看不到尽头。看起来它应该很简单,但不能绕过它。 SumBackOrderQty显然会被计算两次,因为SumQtyOnHand被评估。到目前为止,我一直在PHP而不是select语句中进行计算,但是想尽可能地清理一下。

当前查询语句为:

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

2 个答案:

答案 0 :(得分:0)

这是一种方法:

select MStockCode, 
(select sum(MBackOrderQty) from sorDetail as T2
where T2.MStockCode = T1.MStockCode ) as SumBackOrderQty,
(select sum(QtyOnHand) from invWarehouse as T3
where T3.MStockCode = T1.MStockCode ) as SumQtyOnHand 
from 
(
select mstockcode from sorDetail
union
select mstockcode from invWarehouse
) as T1

这里有一个小提琴:http://sqlfiddle.com/#!9/fdaca/6

虽然我的SumQtyOnHand值不符合你的价值(正如@Gordon指出的那样)。

答案 1 :(得分:0)

在没有提供完整图片的情况下,就您的RDBMS,数据库架构,您尝试解决的问题的描述以及与上述内容匹配的示例数据而言,以下仅仅是基于Barmar的解决方案的说明评论可能看起来像:

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;