Solved

How to concatenate entries?

Posted on 2010-11-12
3
257 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 143

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

790 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