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
 Creating a join with "In" in it

Author  Topic 

Lionheart
Starting Member

41 Posts

Posted - 2010-08-09 : 21:57:32
I have 2 tables from which I need to pull data. e.g.

tbl_t1 (Region;CodeID)

Region is a varchar and CodeID is an int, with one number e.g.
US;1
UK;2
FR;3
Can;4

tbl_t2 (Manager;CodeID)

Manager is the manager for a group of regions (varchar) and CodeID in this case is the all the CodeID's that s/he manages (I am thinking would need to be varchar, but confirmation on this would help) e.g.
Fred;1,4
Wilma;2,3

I need to be able to pull a list of who manages each country e.g.
select t1.region, t2.manager
from tbl_t1 inner join
tbl_t2 t2 on t1.codeID in t2.codeID


Any suggestions on how I can get this to work? Is using "in" in the join or something equivalent possible?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-08-09 : 22:03:02
the result will be a string as it is a comma separated value.

See the link below on how to do that
concatenate records without UDF
Rowset string concatenation: Which method is best ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Lionheart
Starting Member

41 Posts

Posted - 2010-08-09 : 22:42:45
Sorry, I may have not been clear enough. The comma separated values are already in the tbl_t2. What I need is to join the rows if the codeID in tbl_t1 is part of the string of CSV CodeID's in tbl_t2.

Thx

LH
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-08-09 : 22:53:12
are you using SQL 2005/2008 ? you can use CROSS APPLY to a split function like
CSVTable
fnParseList


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-08-10 : 11:37:09
You might want to look at properly normalizing your data:
http://en.wikipedia.org/wiki/Database_normalization
Go to Top of Page
   

- Advertisement -