Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
|
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 January 31st, 2007, 08:17 AM
Authorized User
 
Join Date: Nov 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default show columns with same ID in the same row

Hi,

I have a table with 2 columns :
ID Product
----- ----------
1 a
2 b
3 c
1 h

how can I create a query that will return :

ID Product
----- ----------
1 a , h
2 b
3 c

meaning - will "group" the "product" column values by the ID column.

Thank you,

Yuval

Yuval Kronenfeld
Israel
__________________
Yuval Kronenfeld
Israel
 
Old January 31st, 2007, 12:49 PM
Registered User
 
Join Date: Jan 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yuval: How many ID's are you dealing with?

Daniel
Scottsdale, AZ

 
Old February 1st, 2007, 11:18 AM
Authorized User
 
Join Date: Nov 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hI Daniel,

up to 1000 or so...

just to note - the ID Field is not a unique field - I just wanted to make it a short example.

Thanks,
yuval

Yuval Kronenfeld
Israel
 
Old February 2nd, 2007, 12:19 PM
Registered User
 
Join Date: Jan 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Yuval,

 That's a lot. If it was a small set I was going to recommend you nest a sub query with aggregated case statements. The only solution I can come up with is that you create a scalar CLR function, pass ID to it, loop through all the values of PRODUCT and return STRING. That or a extended stored PROC that you can nest into a query that groups by ID.

Regards,

 
Old February 2nd, 2007, 07:46 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Yuvalk,

Do you know how to create the function everyone is talking about?

It would look something like this
Code:
CREATE FUNCTION dbo.fnConcatProduct
        (
        @pID INT
        )
RETURNS VARCHAR(8000)
     AS
  BEGIN
        --===== Declare a return variable to hold concatenated CSV info
        DECLARE @rCSVData VARCHAR(8000)

        --===== Concatenate the data based on the inputs
         SELECT @rCSVData = ISNULL(@rCSVData+',','')+Product
           FROM YourTable
          WHERE ID = @pID

        --===== Return the concatenated CSV info
         RETURN @rCSVData
    END
    and the query would look something like this...

 
Code:
SELECT DISTINCT
Code:
        ID,dbo.fnConcatProduct(ID) AS ProductCSV
   FROM YourTable


...of course, you would need to change the table name and perhaps the column names to match your table.
--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
html table row id myself.panku ASP.NET 1.x and 2.0 Application Design 0 April 17th, 2008 01:56 PM
Combo Box Show all bound columns Coby Excel VBA 2 October 22nd, 2007 02:24 PM
sql - concating columns for rows with same ID yuvalk SQL Server 2000 6 September 26th, 2006 10:47 AM
Creating a Unique ID for each row maxworlund SQL Server DTS 1 March 8th, 2005 09:33 PM
select id from row in array mikeuk PHP Databases 2 July 19th, 2004 11:00 AM





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