SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Selecting those observations whose ID is in a list
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jenzhen99
Starting Member

3 Posts

Posted - 09/18/2012 :  07:59:15  Show Profile  Reply with Quote
Hi,
I am loading SQL observations into Stata using -SELECT-. I would like to load only those observations, whose ID number is on a list (in another Stata dataset). One way to specify this would be to say -WHERE ID=5 OR ID=23 OR ...", but since I have a few hundred cases to load this seems inconvenient. Is there a more convenient way to do this?
Thanks so much,
JZ

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 09/18/2012 :  08:06:22  Show Profile  Visit webfred's Homepage  Reply with Quote
table_1 holds the data you want to select.
table_2 holds IDs to check in that the select.

-- inner join is one way
select t1.*
from table_1 as t1
inner join table_2 as t2 on t1.id = t2.id

-- in() is another way
select * from table_1
where id in(select id from table_2)

edit:
-- also possible
select t1.* from table_1 t1
where exists(select * from table_2 t2 where t2.id = t1.id)


Too old to Rock'n'Roll too young to die.

Edited by - webfred on 09/18/2012 08:42:11
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000