Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
String together multiple rows into one
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Scripting
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Web
Divers
Thread ID:
01520834
Message ID:
01520835
Vues:
69
This message has been marked as the solution to the initial question of the thread.
>Hello all,
>
>I have a table which lists work order reference numbers for a shipment reference number (both are int) as follows:
>
>
>ShpmID   WOID
>21508	14839
>21508	15095
>21508	15254
>
>
>I would like to create a query that would return one row for the shipment id with a concatenated string list of work order ids as follows:
>
>
>ShpmID   WORefNbrs
>21508	14839, 15095, 15254
>
>
>How can I create a query to do this?
>
>As always, TIA
>
>Bob

Try
SELECT S.ShpmID, stuff((select ', ' + convert(varchar(20),WOID) 
FROM Shipments S1 where S1.ShmID = S.ShpmID order by WOID
FOR XML PATH('')),1,2,'') as [WORefNbrs]
FROM Shipments S
GROUP BY S.ShpmID
See these blogs for explanations:
MSDN thread about concatenating rows

Making a list and checking it twice

Concatenating Rows - Part 1

Concatenating Rows - Part 2
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform