2016-01-21 77 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

相關問題