SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to architect a sublist with XML path?

Execute the following example Microsoft SQL Server T-SQL scripts in Management Studio Query Editor to demonstrate how to create a comma-delimited (csv) list within an SQL query.

The FOR XML PATH clause creates the comma-limited sublist for each territory.

-- SQL xml path comma delimited list- SQL correlated subquery - sql comma delimited list

-- SQL create comma-limited list - sql comma separated list - mssql csv list

Select Territory=st.[Name],

SalesYTD = '$'+convert(varchar,floor(SalesYTD),1), -- SQL currency format

SalesStaffAssignmentHistory = Stuff((

      Select ', ' + c.FirstName+' '+c.LastName as [text()]

      From Person.Contact c

      Join Sales.SalesTerritoryHistory sth

            On c.ContactID = sth.SalesPersonID

      Where sth.TerritoryID = st.TerritoryID

      Order by StartDate

      For XML Path ('')), 1, 1, '')

From AdventureWorks.Sales.SalesTerritory st

Order by SalesYTD desc ;

Go

/*  Results

 

Territory   SalesYTD          SalesStaffAssignmentHistory

Northwest   $9,178,765.00     Shannon Elliott, Terry Eminhizer, Martha Espinoza

Southwest   $8,351,296.00     Shelley Dyck, Jauna Elson

Canada      $6,917,270.00     Carla Eldridge, Michael Emanuel, Gail Erickson

Central     $4,677,108.00     Linda Ecoffey, Maciej Dusza

France      $3,899,045.00     Mark Erickson

Northeast   $3,857,163.00     Maciej Dusza, Linda Ecoffey

United Kingdom    $3,514,865.00     Michael Emanuel

Southeast   $2,851,419.00     Carol Elliott

Germany     $2,481,039.00     Janeth Esteves

Australia   $1,977,474.00     Twanna Evans

*/

------------

 

/* The following query produces identical results applying GROUP BY */

-- SQL group by csv list create - SQL min aggregate function

-- SQL stuff - SQL text() function

Select Territory=st.[Name],

SalesYTD = '$'+convert(varchar,floor(MIN(SalesYTD)),1),

SalesStaffAssignmentHistory = Stuff((

      Select ', ' + c.FirstName+' '+c.LastName as [text()]

      From Person.Contact c

      Join Sales.SalesTerritoryHistory sth

            On c.ContactID = sth.SalesPersonID

      Where sth.TerritoryID = st.TerritoryID

      Order by StartDate

      For XML Path ('')), 1, 1, '')

From AdventureWorks.Sales.SalesTerritory st

GROUP BY st.[Name], TerritoryID

Order by SalesYTD desc ;

Go
------------

-- Generating T-SQL comma separated list using a local variable

-- Requires multi-statements - cannot be imbedded in another query

USE AdventureWorks2008;

DECLARE @CommaLimitedList VARCHAR(MAX) = ''

SELECT @CommaLimitedList = Name + ', ' + @CommaLimitedList

FROM Production.Product

WHERE ProductID BETWEEN 800 and 810

SELECT @CommaLimitedList

GO

/*

HL Mountain Handlebars, ML Mountain Handlebars, LL Mountain Handlebars, HL Headset,

ML Headset, LL Headset, HL Fork, ML Fork, LL Fork, Road-550-W Yellow, 48,

Road-550-W Yellow, 44,

*/

------------

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.