SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Concatenate multiple rows into one column?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

lwhalen618
Starting Member

4 Posts

Posted - 05/07/2008 :  21:07:08  Show Profile  Reply with Quote
Newbie question here. I have two tables that have a one to many relationship. I want to create a query that takes the info from the child table (possibly multiple rows) and concatenates it into a single column in the parent table. The tables are:

TableParent (ASSIGNNUM (PK), DESC, STARTDATE)
TableChild (ASSIGNNUM (FK), EMPLOYEENUM)

There could be multiple employees for each assignment. Sample data:

TableParent
1....First Assignment....05/01/2008
2....Second Assignment...05/03/2008
3....Third Assignment....05/07/2008

TableChild
1....55342
2....33456
2....52343
3....35225
3....45121
3....11553

I would like the query result to look like this:

1....First Assignment....05/01/2008....55342
2....Second Assignment...05/03/2008....33456,52343
3....Third Assignment....05/07/2008....35225,45121,11553

Any suggestions would be appreciated!

nathans
Aged Yak Warrior

USA
933 Posts

Posted - 05/08/2008 :  00:04:56  Show Profile  Reply with Quote
Here are a few examples:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=53293


Nathan Skerl
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 05/08/2008 :  04:08:51  Show Profile  Reply with Quote
SELECT p.ASSIGNNUM,p.DESC,p.STARTDATE,LEFT(el.EmpList,LEN(el.EmpList)-1)
FROM TableParent p
CROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()]
             FROM TableChild
             WHERE ASSIGNNUM =p.ASSIGNNUM 
             FOR XML PATH(''))el(EmpList)
Go to Top of Page

lwhalen618
Starting Member

4 Posts

Posted - 05/08/2008 :  22:59:36  Show Profile  Reply with Quote
Thanks, everyone. I'm working through your suggestions and will report back my solution.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 05/09/2008 :  04:46:05  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

lwhalen618
Starting Member

4 Posts

Posted - 05/12/2008 :  23:10:27  Show Profile  Reply with Quote
I used a ConcatOrderProducts function found in the following post to solve my problem. It worked perfectly!

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=53293

Thanks for your help everyone.

Lisa
Go to Top of Page

Gawaine79
Starting Member

6 Posts

Posted - 12/23/2009 :  15:56:31  Show Profile  Reply with Quote
Anyone looking for another simple way to do with
Works in sql 2005 and later I believe.


CREATE TABLE #TableParent (ASSIGNNUM int primary key, description varchar(512), STARTDATE datetime)
CREATE TABLE #TableChild (ASSIGNNUM int, EMPLOYEENUM int)

/*
There could be multiple employees for each assignment. Sample data:

*/
INSERT INTO #TableParent VALUES ( 1,'First Assignment', '05/01/2008')
INSERT INTO #TableParent VALUES ( 2,'Second Assignment','05/03/2008')
INSERT INTO #TableParent VALUES ( 3,'Third Assignment','05/07/2008')

INSERT INTO #TableChild VALUES (1,55342)
INSERT INTO #TableChild VALUES (2,33456)
INSERT INTO #TableChild VALUES (2,52343)
INSERT INTO #TableChild VALUES (3,35225)
INSERT INTO #TableChild VALUES (3,45121)
INSERT INTO #TableChild VALUES (3,11553)



select p.assignNum, p.description, p.startDate,
stuff((select ', ' + cast(employeeNum as varchar(512))from #TableChild c where c.ASSIGNNUM = p.ASSIGNNUM for xml path('')),1,2,'') as employeeList
from #TableParent p
;


Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22460 Posts

Posted - 12/24/2009 :  01:31:07  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by Gawaine79

Anyone looking for another simple way to do with
Works in sql 2005 and later I believe.


CREATE TABLE #TableParent (ASSIGNNUM int primary key, description varchar(512), STARTDATE datetime)
CREATE TABLE #TableChild (ASSIGNNUM int, EMPLOYEENUM int)

/*
There could be multiple employees for each assignment. Sample data:

*/
INSERT INTO #TableParent VALUES ( 1,'First Assignment', '05/01/2008')
INSERT INTO #TableParent VALUES ( 2,'Second Assignment','05/03/2008')
INSERT INTO #TableParent VALUES ( 3,'Third Assignment','05/07/2008')

INSERT INTO #TableChild VALUES (1,55342)
INSERT INTO #TableChild VALUES (2,33456)
INSERT INTO #TableChild VALUES (2,52343)
INSERT INTO #TableChild VALUES (3,35225)
INSERT INTO #TableChild VALUES (3,45121)
INSERT INTO #TableChild VALUES (3,11553)



select p.assignNum, p.description, p.startDate,
stuff((select ', ' + cast(employeeNum as varchar(512))from #TableChild c where c.ASSIGNNUM = p.ASSIGNNUM for xml path('')),1,2,'') as employeeList
from #TableParent p
;





This method is same as the one I posted. See that link

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

f_tonmoy
Starting Member

1 Posts

Posted - 01/26/2011 :  10:06:29  Show Profile  Reply with Quote
you can get help from my website
visit here http://www.bdlab.byethost32.com/index.php/sql-server/97-sql-query-for-multiple-row-to-single-column

http://www.bdlab.byethost32.com
Go to Top of Page

cknight44
Starting Member

USA
3 Posts

Posted - 01/25/2012 :  18:34:56  Show Profile  Reply with Quote
visakh16, this is awesome! Thanks. This a great technique.

quote:
Originally posted by visakh16

SELECT p.ASSIGNNUM,p.DESC,p.STARTDATE,LEFT(el.EmpList,LEN(el.EmpList)-1)
FROM TableParent p
CROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()]
             FROM TableChild
             WHERE ASSIGNNUM =p.ASSIGNNUM 
             FOR XML PATH(''))el(EmpList)


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 01/26/2012 :  09:04:30  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cris_0007
Starting Member

2 Posts

Posted - 05/07/2012 :  12:44:14  Show Profile  Reply with Quote
quote:
Originally posted by cknight44

visakh16, this is awesome! Thanks. This a great technique.

quote:
Originally posted by visakh16

SELECT p.ASSIGNNUM,p.DESC,p.STARTDATE,LEFT(el.EmpList,LEN(el.EmpList)-1)
FROM #TableParent p
CROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()]
             FROM #TableChild
             WHERE ASSIGNNUM =p.ASSIGNNUM 
             FOR XML PATH('')) el(EmpList)




I am getting the error

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value ',' to data type int.

when I run this


CREATE TABLE #TableParent (ASSIGNNUM int primary key, DESCC varchar(512), STARTDATE datetime)
CREATE TABLE #TableChild (ASSIGNNUM int, EMPLOYEENUM int)

/*
There could be multiple employees for each assignment. Sample data:

*/
INSERT INTO #TableParent VALUES ( 1,'First Assignment', '05/01/2008')
INSERT INTO #TableParent VALUES ( 2,'Second Assignment','05/03/2008')
INSERT INTO #TableParent VALUES ( 3,'Third Assignment','05/07/2008')

INSERT INTO #TableChild VALUES (1,55342)
INSERT INTO #TableChild VALUES (2,33456)
INSERT INTO #TableChild VALUES (2,52343)
INSERT INTO #TableChild VALUES (3,35225)
INSERT INTO #TableChild VALUES (3,45121)
INSERT INTO #TableChild VALUES (3,11553)

--select * from #TableParent
--select * from #TableChild

SELECT p.ASSIGNNUM,p.DESCC,p.STARTDATE,LEFT(el.EmpList,LEN(el.EmpList)-1)
FROM #TableParent p
CROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()]
FROM #TableChild
WHERE ASSIGNNUM =p.ASSIGNNUM
FOR XML PATH('')) as el(EmpList)






Edited by - cris_0007 on 05/07/2012 13:29:51
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47023 Posts

Posted - 05/07/2012 :  17:28:32  Show Profile  Reply with Quote

SELECT p.ASSIGNNUM,p.DESCC,p.STARTDATE,LEFT(el.EmpList,LEN(el.EmpList)-1)
FROM #TableParent p
CROSS APPLY (SELECT CAST(EMPLOYEENUM AS varchar(10)) + ',' AS [text()]
FROM #TableChild
WHERE ASSIGNNUM =p.ASSIGNNUM 
FOR XML PATH('')) as el(EmpList)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000