2016-01-21 75 views
0

我有两个相同的表,每个目的不同。两个相同的非关系表比较,并给出行数据结果

表与查询

ID,TypeofAsset, Amount 
1,C,300 
2,A,40 
3,F,90 

表B

ID,TypeofAsset,amount 
1,G,500 
2,A,20 
3,C,150 

结果(表A ID = 1与表B ID = 3比较)

Col, Result 
TypeofAsset, match   -- (C) 
Amount, 150    --(Absolute value of Amount difference) 

任何帮助将欣赏。

感谢

回答

0

你可以做一个JOINTypeofAsset柱像

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; 
+0

我想知道为什么你没有使用'ABS( )' –

+0

@JuanCarlosOropeza编辑答案。感谢您指点。 – Rahul

+0

感谢您的回答!但在这两个例子中,你忘记提及第二排。这是说资产的类型和结果将匹配查询插入的单词?任何帮助在这里! – PantherUSA

相关问题