[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL View to concatenate multiple lines into one

Posted on 2011-03-18
3
Medium Priority
?
706 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 61

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

873 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