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.
Author |
Topic |
lwhalen618
Starting Member
4 Posts |
Posted - 2008-05-07 : 21:07:08
|
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:TableParent1....First Assignment....05/01/20082....Second Assignment...05/03/20083....Third Assignment....05/07/2008TableChild1....553422....334562....523433....352253....451213....11553I would like the query result to look like this:1....First Assignment....05/01/2008....553422....Second Assignment...05/03/2008....33456,523433....Third Assignment....05/07/2008....35225,45121,11553Any suggestions would be appreciated! |
|
nathans
Aged Yak Warrior
938 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-08 : 04:08:51
|
[code]SELECT p.ASSIGNNUM,p.DESC,p.STARTDATE,LEFT(el.EmpList,LEN(el.EmpList)-1)FROM TableParent pCROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()] FROM TableChild WHERE ASSIGNNUM =p.ASSIGNNUM FOR XML PATH(''))el(EmpList)[/code] |
|
|
lwhalen618
Starting Member
4 Posts |
Posted - 2008-05-08 : 22:59:36
|
Thanks, everyone. I'm working through your suggestions and will report back my solution. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-05-09 : 04:46:05
|
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254MadhivananFailing to plan is Planning to fail |
|
|
lwhalen618
Starting Member
4 Posts |
|
Gawaine79
Starting Member
6 Posts |
Posted - 2009-12-23 : 15:56:31
|
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 employeeListfrom #TableParent p; |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-12-24 : 01:31:07
|
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 employeeListfrom #TableParent p;
This method is same as the one I posted. See that linkMadhivananFailing to plan is Planning to fail |
|
|
f_tonmoy
Starting Member
1 Post |
|
cknight44
Starting Member
3 Posts |
Posted - 2012-01-25 : 18:34:56
|
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 pCROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()] FROM TableChild WHERE ASSIGNNUM =p.ASSIGNNUM FOR XML PATH(''))el(EmpList)
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-26 : 09:04:30
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
cris_0007
Starting Member
3 Posts |
Posted - 2012-05-07 : 12:44:14
|
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 pCROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()] FROM #TableChild WHERE ASSIGNNUM =p.ASSIGNNUM FOR XML PATH('')) el(EmpList)
I am getting the errorMsg 245, Level 16, State 1, Line 1Conversion failed when converting the varchar value ',' to data type int.when I run thisCREATE 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 pCROSS APPLY (SELECT EMPLOYEENUM + ',' AS [text()] FROM #TableChild WHERE ASSIGNNUM =p.ASSIGNNUM FOR XML PATH('')) as el(EmpList)
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-07 : 17:28:32
|
[code]SELECT p.ASSIGNNUM,p.DESCC,p.STARTDATE,LEFT(el.EmpList,LEN(el.EmpList)-1)FROM #TableParent pCROSS APPLY (SELECT CAST(EMPLOYEENUM AS varchar(10)) + ',' AS [text()]FROM #TableChildWHERE ASSIGNNUM =p.ASSIGNNUM FOR XML PATH('')) as el(EmpList)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|