如何从两个数据表中减去相似列和值的行

时间:2017-11-23 11:45:41

标签: c# sql datatable

enter image description here

如何从两个sql数据表中减去值?在pic我有两个数据表,我想先匹配table1" partnum"列,如果它与表2" partnum"匹配然后从table2" FinalStockout"中减去table1" FinalstockIN"值。然后将其显示在另一列中。

我从别人的建议中尝试过但这并没有显示完整的数据。

with spare_parts as (

SELECT s.partnum,partdesc,partmodel,stockin,dateofstockin, (SELECT 
SUM(stockin)
FROM Tbl_SpareParts
GROUP BY partnum
HAVING partnum = s.partnum) AS FinalTotalStockIN FROM Tbl_SpareParts s
),
cases as (
SELECT 
b.partnum,partdesc,partmodel,outstock,outstockdate,
outstockcaseid_billnum,remarks, (SELECT SUM(outstock)
FROM Tbl_OutCaseID
GROUP BY partnum
HAVING partnum = b.partnum) AS FinalTotalStockOut FROM Tbl_OutCaseID b
)
select
s.partnum, s.partdesc, s.partmodel, s.stockin, 
s.dateofstockin, s.FinalTotalStockIN,
c.partnum AS Stockoutpartnum, 
c.partdesc AS Stockoutpartdesc, 
c.partmodel AS stockoutpartmodel, c.outstock, 
c.outstockdate, c.outstockcaseid_billnum, 
c.remarks, c.FinalTotalStockOut
from
spare_parts s
join cases c on s.partnum = c.partnum

1 个答案:

答案 0 :(得分:0)

SELECT STOCKOUTPARTNUM,STOCKOUTPARTNUM,NULL FROM TABLE 2 MINUS SELECT PARTNUM,NULL,PARTNUM FROM TABLE 1

DATATYPE必须与PARTNUM相同的STOCKOUTPARTNUM