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 |
gerbenhk
Starting Member
2 Posts |
Posted - 2010-10-02 : 09:18:12
|
Hi,I would like to combine multiple rows into one cell. I have the following table:Incidents-accountid-subjectThe table can containt multiple rows with the same account id like this:11111 This is a test11111 This is another testNow I would like to combine the subjects and put this in a cell in another table:Combined- Accountid- Combined Subjectsthen the row must be:11111 This is a test;This is another testCan anyone help me out? |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-10-02 : 09:56:56
|
Try this - ;WITH C AS (SELECT accountid, Subject FROM Incidents)SELECT Distinct accountid, STUFF((SELECT ';' + subject FROM Incidents WHERE accountid = c.accountid FOR XML PATH('')),1,1,'') FROM C Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
gerbenhk
Starting Member
2 Posts |
Posted - 2010-10-02 : 13:36:04
|
Hi,Based on your example, this is what I have made, and it works;WITH C AS (SELECT [Ask_Roger_B_V__MSCRM].dbo.IncidentBase.accountid, [Ask_Roger_B_V__MSCRM].dbo.IncidentBase.title FROM [Ask_Roger_B_V__MSCRM].dbo.IncidentBase)SELECT Distinct accountid, STUFF((SELECT + CHAR(13) + CHAR(10) + title FROM [Ask_Roger_B_V__MSCRM].dbo.IncidentBase WHERE accountid = c.accountid FOR XML PATH('')),1,6,'') FROM CNow I only want to run this query as a job, which update the two columns AccountID and Title in the table [Ask_Roger_B_V__MSCRM].dbo.AllCan_Incidents Can you help we wich command I have to add to this querythanks |
 |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-10-04 : 03:03:32
|
Might be this - UPDATE [Ask_Roger_B_V__MSCRM].dbo.AllCan_Incidents SET title = A.titleFROM [Ask_Roger_B_V__MSCRM].dbo.AllCan_Incidents UINNER JOIN (SELECT Distinct accountid, STUFF((SELECT + CHAR(13) + CHAR(10) + title FROM [Ask_Roger_B_V__MSCRM].dbo.IncidentBase WHERE accountid = c.accountid FOR XML PATH('')),1,6,'') AS titleFROM C) A ON U.accountid = A.accountid Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
|
|
|
|