Member of the LearnItFirst.com Video Training Network | LearnSqlServer.com | SQL SSIS Training | SQL Programming Tutorials |
LearnSqlServer.com Forums LearnSqlServer.com
Welcome Guest Search | New Posts | Members | Log In | Register

Comma separated list of values Options
fullyii
Posted: Friday, April 25, 2008 8:08:17 PM
Rank: Newbie

Joined: 4/25/2008
Posts: 1
Points: 3
Location: US
Hello I have a need to retun a comma seperated list of distinct values from the list below.

The query I have returns 2 rows.

ELPM_NO INV_NO BUDGET_TYPE
1273 12 LLD
1273 12 LOB

Code:

SELECT DISTINCT
a.ELPM_NO,
b.INV_NO,
c.BUDGET_TYPE

FROM tbl_SOWCO AS a
INNER JOIN tbl_INVOICE AS b ON b.ORD_NO = a.ORDER_NO
INNER JOIN tbl_FUNDING AS c ON c.ELPM_NO = a.ELPM_NO
WHERE (a.ELPM_NO = '1273') AND (b.INV_NO LIKE '12')


I would like the query to return 1 row like

Code:

ELPM_No        INV_NO       BUDGET_TYPES
1273              12               LLD,LOB




How can this be accomplished?

Scott Whigham
Posted: Tuesday, April 29, 2008 7:25:49 AM


Rank: Super Mod

Joined: 3/20/2006
Posts: 345
Points: 748
Location: Dallas, TX
There are many, many options. Here's just a dump of my "Create a CSV list from column.sql" script:
Code:
-- List of all Production.ProductSubcategories that a contact has ordered:
-- Choose contact that has ordered the most items
DECLARE @ContactId INT

SELECT @ContactId = ContactId
FROM Person.Contact
WHERE ContactId = (SELECT TOP(1) ContactId FROM Sales.SalesOrderHeader GROUP BY ContactId ORDER BY COUNT(*) DESC);

WITH ColumnToPivot ([data()]) AS (
        SELECT TOP 100 PERCENT ps.Name + N', ' AS ColumnYouWantToMakeACSVList_PlusAComma
        FROM Production.ProductSubcategory ps
        WHERE EXISTS (
            SELECT *
            FROM Production.Product p JOIN Sales.SalesOrderDetail od
                ON od.ProductId = p.ProductId
            JOIN Sales.SalesOrderHeader o
            ON o.SalesOrderId = od.SalesOrderId
            WHERE o.ContactId = @ContactId
                AND p.ProductSubcategoryID = ps.ProductSubcategoryID
            )
        ORDER BY ColumnYouWantToMakeACSVList_PlusAComma -- better to just order by ps.Name but, for example purposes, I include this
        FOR XML PATH(''), TYPE
    )
    , XmlRawData (CSVString) AS (
        SELECT (SELECT [data()] AS mydata FROM ColumnToPivot AS d FOR XML RAW, TYPE).value( '/row[1]/mydata[1]', 'NVARCHAR(max)') AS CSV_Column
    )
SELECT LEFT(CSVString, LEN(CSVString)-1) AS CSVList
FROM XmlRawData

-- Alternate approach:
DECLARE @Title VARCHAR(128)
SET @Title ='POP3 and IMAP4 in Exchange 2003';

DECLARE @ColumnList NVARCHAR(MAX) 
SET @ColumnList = ''
SELECT @ColumnList = @ColumnList + N', ' + CAST(vd.SubscriberId AS NVARCHAR)
    FROM Videos v JOIN dbo.VideosDownloadedBySubscriber vd
        ON v.VideoId = vd.VideoId
WHERE v.Title = @Title

SELECT NULLIF(SUBSTRING(@ColumnList, 3, 1024), '')

-- Another:
SELECT  STUFF((SELECT ', ' + name FROM sys.databases ORDER BY name FOR XML PATH('')), 1, 1, '')
These code samples come from all over the web and lots of authors. I think one of these is a mix that Adam Machanic, Tony Rogerson and I came up with, aanother comes from Itzik Ben-Gan, and I can't guess the rest. Between these examples should be a technique that makes sense to you that you can use.
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
     
Don't Forget!
LearnItFirst.com
Don't Forget!
LearnExchange.com
 
Home | About Us | Support | Contact Us | Privacy | Site Map | Blogs Blogs Refer a Friend and Get a Free Subscription!
© Copyright 2004-2007 LearnItFirst.com LLC. All rights reserved. All trademarks remain the property of their respective owners.
This site is not affiliated in any way with the Microsoft Corporation.