你真的不需要相交或除了你要找的东西,但你可以这样。
SELECT Encounter.EncounterNumber, substring(Encounter.EncounterNumber,4,9) as Acct,
MedicalRecordNumber, [AdmitDate - CCYYMMDD] as AdmitDate, [DischargeDate - CCYYMMDD] as
DischDate, DischargeDisposition, Encounter.Age, EnctrAPR.APRDRG, Age18, Age18To64, Age65
from Encounter
full outer join EnctrAPR on
substring(Encounter.EncounterNumber,4,9) = EnctrAPR.EncounterNumber
where HSP# = 1
and InOutCode = 'I'
and ActualTotalCharge >0
and AdmitSubService <> 'SIG'
and [DischargeDate - CCYYMMDD] between @StartDate and @EndDate
and Encounter.Age >= 18
INTERSECT -- OR EXCEPT
SELECT Encounter.EncounterNumber, substring(Encounter.EncounterNumber,4,9) as Acct,
MedicalRecordNumber, [AdmitDate - CCYYMMDD] as AdmitDate, [DischargeDate - CCYYMMDD] as
DischDate, DischargeDisposition, Encounter.Age, EnctrAPR.APRDRG, Age18, Age18To64, Age65
from EnctrAPR
join Encounter on
substring(Encounter.EncounterNumber,4,9) = EnctrAPR.EncounterNumber
where HSP# = 1
and InOutCode = 'I'
and ActualTotalCharge >0
and AdmitSubService <> 'SIG'
and [DischargeDate - CCYYMMDD] between @StartDate and @EndDate
and Encounter.Age >= 18
你可以通过这种方式完成它,但我不推荐它。这是一种蛮力的方法。要仅获取 EnctrAPR 中的记录,您可以更改 FROM EnctrAPR 并使用内连接到 Encounter。
SELECT Encounter.EncounterNumber, substring(Encounter.EncounterNumber,4,9) as Acct,
MedicalRecordNumber, [AdmitDate - CCYYMMDD] as AdmitDate, [DischargeDate - CCYYMMDD] as
DischDate, DischargeDisposition, Encounter.Age, EnctrAPR.APRDRG, Age18, Age18To64, Age65
from EnctrAPR
join Encounter on
substring(Encounter.EncounterNumber,4,9) = EnctrAPR.EncounterNumber
where HSP# = 1
and InOutCode = 'I'
and ActualTotalCharge >0
and AdmitSubService <> 'SIG'
and [DischargeDate - CCYYMMDD] between @StartDate and @EndDate
and Encounter.Age >= 18
要仅获取 EnctrApr 中不存在的记录,我将使用左连接:
SELECT Encounter.EncounterNumber, substring(Encounter.EncounterNumber,4,9) as Acct,
MedicalRecordNumber, [AdmitDate - CCYYMMDD] as AdmitDate, [DischargeDate - CCYYMMDD] as
DischDate, DischargeDisposition, Encounter.Age, EnctrAPR.APRDRG, Age18, Age18To64, Age65
from Encounter
left outer join EnctrAPR on
substring(Encounter.EncounterNumber,4,9) = EnctrAPR.EncounterNumber
where HSP# = 1
and InOutCode = 'I'
and ActualTotalCharge >0
and AdmitSubService <> 'SIG'
and [DischargeDate - CCYYMMDD] between @StartDate and @EndDate
and Encounter.Age >= 18
and EnctrAPR.EncounterNumber IS NULL