SQL Server - 努力在多个相关表中对值进行分组/求和

时间:2016-06-03 11:10:14

标签: sql-server

我正在运行此查询:

SELECT 
    g.PartNum,
    g.Supplier,
    (g.InitialQuantityToInventory + SUM(COALESCE(t.AmountInventoryAdjusted + t.AmountReturned, 0))) AS StockLevel
FROM 
    GoodsIn g
LEFT JOIN 
    Transfers t ON g.GoodsInNumber = t.GoodsInNumber
WHERE 
    g.PartNum = '123'
GROUP BY 
    g.PartNum, g.Supplier, g.InitialQuantityToInventory

它会返回这些结果:

123,SUP1,67
123,NULL,18
123,NULL,0
123,NULL,45
123,NULL,0

但是我希望供应商名称上的StockLevel(在第3列中)SUM,即使它为空,所以我的预期结果应为:

123,SUP1,67
123,NULL,63

我做错了什么?查询应该(在所有具有相同PartNumber& Supplier的商品编号中)将InitialQuantities及其金额调整为&返回与GoodsIn记录关联的每次转移。

这是该部分的数据& GoodsIn表中的供应商:

GINum  Part Num    Supplier InitialQuantityToInventory
73367   123        NULL     81    
73570   123        NULL     18
74154   123        NULL     320
74835   123        Sup1     0
74836   123        NULL     500
75738   123        Sup1     0

这是Transfers表中的相应行(T是TransferNum的缩写):

GINum      T    Adj    Ret
73367      1    -81    0
74154      1    -200   0
74154      2    -120   45
74835      1    67     0
74836      1    -500   0
75738      1    -300   0
75738      2    300    0

3 个答案:

答案 0 :(得分:1)

您是否需要不按InitialQuantityToInventory分组?

;WITH Logs (Supplier, Initial, StockChange)
AS
(
SELECT
    g.Supplier,
    g.InitialQuantityToInventory,
    (SUM(COALESCE(t.AmountInventoryAdjusted  + t.AmountReturned,0))) AS StockChange
FROM
    GoodsIn g
LEFT JOIN
    Transfers t ON g.GoodsInNumber = t.GoodsId
GROUP BY
    g.Supplier, g.InitialQuantityToInventory
)

SELECT 
    Supplier,
    SUM(Initial) + SUM(StockChange) AS StockLevel
 FROM
    Logs
GROUP BY
    Supplier

可能会发生的事情是你每个独特的InitialQuantityToInventory获得一行,但是从我收集的内容中,你想要对它们进行求和,对吗?

这可能会进一步优化(并且可能不需要CTE),但希望它至少可以返回您期望的数据。

答案 1 :(得分:0)

我认为你不需要GROUP BY InitialQuantityToInventory。请尝试以下查询,并告诉我这是否适合您的需要。

SELECT
    g.PartNum,
    g.Supplier,
    MAX(g.InitialQuantityToInventory) + SUM(COALESCE(t.AmountInventoryAdjusted + t.AmountReturned,0)) AS StockLevel
FROM
    GoodsIn g
LEFT JOIN
    Transfers t ON g.GoodsInNumber = t.GoodsInNumber
WHERE
    g.PartNum = '123'
GROUP BY
    g.PartNum, g.Supplier

答案 2 :(得分:0)

试试这个,在加入和分组之前对转移进行分组

{ [Function: bound ]
  getAsync: [Function: bound ]
  update: [Function: bound ],
  destroy: [Function: bound ],
  create: [Function: bound ],
  build: [Function: bound ],
  _targetClass: 'erudoUser' }
{ [Function: bound ]
  getAsync: [Function: bound ]
  update: [Function: bound ],
  destroy: [Function: bound ],
  create: [Function: bound ],
  build: [Function: bound ],
  _targetClass: 'erudoUser' }
{ [Function: bound ]
  getAsync: [Function: bound ]
  update: [Function: bound ],
  destroy: [Function: bound ],
  create: [Function: bound ],
  build: [Function: bound ],
  _targetClass: 'erudoUser' }
{ [Function: bound ]
  getAsync: [Function: bound ]
  update: [Function: bound ],
  destroy: [Function: bound ],
  create: [Function: bound ],
  build: [Function: bound ],
  _targetClass: 'erudoUser' }