Solved

Join 2 tables & get a Comma seperated list of names

Posted on 2006-06-19
6
239 Views
Last Modified: 2011-10-03
Hi,

I have 2 tables like this:

Table1:
TID int

Table2:
NAME varchar,
TSTRING varchar

Table1  Data:

TID
101
200
309
408
987
9
8


Table2 Data:
NAME        TSTRING
A              101,200,408,987
B              9,8,101,200
C              987,408,309

TSTRING id a list of TID's.
The result I should get USING SQLserver 2000 as

TID           Name
101            A,B
200            A,B
309            C
408            A,C
987            A,C
9               B
8               B

Can anyone know how to do this?

Thanks,
Neera
0
Comment
Question by:neerak
6 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 16939033
go back and revisit your design...

once you store your data in properly normalised tables...
then the client side display results you desire will be trivial to achieve..

and ps convince me this isn't homework please...
0
 

Author Comment

by:neerak
ID: 16939236
Here is the clear description:
I have the  SQL Server 2000 database with a table "ZONES" and a table of "TERMS".  Each entry in the ZONES table contains a ZONE Id 'ZID'  and Name of the ZONE "ZNAME"  and Zone String "ZSTRING", which is a comma seperated list of term id' & it can vary not fixed.Each TERM has a TERM Id "TID", Term Name "TNAME", etc..

How can I create a query or cursor that gives the comma seperated list of Zone names "ZNAMES" for the terms with TIDS?

ZONES table:

ZID      ZNAME       ZSTRING
 ---    ------        ----------
1        TX           101,200,408,987
2        VA           9,8,101,200
3        MA          987,408,309

TERMS table:
TID                 TNAME
----                 --------
101                 HOU
200                 ADH
309                 INH
408                 HALL
987                 SYHI
9                     KLI
8                    TPU


The Result I should get as:
TID          ZONENAMES
----         ---------------
101             TX,VA    
200             TX,VA  
309              MA
408             TX,MA
987             TX,MA    
9                 VA
8                 VA

I am working on some application, I am confused here, If you could help that would be great.

Thanks,
neera

0
 
LVL 10

Expert Comment

by:StephenCairns
ID: 16939560
Where does the "TERMS table: " come into this?
The Result I should get as:
TID          ZONENAMES
----         ---------------
101             TX,VA    
200             TX,VA  
309              MA
408             TX,MA
987             TX,MA    
9                 VA
8                 VA
 
is simply
ZID      ZNAME       ZSTRING
 ---    ------        ----------
1        TX           101,200,408,987
2        VA           9,8,101,200
3        MA          987,408,309

the other way round

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:neerak
ID: 16939605
"ZSTRING " from ZONES table consists of list of TID's from TERMS table.
I tried to convert the ZSTRING from ZONE table into a temp table & joined the this temp table with TERMS table TID.
0
 

Author Comment

by:neerak
ID: 16939634
In the the Result, I have to get, the TID is from the TERMS table
TID          ZONENAMES
----         ---------------
101             TX,VA    
200             TX,VA  
309              MA
408             TX,MA
987             TX,MA    
9                 VA
8                 VA
0
 
LVL 17

Accepted Solution

by:
HuyBD earned 500 total points
ID: 16939814
This is not good way but good result :))

Create Function dbo.getlist (@id as int)
Returns varchar(1000)
As

BEGIN

Declare @reval varchar(1000)

Select @reval = IsNull(@reval + ',', '') + ZNAME
From ZONES
where (','+rtrim(ZSTRING)+',') like ('%,'+rtrim(cast(@id as char(10)))+',%')
Return @reval
END

And then:
select TID, dbo.getlist(TID) as ZONENAMES from TERMS
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now