Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2008, 06:46 PM
Registered User
 
Join Date: Jun 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Column to CSV

I ran into a query that seemed quite simple, but for some reason I cannot figure it out. It is essentially de-relationalizing a database column into a comma separated list, but it isn't as easy as just using coalesce.

There are three tables: one with customers, one with customer types, and a mapping table (1 customer to N types).


CUSTOMERS
------------------------
CUSTOMERID (varchar, PK)
NAME (varchar)


CUSTOMERS_TYPES_MAP
---------------------
CUSTOMERID (varchar)
CUSTOMERTYPE (varchar)


CUSTOMERTYPES
---------------------
CUSTOMERTYPE (varchar)



What I'm working toward is a query that retrieves a comma-separated list of CUSTOMERTYPES per CUSTOMERID. So the output would be something like this:


CUSTOMER CUSTOMERTYPES
--------------------------------------------------------
Customer A Vendor, Manufacturer
Customer B Vendor
Customer C Vendor, Manufacturer, Retailer


I was able to do this by creating a function (see below) that accepts a customer ID and returns a single comma separated list... However, I cannot use a function (long story... dumb customer). Any way to do this in a single statement without the use of a function?


CREATE FUNCTION getCustomerTypes(@CustomerId varchar(16))
RETURNS varchar(1024)
AS
BEGIN
      DECLARE @CustomerTypeList varchar(1024)
      SELECT @CustomerTypeList = COALESCE(@CustomerTypeList + ', ', '') +
                 CAST (CTM.CUSTOMERTYPE AS varchar(32))
      FROM CUSTOMERS_TYPES_MAP CTM
      WHERE CTM.CUSTOMERID = @CustomerId

      RETURN @CustomerTypeList
END


SELECT C.CUSTOMERID,
       C.NAME,
       dbo.getCustomerTypes(C.ID) CUSTOMERTYPES
FROM CUSTOMERS C
 
Old June 4th, 2008, 08:02 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Understood about the customer... everyone thinks conatenation functions are bad... have them take a look at the following article...

http://www.sqlservercentral.com/arti...st+Data/61572/

This will solve the problem for you, though... It's XML so it'll be just a touch slower...

Code:
DECLARE @tbl1 TABLE( ID INT, Descr VARCHAR(25))
DECLARE @tbl2 TABLE( ID INT, Descr VARCHAR(25))

 --==== Create a couple of test tables
 INSERT INTO @tbl1
 SELECT 1, 'test1' UNION ALL
 SELECT 2, 'test2' UNION ALL
 SELECT 3, 'test3'

INSERT INTO @tbl2
 SELECT 1, 'test1 additional'  UNION ALL
 SELECT 1, 'test1 additional2' UNION ALL
 SELECT 2, 'test2 additional'  

--===== Concatenatate the data without a function
 SELECT t1.ID , 
        STUFF((SELECT ',' + t2.Descr FROM @tbl2 t2 WHERE t1.id = t2.id FOR XML PATH('')),1,1,'')
   FROM @tbl2 t1
  GROUP BY t1.ID
--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing CSV inspiron SQL Server DTS 1 October 24th, 2006 03:17 PM
previous column & next column ct Excel VBA 4 August 19th, 2005 04:50 AM
template column and bound column hidayah ASP.NET 1.x and 2.0 Application Design 1 April 9th, 2005 03:50 PM
Export csv grahampinkney Access 3 February 21st, 2004 07:35 AM
Compare two Items of data(in column A and column B ever Excel VBA 6 February 13th, 2004 02:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.