我有一個像下面的查詢:減法 - 嵌套查詢SQL
select ISNULL(sum(fonhand),0.00) as 'Supply', 0 as 'Demand'
from invoice where ptno = @ptno
union
select distinct a.quantity as 'Supply', 0 as 'Demand'
from jbMstr a join jbDetails b on a.fjobno = b.fjobno
where a.ptno = @ptno AND a.status = 'RELEASED' AND fbmsource = 'S'
union
select 0 as 'Supply', ftotqty as 'Demand'
from jbDetails a join jbMstr b on a.fjobno = b.fjobno
where fbompart = @ptno and fstatus = 'RELEASED' AND fbmsource = 'S'
輸出像以下:
Supply Demand Avail
-4.00000 0.0000000000 -4
0.00000 1.0000000000 -5
0.00000 1.0000000000 -6
0.00000 4.0000000000 -10
0.00000 -1.0000000000 -9
在這裏,我想獲得新的欄目「庫存狀況」,它應該從每行的「需求」欄中減去。如何實現這一點?
任何人都可以幫助我做到這一點? 在此先感謝...
標記使用的dbms。 (ISNULL()是特定於產品的功能。) – jarlh
由於UNION刪除所有重複項,因此無需執行SELECT DISTINCT。 – jarlh