How to use a UNION with inner joins

  • I'm getting this error ........

    Msg 156, Level 15, State 1, Line 87

    Incorrect syntax near the keyword 'where'.

    I'm probably missing a comma or something somewhere but, can't find anything. Any thoughts?

    declare@StartDate datetime,

    @EndDate datetime

    set @StartDate='2016-12-01'

    set @EndDate='2016-12-31'

    selectRAM.AccountNumber,

    RAM.VisitID,

    HRMRN.PrefixMedicalRecordNumber as UnitNumber,

    HRM.Name,

    RAM.AdmitDateTime,

    PCVAC.Item,

    PCVAC.DatePerformed,

    case when PCVAC.ChargeType is null then 'None' else PCVAC.ChargeType end as ChargeType,

    PCVAC.Interventions

    from livefdb.dbo.RegAcct_Main RAM

    inner join livefdb.dbo.HimRec_VisitData HRVD

    on RAM.SourceID=HRVD.SourceID

    and RAM.VisitID=HRVD.VisitID

    and RAM.PatientID=HRVD.PatientID

    and RAM.RegistrationType_MisRegTypeID=HRVD.VisitType_MisRegTypeID

    and RAM.RegistrationType_MisRegTypeID='IN'

    --and RAM.Location_MisLocID='CCU'

    inner join livefdb.dbo.HimRec_Main HRM

    on RAM.SourceID=HRM.SourceID

    and RAM.PatientID=HRM.PatientID

    inner join livefdb.dbo.HimRec_MedicalRecordNumbers HRMRN

    on HRM.SourceID=HRMRN.SourceID

    and HRM.PatientID=HRMRN.PatientID

    and HRMRN.MrnPrefixID='M'

    inner join

    (

    --RT:PCV AC

    selectPADM.SourceID,

    'RT:PCV AC' as Item,

    left(PADM.PcsAssmntDataID,21) as VisitID,

    convert(date, PADM.DateTimePerformed, 121) as DatePerformed,

    max(case when QuerySetID=6 and QueryNumberID=1 then QueryValue else NULL end) as ChargeType,

    count(*) as Interventions

    from livefdb.dbo.PcsAssmntData_Main PADM

    inner join livefdb.dbo.PcsAssmntData_Queries PADQ

    on PADM.SourceID=PADQ.SourceID

    and PADM.PcsAssmntDataID=PADQ.PcsAssmntDataID

    and PADM.IdentifierID=PADQ.IdentifierID

    cross apply (selecttop 1 *

    from livefdb.dbo.PcsAcctAct_IntActivity

    where PADM.SourceID=SourceID

    and left(PADM.PcsAssmntDataID,21)=VisitID

    and PADM.IdentifierID=InterventionActivityUrnID

    order by InterventionActivityRecordDateTime desc) PAAIA

    where PADM.SourceID='BRO'

    and PADM.PcsAssmntDataID like '%{A^RESP.PCVAC}'

    group by PADM.SourceID,

    PADM.PcsAssmntDataID,

    convert(date, PADM.DateTimePerformed, 121)

    union

    --RT:NPPV/BIPAP

    selectPADM.SourceID,

    'RT:NPPV/BIPAP' as Item,

    left(PADM.PcsAssmntDataID,21) as VisitID,

    convert(date, PADM.DateTimePerformed, 121) as DatePerformed,

    max(case when QuerySetID=6 and QueryNumberID=1 then QueryValue else NULL end) as ChargeType,

    count(*) as Interventions

    from livefdb.dbo.PcsAssmntData_Main PADM

    inner join livefdb.dbo.PcsAssmntData_Queries PADQ

    on PADM.SourceID=PADQ.SourceID

    and PADM.PcsAssmntDataID=PADQ.PcsAssmntDataID

    and PADM.IdentifierID=PADQ.IdentifierID

    cross apply (selecttop 1 *

    from livefdb.dbo.PcsAcctAct_IntActivity

    where PADM.SourceID=SourceID

    and left(PADM.PcsAssmntDataID,21)=VisitID

    and PADM.IdentifierID=InterventionActivityUrnID

    order by InterventionActivityRecordDateTime desc) PAAIA

    where PADM.SourceID='BRO'

    and PADM.PcsAssmntDataID like '%{A^RESP.BIPAP}'

    group by PADM.SourceID,

    PADM.PcsAssmntDataID,

    convert(date, PADM.DateTimePerformed, 121)

    )

    where RAM.SourceID='BRO'

    and RAM.AdmitDateTime>=@StartDate and RAM.AdmitDateTime<dateadd(day, 1, @EndDate)

    order by Name,

    Item,

    PCVAC.DatePerformed

  • You are missing the on clause for the inner join

    😎

    Add the ON clause after the closing parenthesis in line 85

  • Thanx.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply