?
Solved

SQL View to concatenate multiple lines into one

Posted on 2011-03-18
3
Medium Priority
?
697 Views
Last Modified: 2012-06-27
I'm trying to create a view using SQL Server Management Studio 2008 R2.  I have sales records in a table which have assigned tracking numbers.  There may be up to 5 tracking numbers for each order, so a portion of the table looks like this:

SOPNUMBE                    SOPTYPE      Tracking_Number      
INV-500001                 3      15461d3f2s489fs4                               
INV-500001                 3      sdf5s4df89s4d3f2                               
INV-500002                 3      068077960260939                                
INV-500003                 3      1ZR45F660354550566                             
INV-500004                 3      068077960260816                                

As you can see, INV-500001 is in the table twice because there are 2 tracking numbers.  In the view, I want that record to appear like this:

SOPNUMBE                    SOPTYPE      Tracking_Number      
INV-500001                 3      15461d3f2s489fs4, sdf5s4df89s4d3f2    

with the tracking numbers concatenated into one field.  I tried using a self join, but that concatenated the numbers twice, switching the order of the tracking numbers in the concatenation, and resulted in four lines, rather than one.

I could do this with a cursor, but obviously that is out of the question for a view.  Any ideas on how I might be able to accomplish this?

T
0
Comment
Question by:thoecherl
[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
3 Comments
 
LVL 58

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 35166948
try this:

select distinct sopnumbe, soptype, (
SELECT Tracking_Number + ', ' FROM sop s2 WHERE s1.SOPNUMBE=s2.SOPNUMBE FOR XML Path('')
) Tracking_Numbers
from sop s1
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35167035

SELECT SOPNUMBE,                  SOPTYPE,
        STUFF(
            (
            SELECT ', ' + Tracking_Number
            FROM Table1 B
            WHERE SOPNUMBE = A.SOPNUMBE
            FOR XML PATH('')
            ), 1, 1, ''
        ) As  Tracking_Number
from Table1 A
group by SOPNUMBE, SOPTYPE
0
 

Author Closing Comment

by:thoecherl
ID: 35167088
Brilliant.  Thank you.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

762 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