Avatar of Becky Edwards
Becky EdwardsFlag for United States of America

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.
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
Becky Edwards
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

i think it should be:

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?
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

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.
Avatar of Becky Edwards
Becky Edwards
Flag of United States of America image

ASKER

I only tried the first one and it worked perfectly.  Thank you both for your comments!
Microsoft SQL Server 2008
Microsoft SQL Server 2008

Microsoft SQL Server 2008 is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning. Major improvements include the Always On technologies and support for unstructured data types.

50K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo