Joining 2 result sets

  • [font="Tahoma"]Hello Friends,

    I am trying to join 2 record sets. The first record set is generated by joining 2 tables and the second record set is again generated by 2 tables.

    Following is the sample query used to derive the record sets

    --First record set

    select x.patientid,row_number() over (partition by x.patientid order by x.observationid) as rid,x.observationid,y.code from

    xxx x

    Inner Join yyy y on x.observationid = y.observationid

    where y.code like 'glucose' and o.patientid = 327

    --second record set

    select x.patientid,row_number() over (partition by x.patientid order by x.observationid) as rid,x.observationid,y.code from

    xxx x

    Inner Join yyy y on x.observationid = y.observationid

    where y.code like 'alt' and o.patientid = 327

    Now i would like do a full outer join of these record sets using the patientid and the rid fields. Can somebody help me out in writing the query for joining these 2 record sets.

    Thanks & regards,

    Murali[/font]

  • Murali

    What column(s) do you want to match the two result sets on?

    John

  • ;WITH First_record_set as (

    select x.patientid,row_number() over (partition by x.patientid order by x.observationid) as rid,x.observationid,y.code

    from xxx x

    Inner Join yyy y on x.observationid = y.observationid

    where y.code like 'glucose' and o.patientid = 327

    ),

    second_record_set as (

    select x.patientid,row_number() over (partition by x.patientid order by x.observationid) as rid,x.observationid,y.code

    from xxx x

    Inner Join yyy y on x.observationid = y.observationid

    where y.code like 'alt' and o.patientid = 327

    )

    SELECT f.*, s.*

    FROM First_record_set f

    FULL OUTER JOIN second_record_set s

    ON s.patientid = f.patientid AND s.rid = f.rid

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • [font="Tahoma"]

    Hello John,

    I would like to join these record sets using the patientid and the rid columns.

    [/font]

  • Hey Chris.. That worked the way that i was looking for.. Thanks for your help and Thanks John for ur response.

Viewing 5 posts - 1 through 4 (of 4 total)

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