Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 15th, 2003, 08:29 AM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default Aggregate Concatenation

I would really love to know how I could group several rows together and display a comma delimited list of values from one column in a cell.

For example lets say there is a table books with columns title, author, and language. For a given title there could be multiple entries representing all the translations of that book. Here's some sample data:

title author language
--------------------------------------
The Illiad Homer Greek
The Illiad Homer English
The Illiad Homer French
Moby Dick Melville English
Moby Dick Melville Spanish
Tom Sawyer Twain English

Then I would like a query to return:

title languages
------------------------
The Illiad Greek, English, French
Moby Dick English, Spanish
Tom Sawyer English

It seems like there should be a function analogous to SUM that concatenates strings, but as far as I know none exists. Do any of you know a way to achieve this? I've been wondering about this for a while.

Thanks,
Mike
Reply With Quote
  #2 (permalink)  
Old July 15th, 2003, 09:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

There isn't a good way to do this, I'm afraid. This is really a presentation issue, and is more efficiently handled in client code. You could write a solution using a cursor, but since I abhor cursors, that'll be left as an exercise to the reader .

That having been said, there is sort of a way. I believe this is officially considered undocumented behavior in SQL Server, so you'll have to eat this message after you read it :D.

The TSQL code:
Code:
DECLARE @sa varchar(1000)
SELECT @Sa=@Sa + language + ',' 
    FROM books
    WHERE title='The Illiad';
will return the string 'Greek, English, French,'. Or maybe the values in some other order, depending on the phases of the moon and the whims of the optimizer. And that's the reason its undocumented: there is no good way to influence the order of the rows returned while a query is being processed.

Note also that the above technique will only do one 'title' at a time.

In SQL Server 2000 you can define a user-defined function to return the aggregate. If you do this, you can invoke the function for each title:
Code:
CREATE FUNCTION CSVLanguage (@Title as varchar(50))
RETURNS varchar(1000)
AS
BEGIN
DECLARE @Sa varchar(1000)
SELECT @Sa=@Sa + language + ','
    FROM books
    WHERE title=@Title
RETURN (@Sa)
END
Then invoke the user defined function in your query, as e.g.:
Code:
SELECT title, CSVLanguage(title)
    FROM Books
    GROUP BY Title;
and this will sort of work, with the same caveat about the 'language' order and the additional warning that there is an inherent limit to the number of languages which can be concatenated together.

If you don't like the trailing comma, I think replacing the
Code:
SELECT @Sa=@Sa + language + ','
line with
Code:
SELECT @Sa=COALESCE(@sa + ',','') + language
will "work". (I put "work" in quotations because I think it is arguable that this trick "works" at all, given that its output is undeterministic (nondeterministic? - whatever)).

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #3 (permalink)  
Old January 8th, 2006, 03:10 PM
Authorized User
 
Join Date: Jan 2006
Location: bangalore, karnataka, India.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have one small doubt regarding the above, will coalesce work for concatenaing string which will be of lenght 2000 chars



regards
rahul pokharna
Reply With Quote
  #4 (permalink)  
Old January 10th, 2006, 01:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by rahulpokharna
 I have one small doubt regarding the above, will coalesce work for concatenaing string which will be of lenght 2000 chars
COALESCE is a function which returns the value of one of the expressions which are it's arguments. Whether or not the results of a COALESCE (or what you do with it) exceed your database's limits depends on the arguments, not on COALESCE itself.

Strings (varchar actually) in SQL Server can be up to 8000 characters in length. YMMV using other databases, but they all have some limit. Thus, the code above will fail when the concatenation operation, and not COALESCE, exceeds your database limit for varchar expressions.



Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
Reply With Quote
  #5 (permalink)  
Old January 10th, 2006, 03:26 PM
Authorized User
 
Join Date: Jan 2006
Location: bangalore, karnataka, India.
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Jeff, things are clear now.


regards
rahul pokharna
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenation question, please savoym VBScript 12 May 5th, 2009 02:30 PM
Server.mappath and concatenation MArk_dB Classic ASP Basics 5 March 6th, 2007 03:48 PM
Concatenation of fields JpJoe Access 8 April 8th, 2005 01:32 AM
string concatenation nulogix PHP How-To 1 June 24th, 2004 06:17 AM
string concatenation matt_99 Access VBA 2 January 17th, 2004 09:55 AM



All times are GMT -4. The time now is 10:23 AM.


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