Solved

How to concatenate entries?

Posted on 2010-11-12
3
255 Views
Last Modified: 2012-05-10
I have 2 tables
1. tblENQUIRY and
2. tblENQUIRY_FOLLOW_UP

The history contains the PK ENQUIRY_ID from the  tblENQUIRY

 tblENQUIRY tblENQUIRY_FOLLOW_UP SAMPLE DATA IN FOLLOW UP
What I need to do is create a new table with the 2 columns
ENQUIRY_ID and ENQ_FU_COMMENT

for each ENQUIRY_ID concatenate all of the individual lines in just one line.

like this ...
 what it should look like
I really appreciate help on this.

My solution does not work correctly.

best regards
0
Comment
Question by:STOCRIC
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34120133
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 34120201
Hi,

Check this.
Select 
	[Enquiry_ID],
	(Stuff((Select ', ' + ENQ_FU_COMMENT From tblEnquiry_Follow_Up E2 Where E1.Enquiry_ID = E2.Enquiry_ID FOR XML PATH('')),1,2,'')) as ENQ_FU_COMMENT
From tblEnquiry E1
Order By Enquiry_ID

Open in new window

0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 34120206
For Sample
Declare @Aliases Table
(
	[FirstName] varchar(50) not null,
	[LastName] varchar(50) not null,
	[Alias] varchar(100) not null
)

Insert @Aliases
Select 'Clark','Kent','Superman'
Union All
Select 'Clark','Kent','Kal-El'
Union All
Select 'Clark','Kent','Gangbuster'
Union All
Select 'Clark','Kent','Supernova'
Union All
Select 'Clark','Kent','Nightwing'
Union All
Select 'Peter','Parker','Spiderman'
Union all
Select 'Peter','Parker','WebSligner'

Select * from @Aliases

Select Distinct
	[LastName],
	[FirstName],
	(Stuff((Select ', ' + Alias From @Aliases T2 Where T2.FirstName = T1.FirstName and T2.LastName = T2.LastName FOR XML PATH('')),1,2,'')) as Aliases
From @Aliases T1
Order By [LastName], [FirstName]

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now