2013-03-19 96 views
0

加入更多聯接後,當我進行加入時,會獲得重複項和其他數據。任何想法我可能會做錯什麼?加入加入行後獲取重複

舊代碼,沒有愚弄工作:

SELECT 
    dbo.dateonly(ctl.datetrans) AS DateRecorded, 
    L.Clientkey AS ClientNum, 
    L.Fullname, 
    cs.Datesvc, 
    cs.chargekey, 
    ctl.CheckNum, 
    ctl.ReceivedFrom, 
    ctl.Delta, 
    CONVERT(varchar(10), dept.deptkey) AS Deptkey, 
    dept.DeptName, 
    dept.PgmName, 
    CONVERT(varchar(10), dept.pgmKey) AS PGMKey, 
    ctl.Transtypelu 
FROM 
    bil_Chargetranslog ctl 
    LEFT JOIN dbo.BIL_ChargeSlips cs 
     ON cs.Chargekey = ctl.Chargekey 
    LEFT JOIN dbo.BLV_Clients L 
     ON cs.ClientKey = L.ClientKey 
    LEFT JOIN Blv_DeptPgm dept 
     ON dept.Pgmkey = cs.PgmLU 
WHERE 
    (
     ctl.Transtypelu = 4 
     AND (
      (
       ctl.descr <> 'Client Payment' 
       AND ctl.descr <> 'Copayment' 
      ) OR ctl.descr IS NULL 
     ) 
    ) 

UNION ALL 

/* note below that the checknum is converted to a varchar for the query*/ 
SELECT 
    dbo.dateonly(daterecorded) AS daterecorded, 
    L.Clientkey AS ClientNum, 
    L.Fullname, 
    DatePayment, 
    NULL AS chargekey, 
    CONVERT(varchar(15), checknum) AS checknum, 
    NULL AS receivedfrom, 
    (- 1 * Amount) AS delta, 
    CONVERT(varchar(10), dept.deptkey) AS Deptkey, 
    dept.DeptName, 
    dept.PgmName, 
    CONVERT(varchar(10), dept.pgmKey) AS PGMKey, 
    4 AS transtypelu 
FROM 
    bil_clientdeposit 
    LEFT JOIN dbo.BLV_Clients L 
     ON bil_clientdeposit.ClientKey = L.ClientKey 
    LEFT JOIN Blv_DeptPgm dept 
     ON dept.Pgmkey = Bil_ClientDeposit.PgmKey 

這是我的新代碼:

SELECT 
    dbo.dateonly(ctl.datetrans) AS DateRecorded, 
    L.Clientkey AS ClientNum, 
    L.Fullname, 
    cs.Datesvc, 
    cs.chargekey, 
    ctl.CheckNum, 
    ctl.ReceivedFrom, 
    ctl.Delta, 
    CONVERT(varchar(10), dept.deptkey) AS Deptkey, 
    dept.DeptName, 
    dept.PgmName, 
    CONVERT(varchar(10), dept.pgmKey) AS PGMKey, 
    ctl.Transtypelu, 
    pr.PlanName 
FROM 
    bil_Chargetranslog ctl 
    LEFT JOIN dbo.BIL_ChargeSlips cs 
     ON cs.Chargekey = ctl.Chargekey 
    LEFT JOIN dbo.BLV_Clients L 
     ON cs.ClientKey = L.ClientKey 
    LEFT JOIN Blv_DeptPgm dept 
     ON dept.Pgmkey = cs.PgmLU 
    left join BIL_ARTRANSLOG AR 
     ON ctl.chargekey = ar.chargekey 
    left join BIL_PAYORPLANS PR 
     ON PR.PAYORPLANKEY = AR.PAYORPLANKEY 
WHERE 
    (
     ctl.Transtypelu = 4 
     and ar.TransTypeLU = 4 
     AND (
      (
       ctl.descr <> 'Client Payment' 
       and AR.ARDescr <> 'Client Payment' 
       AND ctl.descr <> 'Copayment' 
       and AR.ARDescr <> 'Copayment' 
      ) OR ctl.descr IS NULL 
      or ar.ARDescr is null 
     ) 
    ) 

UNION ALL 

/* note below that the checknum is converted to a varchar for the query*/ 
SELECT 
    dbo.dateonly(daterecorded) AS daterecorded, 
    L.Clientkey AS ClientNum, 
    L.Fullname, 
    DatePayment, 
    NULL AS chargekey, 
    CONVERT(varchar(15), checknum) AS checknum, 
    NULL AS receivedfrom, 
    (- 1 * Amount) AS delta, 
    CONVERT(varchar(10), dept.deptkey) AS Deptkey, 
    dept.DeptName, 
    dept.PgmName, 
    CONVERT(varchar(10), dept.pgmKey) AS PGMKey, 
    4 AS transtypelu, 
    NULL as planname 
FROM 
    bil_clientdeposit 
    LEFT JOIN dbo.BLV_Clients L 
     ON bil_clientdeposit.ClientKey = L.ClientKey 
    LEFT JOIN Blv_DeptPgm dept 
     ON dept.Pgmkey = Bil_ClientDeposit.PgmKey 

我加了臺詞:

left join BIL_ARTRANSLOG AR 
    ON ctl.chargekey = ar.chargekey 
left join BIL_PAYORPLANS PR 
    ON PR.PAYORPLANKEY = AR.PAYORPLANKEY 

回答

0

「聯盟」 之間的區別和「union all」是後者返回所有記錄,而前者返回不同的記錄。如果你不想重複記錄,請使用前者。

+0

生病嘗試,快速,讓你知道,謝謝你丹。 – ProfessorKaos64 2013-03-19 15:13:58

+0

什麼都不是,我認爲它有一些問題,有些行有時會有額外的數據,有些則沒有。我只用一個SELECT語句檢查了這一點。 – ProfessorKaos64 2013-03-19 15:20:40

+0

如果是這樣的話,它們不是重複的行。 – 2013-03-19 16:15:28

0

如果您添加了幾個額外的連接並突然看到重複的行,則在其中一個或兩個連接表中可能存在其他連接表中不存在的一對多關係。嘗試做一個SELECT *並查看不同的列是什麼似乎是重複的記錄。

你可以通過你的SELECT陳述與之前刪除重複的:

SELECT DISTINCT 
    dbo.dateonly(ctl.datetrans) AS DateRecorded, 
    L.Clientkey AS ClientNum, 
    L.Fullname, 
    .... 
+0

好主意,生病看看選擇讓我第一。 – ProfessorKaos64 2013-03-19 15:19:58