Solved

sql 2012 concatenate rows into matched rows

Posted on 2014-11-10
6
109 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing SCCM 2016 4 51
Creating a View from a CTE 15 47
Datatable / Dates ? 4 31
SQL - Using an 'array' kind of variable 5 15
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

739 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