添加几个连接后,当我进行连接时,我会得到重复项和其他数据。我可能做错了什么想法?
没有欺骗的旧代码:
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 :(得分:0)
“union”和“union all”之间的区别在于后者返回所有记录,而前者返回不同的记录。如果您不想要重复记录,请使用前者。
答案 1 :(得分:0)
如果添加了一些额外的连接并且突然看到重复的行,则在一个或两个连接的表中可能存在一对多的关系,而这些关系在其他连接的表中不存在。尝试执行SELECT *
,看看不同的列是什么似乎是重复记录。
您可以在SELECT
语句前加上:
SELECT DISTINCT
dbo.dateonly(ctl.datetrans) AS DateRecorded,
L.Clientkey AS ClientNum,
L.Fullname,
....