SupplierId NetAmt
1 400
2 400
3 300
2 200
SupplierId RecdAmt
2 200
2 200
2 200
预期结果:
1 400
3 300
第一个表是向内表,第二个表是付款表,当我向已完全付款的供应商付款时,不应再次显示
SupplierId NetAmt
1 400
2 400
3 300
2 200
SupplierId RecdAmt
2 200
2 200
2 200
预期结果:
1 400
3 300
第一个表是向内表,第二个表是付款表,当我向已完全付款的供应商付款时,不应再次显示
你可以这样做
select SupplierId , NetAmt from table1
where SupplierId not in (select SupplierId from table2)
或者
你也可以试试这个
SELECT SupplierId , NetAmt
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.SupplierId = t2.SupplierId
WHERE t2.SupplierId IS NULL
似乎您需要为每个供应商取得余额
SELECT ISNULL(NetTable.SupID, RecTable.SupID) AS SupID, (ISNULL(NetTable.TotalNet,0) - ISNULL(RecTable.TotalRec,0)) AS BalAmt
FROM (SELECT SupID, SUM(NetAmt) AS TotalNet FROM Table_1 GROUP BY SupID) AS NetTable
LEFT OUTER JOIN
(SELECT SupID, SUM(RecAmt) AS TotalRec FROM Table_2 GROUP BY SupID) AS RecTable
ON NetTable.SupID = RecTable.SupID
WHERE NetTable.TotalNet <> RecTable.TotalRec
需要单独对每个表进行 GROUP BY 供应商并加入摘要。