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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Need (simple?) select help

Author  Topic 

Jacmac
Starting Member

11 Posts

Posted - 2005-06-24 : 12:53:34
I need a select statement that will return rows from table one depending on a column (type) in table two. For example:

Table 1
ID-----|Desc
C0001-|Chamber
C0002-|Walk-in
C0003-|Refrigeration
...

Table 2
ID-----|Type
C0001-|S
C0002-|S
C0003-|G
...

The query needs to return ID's in Table 1 that exist in Table 2 with Type S or G.

Sorry if this is a overly simple question, I don't spend too much time on SQL. Thanks, Jacmac

Kristen
Test

22859 Posts

Posted - 2005-06-24 : 14:18:49
Something like this?

SELECT T1.ID, T1.Desc
FROM Table1 T1
JOIN Table2 T2
ON T2.ID = T1.ID
WHERE T2.Type IN ('S', 'G')

Kristen
Go to Top of Page

Jacmac
Starting Member

11 Posts

Posted - 2005-06-24 : 15:21:29
quote:
Originally posted by Kristen

Something like this?

SELECT T1.ID, T1.Desc
FROM Table1 T1
JOIN Table2 T2
ON T2.ID = T1.ID
WHERE T2.Type IN ('S', 'G')

Kristen



Thanks Kristen, I'm not very good at this, but the final statements that work are:

select * from Active_Chambers T1 join Chamber_Table T2 on T2.INS_Number = T1.INS_Number where T2.Chamber_Type='S'

and

select * from Active_Chambers T1 join Chamber_Table T2 on T2.INS_Number = T1.INS_Number where T2.Chamber_Type='G'

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-06-24 : 15:27:40
Jacmac,

Don't use two queries when you can do it in one. Kristen's solution pulls your two together. IN is just a shorter way of saying OR.

Tara
Go to Top of Page

Jacmac
Starting Member

11 Posts

Posted - 2005-06-24 : 17:44:07
quote:
Originally posted by tduggan

Jacmac,

Don't use two queries when you can do it in one. Kristen's solution pulls your two together. IN is just a shorter way of saying OR.

Tara



Thanks, but I actually needed to do seperate queries to fill two different temporary tables in my program.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2005-06-24 : 23:50:32
If you are new to it then I'd recommend you don't use SELECT * (unless that was just for your post here). You should always (even if you think its a Pain!) put the list of columns you want.

(If you didn't already know that by all means ask "Why?" )

Kristen
Go to Top of Page
   

- Advertisement -