Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Selecting those observations whose ID is in a list

Author  Topic 

jenzhen99
Starting Member

3 Posts

Posted - 2012-09-18 : 07:59:15
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
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-18 : 08:06:22
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.
Go to Top of Page
   

- Advertisement -