Sql Inner加入不同的值

时间:2017-10-15 00:55:28

标签: sql inner-join

我有3张桌子:

  • 银行转帐
  • 受益人
  • Callback_Procedure

我必须使用内连接加入这3个表。

在银行转帐中,我有一列Number,并在受益人和回拨表中重复Number

Beneficiaries表中,该号码可以重复多次。

运行内部联接查询时,我得到的结果不止一个,而不是获得唯一值。

Beneficiaries表中,我还有一个列Beneficial_Name,此列唯一标识结果,但是在运行连接查询时,我会得到重复的结果。

我尝试了不同的选择,而不是很好的结果。

以下是我的代码 - 有人可以帮帮我吗?

SELECT DISTINCT   
    dbo.BT_Beneficiaries.Beneficial_Name,   
    dbo.Bank_Transfer.Number, dbo.Bank_Transfer.Start_Date, 
    dbo.Bank_Transfer.Team_Name, dbo.Bank_Transfer.Initiator, 
    dbo.Bank_Transfer.Staff, dbo.Bank_Transfer.Process_Name, 
    dbo.Bank_Transfer.Company_Name, dbo.Bank_Transfer.Billing_Amount, 
    dbo.Bank_Transfer.Entry_ID, dbo.Bank_Transfer.Instructions, 
    dbo.Bank_Transfer.BT_Count, dbo.Bank_Transfer.Misspelled, 
    dbo.Bank_Transfer.Sender, dbo.Bank_Transfer.Odd, 
    dbo.Bank_Transfer.Email, dbo.Bank_Transfer.Beneficiary, 
    dbo.Bank_Transfer.Address, dbo.Bank_Transfer.Urgent,
    dbo.Bank_Transfer.Payment, dbo.Bank_Transfer.Sperry, 
    dbo.Bank_Transfer.Contact, dbo.Bank_Transfer.Reasonable, 
    dbo.Bank_Transfer.Evidence_Completed, dbo.Bank_Transfer.End_Date, 
    dbo.Bank_Transfer.First_Mgr_Approval_Name, dbo.Bank_Transfer.First_Mgr_Approval, 
    dbo.Bank_Transfer.First_Mgr_Approval_Date, dbo.Bank_Transfer.CallBack_Workings, 
    dbo.Bank_Transfer.CallBack_Agreement, dbo.Bank_Transfer.CallBack_Procedure, 
    dbo.Bank_Transfer.Second_Mgr_Approval_Name, dbo.Bank_Transfer.Second_Mgr_Approval,
    dbo.Bank_Transfer.Second_Mgr_Approval_Date, dbo.Bank_Transfer.IB, 
    dbo.Bank_Transfer.IB_Date, dbo.Bank_Transfer.ITL_Signatory_Approval, 
    dbo.Bank_Transfer.ITL_Signatory_Name, 
    dbo.BT_Beneficiaries.Account_Number, dbo.BT_Beneficiaries.Currency, 
    dbo.BT_Beneficiaries.Payment_Details, dbo.BT_Beneficiaries.CurrencyPayment, 
    dbo.BT_Beneficiaries.Amount, dbo.BT_Beneficiaries.Purpose, 
    dbo.BT_Beneficiaries.Evidence, dbo.BT_Beneficiaries.Due_Diligence, 
    dbo.BT_Beneficiaries.WC, dbo.BT_Beneficiaries.Resolution_Directors,
    dbo.BT_Beneficiaries.Manager_Approval, dbo.BT_Beneficiaries.Mgr_Approval_Date, 
    dbo.CB_BankAccount.Number AS Expr3, dbo.CB_BankAccount.Company_Name AS Expr4, 
    dbo.CB_BankAccount.Bank_Name, dbo.CB_BankAccount.Bank_Account, 
    dbo.CB_BankAccount.Currency AS Expr5, dbo.CB_BankAccount.Balance, 
    dbo.CB_BankAccount.Bank_Mandate, dbo.CB_BankAccount.Signing_Arrangement AS Expr6,
    dbo.CB_CallBack_Procedure.Number AS Expr8, dbo.CB_CallBack_Procedure.CallStatus, 
    dbo.CB_CallBack_Procedure.Person_CalledBack, dbo.CB_CallBack_Procedure.DateTime,
    dbo.CB_CallBack_Procedure.Notes, dbo.CB_CallBack_Procedure.Mgr_Approval, 
    dbo.CB_CallBack_Procedure.DateTime_Approval, dbo.CB_CallBack_Procedure.Bank_DateTime 
FROM 
    dbo.Bank_Transfer 
INNER JOIN  
    dbo.BT_Beneficiaries ON dbo.Bank_Transfer.Number = dbo.BT_Beneficiaries.Number 
INNER JOIN 
    dbo.CB_BankAccount ON dbo.BT_Beneficiaries.Number = dbo.CB_BankAccount.BT_Number 
INNER JOIN 
    dbo.CB_CallBack_Procedure ON dbo.CB_BankAccount.BT_Number = dbo.CB_CallBack_Procedure.Number

1 个答案:

答案 0 :(得分:0)

只需添加一个WHERE子句,指定Benficial_Name列需要说明的内容以识别唯一记录:

WHERE Beneficiaries = 'Unique Record'

编辑此内容以说出您需要的任何内容。你可以在加入后放这个。