2016-08-17 34 views
1

這裏有兩種表交易Refundtransaction的Sql取得與聯接數據

我想用加入和重複的數據應取來獲取數據。

我使用此查詢

select tr.transactionid, tr.customerid, tr.custname, 
     rt.Amount, rt.Refund_Amount, rt.transactionid, rt.referenceid 
from Transaction tr 
left join RefundTransaction rt on rt.referenceid = tr.transactionid 

它是不是給正確的數據。它不是從退款數據選擇重複數據

Data and expected result

請幫助 我在這裏插入圖像,從中可以得到實際的場景。

+0

我認爲對於提供的數據,您的查詢將正常工作。它將爲匹配的交易ID選擇重複的退款交易。只有在選擇查詢遊覽第一列中進行更改才能替換爲rt.transactionid。 –

回答

0

根據圖片,您需要將TRANSACTION表放入LEFT JOIN。那麼只有你可以從RefundTransaction表中獲得所有條目。

SELECT rt.transactionid 
    ,tr.customerid 
    ,tr.custname 
    ,tr.Amount AS Refund_Amount 
    ,rt.transactionid 
    ,rt.referenceid 
    ,rt.Amount 
FROM RefundTransaction rt 
LEFT JOIN [TRANSACTION] tr ON rt.referenceid = tr.transactionid 
0

如果你要選擇你必須做出一個完整的外所有提交的數據加入

select tr.transactionid, tr.customerid, tr.custname, rt.Amount, rt.Refund_Amount, rt.transactionid, rt.referenceid from Transaction tr full outer join RefundTransaction rt on rt.referenceid = tr.transactionid 

here你可以找到更多信息的加入

0

您正在爲加入excatly相反。

這應該工作。

select tr.transactionid, tr.customerid, tr.custname, 
     rt.Amount, rt.Refund_Amount, rt.transactionid, rt.referenceid 
from RefundTransaction tr left join 
    Transaction rt 
    on rt.referenceid = tr.transactionid