Sql server 两个相同的非关系表比较并给出行数据中的结果

Sql server 两个相同的非关系表比较并给出行数据中的结果,sql-server,Sql Server,我有两张相同的桌子,各有不同的用途 表A 表B 查询结果(表A id=1与表B id=3比较) 任何帮助都将不胜感激 谢谢您可以在TypeofAsset列上执行加入 select t1.TypeofAsset, case when t1.Amount > t2.Amount then t1.Amount - t2.Amount else t2.Amount - t1.Amount end as diff_amount from tablea t1 join tableb t2 on t

我有两张相同的桌子,各有不同的用途

表A 表B 查询结果(表A id=1与表B id=3比较) 任何帮助都将不胜感激


谢谢

您可以在
TypeofAsset
列上执行
加入

select t1.TypeofAsset, 
case when t1.Amount > t2.Amount then t1.Amount - t2.Amount 
else t2.Amount - t1.Amount end as diff_amount
from tablea t1 join tableb t2 on t1.TypeofAsset = t2.TypeofAsset;
您也可以像下面这样使用
ABS()
函数

select t1.TypeofAsset, 
ABS(t1.Amount - t2.Amount) as diff_amount
from tablea t1 join tableb t2 on t1.TypeofAsset = t2.TypeofAsset;

谢谢你的回答!!但在这两个例子中,你们都忘了提到第二排。据说资产的类型和结果会和查询中插入的单词匹配吗?有人帮忙吗!!
Col, Result
TypeofAsset,  match          -- (C)
Amount, 150                --(Absolute value of Amount difference)
select t1.TypeofAsset, 
case when t1.Amount > t2.Amount then t1.Amount - t2.Amount 
else t2.Amount - t1.Amount end as diff_amount
from tablea t1 join tableb t2 on t1.TypeofAsset = t2.TypeofAsset;
select t1.TypeofAsset, 
ABS(t1.Amount - t2.Amount) as diff_amount
from tablea t1 join tableb t2 on t1.TypeofAsset = t2.TypeofAsset;