Solved

SQL View to concatenate multiple lines into one

Posted on 2011-03-18
3
688 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
3 Comments
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 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:ewangoya
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

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 shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

815 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

11 Experts available now in Live!

Get 1:1 Help Now