473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

1-multiple to string

Hello all,

I'm using SS 2000 and NT4.

Say I've got three tables: T1, T2 and T3. T3 contains the 1-to-multiple data
of the relation between T1 and T2:

------------------------------------------------
CREATE TABLE #T1(
T1PK INTEGER NOT NULL PRIMARY KEY,
Name VARCHAR(3) NOT NULL
)

CREATE TABLE #T2(
T2PK CHAR NOT NULL PRIMARY KEY,
Qty INTEGER NOT NULL
)

CREATE TABLE #T3(
T1PK INTEGER NOT NULL,
T2PK CHAR NOT NULL,
)

ALTER TABLE #T3 ADD CONSTRAINT cPK PRIMARY KEY (T1PK, T2PK)
ALTER TABLE #T3 ADD CONSTRAINT T1FK FOREIGN KEY REFERENCES #T1.T1PK
ALTER TABLE #T3 ADD CONSTRAINT T2FK FOREIGN KEY REFERENCES #T2.T2PK

INSERT INTO #T1 (T1PK, Name)
SELECT 1, 'Bob' UNION
SELECT 2, 'Joe' UNION
SELECT 3, 'Bla'

INSERT INTO #T2 (T2PK, Qty)
SELECT 'A', 3 UNION
SELECT 'B', 2 UNION
SELECT 'C', 1

INSERT INTO #T3 (T1PK, T2PK)
SELECT '1', 'A' UNION
SELECT '1', 'B' UNION
SELECT '2', 'A' UNION
SELECT '2', 'B' UNION
SELECT '2', 'C' UNION
SELECT '3', 'B'

------------------------------------------------

What I want is to convert the relation's multiple side to a string. Instead
of:

SELECT T1.Name, T2.T2PK, T2.Qty
FROM #T3 T3
INNER JOIN #T2 T2 ON T2.T2PK = T3.T2PK
INNER JOIN #T1 T1 ON T1.T1PK = T3.T1PK

Name T2PK Qty
---- ---- -----------
Bob A 3
Bob B 2
Joe A 3
Joe B 2
Joe C 1
Bla B 2

I would like to get:

Name Info
---- -----------
Bob "A-3;B-2"
Joe "A-3;B-2;C-1"
Bla "B-2"

Is it possible to do that? How?

Thanks for your time.

Yannick
Jul 20 '05 #1
4 1490
Yannick Turgeon (no****@nowhere.com) writes:
What I want is to convert the relation's multiple side to a string.
Instead of:

SELECT T1.Name, T2.T2PK, T2.Qty
FROM #T3 T3
INNER JOIN #T2 T2 ON T2.T2PK = T3.T2PK
INNER JOIN #T1 T1 ON T1.T1PK = T3.T1PK

Name T2PK Qty
---- ---- -----------
Bob A 3
Bob B 2
Joe A 3
Joe B 2
Joe C 1
Bla B 2

I would like to get:

Name Info
---- -----------
Bob "A-3;B-2"
Joe "A-3;B-2;C-1"
Bla "B-2"

Is it possible to do that? How?


In a single statement, no. You could write a cursor for the task to
accumulate the data in a temp table. You would then find that you would
have to define a maximum length for that Info column. This gives hint
why you cannot do this in a query.

You might be better off doing this in client code.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Erland,

So I cannot do that. That's what I thought but I was hopping for ... humm...
a miracle! :o) I just created a SP using a cursor and a temp table as you
suggested. It's not that fast but it does the job. Thanks for your help.

Yannick
"Erland Sommarskog" <so****@algonet.se> wrote in message
news:Xn*********************@127.0.0.1...
Yannick Turgeon (no****@nowhere.com) writes:
What I want is to convert the relation's multiple side to a string.
Instead of:

SELECT T1.Name, T2.T2PK, T2.Qty
FROM #T3 T3
INNER JOIN #T2 T2 ON T2.T2PK = T3.T2PK
INNER JOIN #T1 T1 ON T1.T1PK = T3.T1PK

Name T2PK Qty
---- ---- -----------
Bob A 3
Bob B 2
Joe A 3
Joe B 2
Joe C 1
Bla B 2

I would like to get:

Name Info
---- -----------
Bob "A-3;B-2"
Joe "A-3;B-2;C-1"
Bla "B-2"

Is it possible to do that? How?


In a single statement, no. You could write a cursor for the task to
accumulate the data in a temp table. You would then find that you would
have to define a maximum length for that Info column. This gives hint
why you cannot do this in a query.

You might be better off doing this in client code.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Jul 20 '05 #3
Check out method #4 in this post:
http://groups.google.com/groups?selm...TNGP09.phx.gbl

I asked the same question in another thread ( called 1:m database relation
"Flattened" for reporting purposes) and was given the above link. Method #4
uses a table-value function and does not require a cursor. You don't have
to write out to a temp table, and you can join against the result. I think
the max length of the output string is 8K, but that is a LOT of concatenated
stuff. I don't know how this function will work with a lot of data, but it
seems pretty neat.

Thanks,

Bill Mac
"Yannick Turgeon" <no****@nowhere.com> wrote in message
news:11*******************@news20.bellglobal.com.. .
Erland,

So I cannot do that. That's what I thought but I was hopping for ... humm... a miracle! :o) I just created a SP using a cursor and a temp table as you
suggested. It's not that fast but it does the job. Thanks for your help.

Yannick
"Erland Sommarskog" <so****@algonet.se> wrote in message
news:Xn*********************@127.0.0.1...
Yannick Turgeon (no****@nowhere.com) writes:
What I want is to convert the relation's multiple side to a string.
Instead of:

SELECT T1.Name, T2.T2PK, T2.Qty
FROM #T3 T3
INNER JOIN #T2 T2 ON T2.T2PK = T3.T2PK
INNER JOIN #T1 T1 ON T1.T1PK = T3.T1PK

Name T2PK Qty
---- ---- -----------
Bob A 3
Bob B 2
Joe A 3
Joe B 2
Joe C 1
Bla B 2

I would like to get:

Name Info
---- -----------
Bob "A-3;B-2"
Joe "A-3;B-2;C-1"
Bla "B-2"

Is it possible to do that? How?


In a single statement, no. You could write a cursor for the task to
accumulate the data in a temp table. You would then find that you would
have to define a maximum length for that Info column. This gives hint
why you cannot do this in a query.

You might be better off doing this in client code.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp


Jul 20 '05 #4
Bill MacLean (bs**********@att.net) writes:
I asked the same question in another thread ( called 1:m database
relation "Flattened" for reporting purposes) and was given the above
link. Method #4 uses a table-value function and does not require a
cursor.


But it does use iteration.

It is of course a neat way to package the iteration, however.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
4
by: blrmaani | last post by:
Here is what I want: string s1 = "This is a list of string"; list<string> s2 = s1.some_method(); Now, I should be able to traverse list s2 and get each member ( which is of type 'string' ). ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.