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
4

2 回答 2

0

如果您添加了几个额外的连接并突然看到重复的行,则可能在一个或两个连接表中存在一对多关系,而在其他连接表中不存在。尝试做 aSELECT *并查看似乎是重复记录的不同列。

您可以通过在SELECT语句前加上以下内容来删除重复项:

SELECT DISTINCT
    dbo.dateonly(ctl.datetrans) AS DateRecorded, 
    L.Clientkey AS ClientNum, 
    L.Fullname, 
    ....
于 2013-03-19T15:15:33.293 回答
0

“union”和“union all”之间的区别在于后者返回所有记录,而前者返回不同的记录。如果您不想重复记录,请使用前者。

于 2013-03-19T15:10:53.653 回答