Solved

SQL View to concatenate multiple lines into one

Posted on 2011-03-18
3
695 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 55

Accepted Solution

by:
Huseyin KAHRAMAN 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: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

Independent Software Vendors: 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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

724 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