SUM与多个表MySQL

时间:2016-07-24 09:02:24

标签: mysql

问题在于制造餐桌的人不是我。这个问题基于数据库适应。所以,我需要你的帮助。

我的表格需要与商品的Quantity相符。

我使用此代码itemsorderset_details表格获取Quantaties。

SELECT
    I.ItemId, I.Code, I.Item, I.Minimum, I.Maximum,
    IFNULL
    (
        (
            SELECT SUM(ROUND(h.Quantity))
            FROM orderset_details AS H
            WHERE H.ItemId = I.ItemId
        )
        , 0
    ) as 'Balance'
FROM items AS I
GROUP BY I.ItemId

输出

enter image description here

但是有些表需要是负数量。我的代码只获得了1件商品的所有数量。

SELECT 
   sts.TransferDate as 'Transaction Date'
  , sts.TransNumber as 'Document Number'
  , orderset_details.Quantity as 'Quantity'
  , '' as 'Po Number'
  , '' as 'Sales Invoice'
  , orderset_details.Cost as 'Cost'
  , orderset_details.SerialNumber as 'Serial Number'
  , orderset_details.LotNumber as 'Lot Number'
  , location.Location as 'Location' 
FROM sts 
LEFT JOIN orderset_details ON sts.OrdersetId = orderset_details.OrdersetId
LEFT JOIN location ON location.LocationId = sts.LocationIdTo  
WHERE orderset_details.ItemId = '4786' 
UNION 
SELECT 
      drs.OrderDate 
    , drs.TransNumber 
    , orderset_details.Quantity 
    , drs.PONumber 
    , '' 
    , orderset_details.Cost 
    , orderset_details.SerialNumber 
    , orderset_details.LotNumber  
    , suppliers.Supplier  
FROM drs 
LEFT JOIN orderset_details ON drs.OrdersetId = orderset_details.OrdersetId 
LEFT JOIN suppliers ON suppliers.SupplierId = drs.SupplierId 
WHERE orderset_details.ItemId = '4786' 
UNION  
SELECT 
      stockadjustment.TransactionDate 
    , stockadjustment.TransactionId 
    , IF(reason.AddsToStock = '1', orderset_details.Quantity, 0 - orderset_details.Quantity)
    , '' 
    , '' 
    , orderset_details.Cost 
    , orderset_details.SerialNumber 
    , orderset_details.LotNumber
    , location.Location   
FROM stockadjustment
LEFT JOIN reason ON reason.ReasonId = stockadjustment.ReasonId
LEFT JOIN orderset_details ON stockadjustment.OrdersetId = orderset_details.OrdersetId 
LEFT JOIN location ON location.LocationId = stockadjustment.LocationId 
WHERE orderset_details.ItemId = '4786' 
UNION  
SELECT 
      issueslip.OrderDate  
    , issueslip.TransNumber 
    , IF(issueslip.OrdersetId = orderset_details.OrdersetId, 0 - orderset_details.Quantity, -1 * orderset_details.Quantity) 
    , '' 
    , '' 
    , orderset_details.Cost 
    , orderset_details.SerialNumber 
    , orderset_details.LotNumber 
    , customers.Customer     
FROM issueslip 
LEFT JOIN orderset_details ON issueslip.OrdersetId = orderset_details.OrdersetId 
LEFT JOIN customers ON customers.CustomerId = issueslip.CustomerId 
WHERE orderset_details.ItemId = '4786' 
UNION  
SELECT 
      invoice.OrderDate 
    , invoice.TransNumber 
    , IF(invoice.OrdersetId = orderset_details.OrdersetId, 0 - orderset_details.Quantity, -1 * orderset_details.Quantity)
    , '' 
    , '' 
    , orderset_details.Cost 
    , orderset_details.SerialNumber 
    , orderset_details.LotNumber 
    , customers.Customer   
FROM invoice 
LEFT JOIN orderset_details ON invoice.OrdersetId = orderset_details.OrdersetId 
LEFT JOIN customers ON customers.CustomerId = invoice.CustomerId 
WHERE orderset_details.ItemId = '4786'

我只需要从此代码中获取Negative Quantity Condition LIKE IF(reason.AddsToStock = '1', orderset_details.Quantity, 0 - orderset_details.Quantity)以及其他2个查询。

我如何将这个查询与我上面的第一个查询相结合?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

SELECT    I.ItemId, I.Code, I.Item, I.Minimum, I.Maximum,
          COALESCE(SUM(ROUND(
              CASE SA.ReasonId WHEN R.ReasonId 
                  THEN  h.Quantity 
                  ELSE -h.Quantity 
              END)), 0) as 'Balance'
FROM       items AS I
CROSS JOIN reason AS R
LEFT JOIN  orderset_details AS H
       ON  H.ItemId = I.ItemId
LEFT JOIN  stockadjustment AS SA
       ON  SA.OrdersetId = H.OrdersetId 
WHERE      R.AddsToStock = '1'
GROUP BY  I.ItemId