我有这两个选择陈述
SELECT SizeName,SUM(DESIGNSIZERELEASE.Quantity) as [Release]
FROM DESIGNRELEASE JOIN
DESIGNSIZERELEASE
ON DESIGNRELEASE.DesignReleaseID = DESIGNSIZERELEASE.DesignReleaseID JOIN
SIZESET
ON SIZESET.SizeSetID = DESIGNSIZERELEASE.SizeSetID AND
DESIGNRELEASE.RevisionNo = SIZESET.RevisionNo
WHERE DESIGNRELEASE.RevisionNo = 2 AND DESIGNRELEASE.DesignID='SD00000'
GROUP BY DESIGNRELEASE.DesignID,Towhom,SizeName
ORDER BY case SizeName when 'XS' then 1 when 'S' then 2 when 'M' then 3 when 'L' then 4 when 'XL' then 5 end
AND
SELECT SUM(DESIGNRECEIVEDETAILS.Quantity) as [Receive]
FROM DESIGNRELEASE JOIN
DESIGNSIZERELEASE
ON DESIGNRELEASE.DesignReleaseID = DESIGNSIZERELEASE.DesignReleaseID JOIN
SIZESET
ON SIZESET.SizeSetID = DESIGNSIZERELEASE.SizeSetID AND
DESIGNRELEASE.RevisionNo = SIZESET.RevisionNo JOIN
DESIGNRECEIVE
ON DESIGNRECEIVE.DesignReleaseID = DESIGNRELEASE.DesignReleaseID JOIN
DESIGNRECEIVEDETAILS
ON DESIGNRECEIVE.ReceiptID = DESIGNRECEIVEDETAILS.ReceiptID AND
DESIGNRECEIVEDETAILS.SizeSetID = SIZESET.SizeSetID
WHERE DESIGNRELEASE.RevisionNo = 2 AND DESIGNRELEASE.DesignID='SD00000'
GROUP BY DESIGNRELEASE.DesignID,Towhom,SizeName
ORDER BY case SizeName when 'XS' then 1 when 'S' then 2 when 'M' then 3 when 'L' then 4 when 'XL' then 5 end
有这两个结果
+----------+-------------+
|Size NAME |RELEASE |
+----------+-------------+
|S |20 |
+----------+-------------+
|M |20 |
+----------+-------------+
+----------+
|Receive |
+----------+
|10 |
+----------+
|10 |
+----------+
我需要获得Release和Receive的区别我该怎么做?谢谢顺便说一下,我将它们分开了,因为当我把它们放在一个选择语句中时,我没有得到正确的结果
答案 0 :(得分:1)
我不确定你是否可以在mysql中这样做,但这是你可以在Oracle中做的事情......
SELECT TABLE1.SizeName, TABLE1.Release, TABLE2.Receive, (TABLE1.Release - TABLE2.Receive) Difference
FROM
(SELECT SizeName, SUM(DESIGNSIZERELEASE.Quantity) as [Release]
FROM DESIGNRELEASE JOIN DESIGNSIZERELEASE ON DESIGNRELEASE.DesignReleaseID = DESIGNSIZERELEASE.DesignReleaseID
JOIN SIZESET ON SIZESET.SizeSetID = DESIGNSIZERELEASE.SizeSetID AND DESIGNRELEASE.RevisionNo = SIZESET.RevisionNo
WHERE DESIGNRELEASE.RevisionNo = 2 AND DESIGNRELEASE.DesignID='SD00000'
GROUP BY DESIGNRELEASE.DesignID,Towhom,SizeName
ORDER BY case SizeName when 'XS' then 1 when 'S' then 2 when 'M' then 3 when 'L' then 4 when 'XL' then 5 end) TABLE1
,
(SELECT SizeName, SUM(DESIGNRECEIVEDETAILS.Quantity) as [Receive]
FROM DESIGNRELEASE JOIN DESIGNSIZERELEASE ON DESIGNRELEASE.DesignReleaseID = DESIGNSIZERELEASE.DesignReleaseID
JOIN SIZESET ON SIZESET.SizeSetID = DESIGNSIZERELEASE.SizeSetID AND DESIGNRELEASE.RevisionNo = SIZESET.RevisionNo
JOIN DESIGNRECEIVE ON DESIGNRECEIVE.DesignReleaseID = DESIGNRELEASE.DesignReleaseID
JOIN DESIGNRECEIVEDETAILS ON DESIGNRECEIVE.ReceiptID = DESIGNRECEIVEDETAILS.ReceiptID
AND DESIGNRECEIVEDETAILS.SizeSetID = SIZESET.SizeSetID WHERE DESIGNRELEASE.RevisionNo = 2
AND DESIGNRELEASE.DesignID='SD00000' GROUP BY DESIGNRELEASE.DesignID,Towhom,SizeName
ORDER BY case SizeName when 'XS' then 1 when 'S' then 2 when 'M' then 3 when 'L' then 4 when 'XL' then 5 end) TABLE2
WHERE TABLE1.SizeName = Table2.SizeName