|
Subject:
|
Aggregate Concatenation
|
|
Posted By:
|
12th_Man
|
Post Date:
|
7/15/2003 8:29:37 AM
|
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 By:
|
Jeff Mason
|
Reply Date:
|
7/15/2003 9:22:47 AM
|
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 .
The TSQL 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:
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.:
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
SELECT @Sa=@Sa + language + ','
line with
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 By:
|
rahulpokharna
|
Reply Date:
|
1/8/2006 2:10:32 PM
|
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 By:
|
Jeff Mason
|
Reply Date:
|
1/10/2006 12:11:13 PM
|
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 By:
|
rahulpokharna
|
Reply Date:
|
1/10/2006 2:26:05 PM
|
Thanks Jeff, things are clear now.
regards rahul pokharna
|