Becky Edwards
asked on
Link two temp tables to get a result set
Need to make a temp table look at the results from another temp table and only bring back results based on the list of people's ID's in the first temp table. I have both queries built and bringing back results.
Need to know how to link the two temp tables and bring back a result set based on both sets.
Example:
IF OBJECT_ID('tempdb..#TEMP1' ) IS NOT NULL
DROP TABLE #TEMP1
GO
SELECT pat_mrn, pat_name, disch_date
INTO #TEMP1
FROM ...
WHERE...
-----then
IF OBJECT_ID('tempdb..#TEMP2' ) IS NOT NULL
DROP TABLE #TEMP2
GO
SELECT pat.MRN, lab.ID
INTO #TEMP2
FROM...
WHERE...
---now what?
I need the pat_mrn, pat_name, disch_date, lab_id but ONLY if the pat_mrn is in both sets of data.
Need to know how to link the two temp tables and bring back a result set based on both sets.
Example:
IF OBJECT_ID('tempdb..#TEMP1'
DROP TABLE #TEMP1
GO
SELECT pat_mrn, pat_name, disch_date
INTO #TEMP1
FROM ...
WHERE...
-----then
IF OBJECT_ID('tempdb..#TEMP2'
DROP TABLE #TEMP2
GO
SELECT pat.MRN, lab.ID
INTO #TEMP2
FROM...
WHERE...
---now what?
I need the pat_mrn, pat_name, disch_date, lab_id but ONLY if the pat_mrn is in both sets of data.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I gave
on t1.pat_mrn = t2.[pat.MRN]
for Becky to link the tables in whatever form is possible. It was not clear to me what the joining columns exactly are.
on t1.pat_mrn = t2.[pat.MRN]
for Becky to link the tables in whatever form is possible. It was not clear to me what the joining columns exactly are.
ASKER
I only tried the first one and it worked perfectly. Thank you both for your comments!
Select t1.pat_mrn , t1.pat_name , t1.disch_date , t2.lab_id From #TEMP1 t1 inner join #TEMP2 t2 on t1.pat_mrn = t2.MRN
instead?