寻找有关从2个不同表格中获取数据集的最佳方法的建议,比较它们,然后仅返回具有BALANCE差异的NAMES。
例如,这是我的表格:
Table A
NAME STATE CITY COMPANY BALANCE
Joe CA San Diego xyz 100
John CA San Diego abc 50
Jim CA Los Angeles xyz 35
Table B
NAME STATE CITY COMPANY BALANCE
Joe CA San Diego xyz 75
John CA San Diego abc 50
Jim CA Los Angeles xyz 40
我想回复:
NAME STATE CITY COMPANY DIFFERENCE
Joe CA San Diego xyz 25
Jim CA Los Angeles xyz 5
提前致谢!!!
答案 0 :(得分:2)
简单的内部联接在这里可以正常工作
SELECT
t1.Name,
t1.State,
t1.City,
ABS(t1.Balance - t2.Balance
FROM
Table1 t1
INNER JOIN Table2 t2
ON t1.Name = t2.Name
and t1.State = t2.State
and t1.City = t2.City
and t1.Company = t2.Company
WHERE
t1.Balance <> t2.Balanace
一些注释