October 20, 2011 at 4:36 am
[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]
October 20, 2011 at 4:45 am
Murali
What column(s) do you want to match the two result sets on?
John
October 20, 2011 at 4:50 am
;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
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
October 20, 2011 at 4:50 am
[font="Tahoma"]
Hello John,
I would like to join these record sets using the patientid and the rid columns.
[/font]
October 20, 2011 at 4:57 am
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