Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql 2012 concatenate rows into matched rows

Posted on 2014-11-10
6
Medium Priority
?
114 Views
Last Modified: 2014-11-12
I have a table called notes with three columns: NOTE_ID,NOTE_LINE,NOTE_DETL

The data looks like this:
NOTE_ID  NOTE_LINE  NOTE_DETL
1    1    1LOREM IPSUM
1    2    2LOREM IPSUM
2    1    1LOREM IPSUM
2    2    2LOREM IPSUM
2    3    3LOREM IPSUM
2    4    4LOREM IPSUM
3    1    1LOREM IPSUM
3    2    2LOREM IPSUM
3    3    3LOREM IPSUM

I need to arrange it like:

NOTE_ID COMPLETE_NOTE
1 2LOREM IPSUM
2 2LOREM IPSUM 3LOREM IPSUM 4LOREM IPSUM
3 2LOREM IPSUM 3LOREM IPSUM

Take each unique NOTE_ID and combine all NOTE_DETL'S ordered by NOTE_LINE while leaving out the NOTE_LINE first note (1) of each NOTE_ID.

Any help would be appreciated.
0
Comment
Question by:PeelSeel2
6 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40433348
Your logic is not clear. Please explain or give a better example.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 2000 total points
ID: 40433410
Untested, but try -
select distinct b.note_id,
stuff((select ' ' + note_detl from notes n2 where n1.note_id = n2.note_id
       for xml path('')), 1, 1, '') as note_details
from notes n1
where note_line > 1;
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40433513
(no points..)

I think you also need to move the note_line filter into the subquery. Otherwise, it may still include the first note in the final note_details value.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:PeelSeel2
ID: 40433706
Awking00,

select distinct note_id,
stuff((select ' ' + note_detl from notes as n2 where n1.note_id = n2.note_id for xml path('')), 1, 1, '') as note_details
from notes as n1
where note_line > 1;

This worked Great.  One Question.  I guess my notes_detl field have large amount of spaces at the end.  How would I trim them in the context of that query?
0
 

Author Closing Comment

by:PeelSeel2
ID: 40433767
Thank You!
0
 
LVL 32

Expert Comment

by:awking00
ID: 40437368
stuff((select ' ' + rtrim(note_detl) from notes ...
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

876 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