このクエリを実行すると:
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'
「oItemQty」の場合は 13650、「iItemQty」の場合は 2730 です。私が抱えている問題は、フィールド「合計」が oItemQty - iItemQty (10920) である必要があることです。現在、13650 (oItemQty) を返しています。
ここで何が欠けていますか?別のフィールドとしてサブクエリを実行すると数値が正しいのに、減算しようとすると正しく機能しないのはなぜですか?
更新: キャスティングの問題であることが判明しました。iItemQty を unsigned としてキャストすると、適切に減算されました。