我正在尝试计算两个量(sum1和sum2)并从sum2中减去sum1作为一个名为tqty的列。以下是sum1的计算方法:
SELECT tblStockMaster.SmName AS StockMaster,
SUM(isnull(tblPurchaseOrderD.TotalQuantity, 0)) AS sum1
FROM tblStockMaster
LEFT JOIN tblPurchaseOrderD
ON tblPurchaseOrderD.StockControl = tblStockMaster.SmName
GROUP BY tblStockMaster.SmName
和结果
itemname sum1 ACTIVE MAPE 1 0 DEVELOPMENTAL READING 2 2 EDUCATIONAL TECHNOLOGY 2 3 FACILITATING LEARNING 2 1 GRAMAMAR AND COMPOSITION 3 HUMANITIES AND DIGITAL ARTS 0 INTRODUCTIORY MEDICAL PHYSICS FOR 1 PHILLIPINE HISTORY AND GOVERMENT 0
这里是sum2:
SELECT tblStockMaster.SmName AS StockMaster,
SUM(isNull(tblReceivingD.TotalQuantity, 0)) AS sum2
FROM tblStockMaster
LEFT JOIN tblReceivingD
ON tblReceivingD.StockControlR = tblStockMaster.SmName
GROUP BY tblStockMaster.SmName
和结果
itemname sum2 ACTIVE MAPE 1 0 DEVELOPMENTAL READING 2 0 EDUCATIONAL TECHNOLOGY 2 0 FACILITATING LEARNING 2 0 GRAMAMAR AND COMPOSITION 396 HUMANITIES AND DIGITAL ARTS 0 INTRODUCTIORY MEDICAL PHYSICS FOR HEALTH 200 PHILLIPINE HISTORY AND GOVERMENT WORKBOOK 6th EDIT 0
我希望得到结果:
itemname Sum1 sum2 tqty ACTIVE MAPE 1 0 0 0 DEVELOPMENTAL READING 2 2 0 -2 EDUCATIONAL TECHNOLOGY 2 3 0 -3 FACILITATING LEARNING 2 1 0 -1 GRAMAMAR AND COMPOSITION 3 396 393 HUMANITIES AND DIGITAL ARTS 0 0 0 INTRODUCTIORY MEDICAL PHYSICS FOR HEALTH 1 200 199 PHILLIPINE HISTORY AND GOVERMENT WORKBOOK 6th EDIT 0 0 0
我正在尝试将sum1和sum2结合起来得到如上所述的结果。
答案 0 :(得分:1)
WITH PO
AS (SELECT SUM(TotalQuantity) AS sum1,
StockControl AS SmName
FROM tblPurchaseOrderD
GROUP BY StockControl),
R
AS (SELECT SUM(TotalQuantity) AS sum2,
StockControlR AS SmName
FROM tblReceivingD
GROUP BY StockControlR)
SELECT tblStockMaster.SmName AS StockMaster,
ISNULL(sum1, 0) AS sum1,
ISNULL(sum2, 0) As sum2,
ISNULL(sum2, 0) - ISNULL(sum1, 0) as tqty
FROM tblStockMaster
LEFT JOIN PO
ON PO.SmName = tblStockMaster.SmName
LEFT JOIN R
ON R.SmName = tblStockMaster.SmName
答案 1 :(得分:0)
这有用吗?
select StockMaster, sum1, sum2, sum2-sum1 as tqtyfrom
(SELECT tblStockMaster.SmName AS StockMaster,
SUM(isnull(tblPurchaseOrderD.TotalQuantity, 0)) AS sum1
FROM tblStockMaster
LEFT JOIN tblPurchaseOrderD
ON tblPurchaseOrderD.StockControl = tblStockMaster.SmName
GROUP BY tblStockMaster.SmName
)A
inner join
(
SELECT tblStockMaster.SmName AS StockMaster,
SUM(isNull(tblReceivingD.TotalQuantity, 0)) AS sum2
FROM tblStockMaster
LEFT JOIN tblReceivingD
ON tblReceivingD.StockControlR = tblStockMaster.SmName
GROUP BY tblStockMaster.SmName )B
on A.StockMaster = B.StockMaster
根据您的实体彼此之间的关系,inner join
可能会更改为Left
或Right
答案 2 :(得分:-1)
Msg 4104,Level 16,State 1,Line 1 无法绑定多部分标识符“tblPurchaseOrderD.TotalQuantity”。
我发布这个错误,当你发布这个感谢发布它帮助这么多希望不能解决这个
WITH PO
AS (SELECT SUM(tblPurchaseOrderD.TotalQuantity) AS sum1,
StockControl AS SmName
FROM tblPurchaseOrderD
GROUP BY StockControl),
R
AS (SELECT SUM(tblPurchaseOrderD.TotalQuantity) AS sum2,
StockControlR AS SmName
FROM tblReceivingD
GROUP BY StockControlR)
SELECT tblStockMaster.SmName AS StockMaster,
ISNULL(MAX(sum1), 0) AS sum1,
ISNULL(MAX(sum2), 0) As sum2,
ISNULL(MAX(sum2), 0) - ISNULL(MAX(sum1), 0) as tqty
FROM tblStockMaster
LEFT JOIN PO
ON PO.SmName = tblStockMaster.SmName
LEFT JOIN R
ON R.SmName = tblStockMaster.SmName
GROUP BY tblStockMaster.SmName
我得到错误先生--->消息4104,等级16,状态1,行1无法绑定多部分标识符“tblPurchaseOrderD.TotalQuantity”。 < ----这一个
抱歉,先生,我想我已经收到了你的帖子!谢谢先生,它帮助了我在你的帖子里得到了它,非常感谢它帮助........