当我运行此查询时:
select
(IFNULL(ROUND(convertUnits('40892',SUM(o.qty),o.pricingUnit,'FT')),0)) as oItemQty,
(SELECT IFNULL(sum(i.qty),0) from inventory i where i.partID='40892' and i.type=16 and i.refDocNum=w.woID and i.refApp='WO') as iItemQty,
(IFNULL(ROUND(convertUnits('40892',SUM(o.qty),o.pricingUnit,'FT')),0) - (SELECT IFNULL(sum(i.qty),0) from inventory i where i.partID='40892' and i.type=16 and i.refDocNum=w.woID and i.refApp='WO')) as sum
from orderitem o left join wo w on o.orderitemID=w.orderitemID
where o.partID='40892' and
w.status not in (1,5) and
(SELECT cancelDate from orders where orders.orderID=o.orderID)='0000-00-00' and
o.createWO=1 and
(SELECT orderDate from orders where orders.orderID=o.orderID) >='2012-07-01'
我得到 13650 的“oItemQty”和 2730 的“iItemQty”。我遇到的问题是“总和”字段应该是 oItemQty - iItemQty (10920)。现在它返回 13650 (oItemQty)。
我在这里想念什么?为什么当我将子查询作为单独的字段运行时,数字是正确的,但是当我尝试减去它时却不能正常工作?
更新:原来这是一个铸造问题。一旦我将 iItemQty 转换为无符号,它就会正确减去。