SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to concatenate row columns and joined in another table


how to concatenate row columns and joined in another table

Author
Message
ernesto.yandoc
ernesto.yandoc
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 1
Hi to all,

Please bear with me. anyways, this is the tables i have

Table1

Name Company OtherVisaID

Mike ABC 1

Joey CBA 2

Table2

OtherVisaID TypeID Remarks

1 1 test1

1 2 test2

2 3 test3

2 2 test4

2 1 test5

Table3

TypeID Description

1 US

2 UK

3 SG


Result should be like this..

OtherVisaID Name Company List of Visa

1 Mike ABC US;UK

2 Joey CBA US;UK;SG


so as you can see it was separated by ";" hope you understand my question..


thanks in advance.
imex
imex
SSC Veteran
SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)SSC Veteran (233 reputation)

Group: General Forum Members
Points: 233 Visits: 199
Hi,

Try:

SELECT
t1.OtherVisaID,
t1.Name,
t1.Company,
STUFF( (SELECT ';' + t3.Description
FROM Table2 as t2
JOIN Table3 as t3
ON t3.OtherVisaID = t2.OtherVisaID
WHERE
t2.OtherVisaID = t1.OtherVisaID
FOR XML PATH(''), TYPE).value('.', 'varchar(max)')
,1, 1, '') as ListOfVisa,
FROM Table1 as t1



Hope this help.

http://www.imoveisemexposicao.com.br
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)

Group: General Forum Members
Points: 37146 Visits: 14411
If you can use SQLCLR you could use GROUP_CONCAT for SQL Server and write your SQL like this:

SELECT  t1.OtherVisaID,
t1.Name,
t1.Company,
dbo.GROUP_CONCAT_D(t3.DESCRIPTION, ';') AS ListOfVisa
FROM Table1 AS t1
JOIN Table2 AS t2 ON t1.OtherVisaID = t2.OtherVisaID
JOIN Table3 AS t3 ON t3.OtherVisaID = t2.OtherVisaID
GROUP BY t1.OtherVisaID,
t1.Name,
t1.Company;



__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search