加入更多聯接後,當我進行加入時,會獲得重複項和其他數據。任何想法我可能會做錯什麼?加入加入行後獲取重複
舊代碼,沒有愚弄工作:
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
生病嘗試,快速,讓你知道,謝謝你丹。 – ProfessorKaos64 2013-03-19 15:13:58
什麼都不是,我認爲它有一些問題,有些行有時會有額外的數據,有些則沒有。我只用一個SELECT語句檢查了這一點。 – ProfessorKaos64 2013-03-19 15:20:40
如果是這樣的話,它們不是重複的行。 – 2013-03-19 16:15:28