简体   繁体   中英

SQL - Multiplied Column Not summing correctly

I have a query where I have to take two columns ( one quantity and one price) multiply the two together to get the issued cost ( per component) and then SUM them in the second . These parts are calculating correctly. The area I am having issues with is SUMMING these totals. When I summarize this area, I get an incorrect result.

Here is the full query ( Including a union all) The column I am having an issue summarizing is the 'Total Issued Cost' in the second query. It should be taking the 'Issued Cost' column from the first query (T1.IssuedQty * T2.Price) and summing them.

The value I am receiving is not correct ( much higher)

Here is my query

SELECT DISTINCT CONVERT(NVARCHAR(100), T0.DOCNUM) AS 'Production Order'
    ,CONVERT(NVARCHAR(100), ((T1.IssuedQty) * T2.Price)) AS 'Issued Cost'
    ,'' 'Total Issued Cost'
FROM OWOR T0
INNER JOIN WOR1 T1 ON T0.DOCENTRY = T1.DOCENTRY
LEFT JOIN IGE1 T2 ON T2.BaseEntry = T1.DocEntry
    AND T2.BaseType = '202'
    AND T1.ItemCode = T2.ItemCode
LEFT JOIN IGN1 T3 ON T3.BaseEntry = T0.DocEntry
    AND T3.BaseType = '202'
LEFT JOIN IGN1 T8 ON T8.BaseEntry = T0.DocEntry
    AND T3.BaseType = '202'
    AND T8.ItemCode = T0.ItemCode
LEFT JOIN ORSC T4 ON T1.Itemcode = T4.ResCode
FULL OUTER JOIN (
    SELECT ITT1.Code 'BOMCODE'
        ,ITT1.Father 'BomFather'
        ,ITT1.VisOrder
        ,ITT1.Quantity
    FROM ITT1
    ) ITT1 ON T0.Itemcode = ITT1.BomFather
    AND T1.Linenum = ITT1.VisOrder

UNION ALL

SELECT T0.DocNum 'Production Order'
    ,'' 'Issued Cost'
    ,Convert(NVARCHAR(100), (T1.IssuedQty * T2.Price)) AS 'Total Issued Cost'
FROM OWOR T0
LEFT JOIN WOR1 T1 ON T0.DOCENTRY = T1.DOCENTRY
INNER JOIN IGE1 T2 ON T2.BaseEntry = T1.DocEntry
    AND T2.BaseType = '202'
    AND T1.ItemCode = T2.ItemCode

You have changed the inner join to OWOR to a left join in the second query which may be increasing the numbers. Also, I did not find the sum function in the query, so I have added that:

SELECT DISTINCT
      CONVERT(nVARCHAR(100),T0.DOCNUM ) AS 'Production Order'
     ,CONVERT(nvarchar(100), ((T1.IssuedQty)*T2.Price)) as 'Issued Cost'
     , '' 'Total Issued Cost'
FROM OWOR T0 
INNER JOIN WOR1 T1 ON T0.DOCENTRY = T1.DOCENTRY
LEFT JOIN IGE1 T2 ON T2.BaseEntry = T1.DocEntry and T2.BaseType = '202' and T1.ItemCode = T2.ItemCode
LEFT JOIN IGN1 T3 on T3.BaseEntry = T0.DocEntry and T3.BaseType = '202'
LEFT JOIN IGN1 T8 on T8.BaseEntry = T0.DocEntry and T3.BaseType = '202' and T8.ItemCode = T0.ItemCode
LEFT JOIN ORSC T4 on T1.Itemcode = T4.ResCode
FULL OUTER Join 
    (Select ITT1.Code 'BOMCODE', ITT1.Father 'BomFather', ITT1.VisOrder,ITT1.Quantity 
     from ITT1  ) ITT1 on T0.Itemcode = ITT1.BomFather and T1.Linenum = ITT1.VisOrder

Union All

SELECT 
    T0.DocNum 'Production Order'
    ,'' 'Issued Cost'
    , Convert(Nvarchar(100), SUM(T1.IssuedQty*T2.Price))) as 'Total Issued Cost'
FROM OWOR T0 
inner JOIN WOR1 T1 ON T0.DOCENTRY = T1.DOCENTRY
inner JOIN IGE1 T2 ON T2.BaseEntry = T1.DocEntry and T2.BaseType = '202' and T1.ItemCode = T2.ItemCode
Group by T0.DocNum

Hope this helps.

更改字段以包含 SUM(DISCINCT() 示例:Convert(Nvarchar(100), SUM(DISTINCT(T1.IssuedQty*T2.Price))) 有效。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM