0

我写了一个存储过程:

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

我想使用EXCEPTorINTERSECT指令向我显示不在EnctrAPR其中的行Encounter。请注意,我EncounterNumber在两个表上有不同的格式。

我将如何做到这一点?

4

2 回答 2

1

获取那些在 Encounter 中而不在 EnctrAPR 中的记录,则只需使用left outer join而不是full outer join,并添加一个子句,不包括 EnctrAPR.EncounterNumber 的空值。

IE

SELECT  
    Encounter.EncounterNumber,  
    substring(Encounter.EncounterNumber,4,9) as Acct,  
    ...
    EnctrAPR.APRDRG,
    Age18, Age18To64, Age65 
from  
    Encounter 
    left outer join EnctrAPR on  
        substring(Encounter.EncounterNumber,4,9) = EnctrAPR.EncounterNumber 
where  
    EnctrAPR.EncounterNumber is null
    and HSP# = 1 
    and InOutCode = 'I' 
    and ActualTotalCharge >0  
    and AdmitSubService <> 'SIG' 
    and [DischargeDate - CCYYMMDD] between @StartDate and @EndDate 
    and Encounter.Age >= 18 

请注意,值EnctrAPR.APRDRG始终为空,因为 EnctrAPR 没有匹配的行。

于 2012-10-05T14:49:34.407 回答
1

你真的不需要相交或除了你要找的东西,但你可以这样。

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
于 2012-10-05T14:53:39.160 回答