私の解決策は以下の通りです: -
CREATE TABLE Base (Id INT, name NVARCHAR(10))
INSERT INTO Base VALUES (1, 'ex1'), (2, 'ex2'), (3, 'ex3')
CREATE TABLE TableA (ExtId INT, p INT)
CREATE TABLE TableB (ExtId INT, p INT)
CREATE TABLE TableC (ExtId INT, p INT)
INSERT INTO TableA VALUES (1, 10), (1, 20), (2, 40), (3, 10), (3, 10)
INSERT INTO TableB VALUES (1, 10), (1, 5), (3, 5), (3, 5)
INSERT INTO TableC VALUES (1, 10), (2, 10), (2, 20), (3, 40)
SELECT BS.Id, BS.name, ISNULL(A.A_total, 0) + ISNULL(B.B_total, 0) - ISNULL(C.C_total, 0)
FROM Base BS
LEFT JOIN (SELECT ExtId, SUM(p) AS A_total FROM TableA GROUP BY ExtId) A ON BS.Id = A.ExtId
LEFT JOIN (SELECT ExtId, SUM(p) AS B_total FROM TableB GROUP BY ExtId) B ON BS.Id = B.ExtId
LEFT JOIN (SELECT ExtId, SUM(p) AS C_total FROM TableC GROUP BY ExtId) C ON BS.Id = C.ExtId
ORDER BY BS.Id
あなたは、彼らがテーブルAまたはBにアレント – Nebi
相撲IDまたはC –
AND(表A +表Bを)何をしようとしなかった - 表Cと、すべてのIDがテーブルベース –