Solved

Join 2 tables & get a Comma seperated list of names

Posted on 2006-06-19
6
240 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IIS Server infected with Ransomware - Postmortem investigation 12 133
System Center 2016 SQL Srv 1 24
grouping logic 6 46
SQL Query stumper 3 36
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

911 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

18 Experts available now in Live!

Get 1:1 Help Now