Simple but tricky String Concatenation issue

  • I have two columns Country and CompanyName. Table looks like this:

    Country              CompanyName

    -----------------------------------

    UK                    ABC Company

    UK                    XYZ Company

    UK                    SQL Company

    USA                   Microsoft

    USA                   Oracle

    Result Expected:

    ----------------

    UK                   ABC Company, XYZ Company, SQL Company

    USA                  Microsoft, Oracle

    How can I achieve this? I tried the following query:

    declare @sql varchar(8000)

    set @sql = ''

    select @sql = @sql + ', ' + companyname from northwind..customers where country = 'UK' group by country, companyname

    print @sql

    But when I do this, it errors out:

    declare @sql varchar(8000)

    set @sql = ''

    Select 'UK',

     (select @sql = @sql + ', ' + companyname from northwind..customers where country = 'UK' group by country, companyname)

    print @sql

    Give me a fish, you feed me for a day! Teach me to fish, you feed me for a life time.

  • Sorry, but what is your question?

     


    N 56°04'39.16"
    E 12°55'05.25"

  • Sorry, I fat fingered the question. I edited the post. Can you see the question now? Thanks.

    Give me a fish, you feed me for a day! Teach me to fish, you feed me for a life time.

  • Have a look at http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=338&messageid=391111

    Peter posts there a very nice and fast solution that uses XML PATH.

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • The other solution would be to write a custom aggregate using CLR code and then you could just select your aggregate function with a group by country. By doing this, you could then create a list of ANY column grouped by any other column.

    If the custom aggregate was called COMMALIST, the select would then sa

    select Country, CommaList(CompanyName) from <table> group by CompanyName

  • If you go for the CLR aggregate, the CLR aggregate may get its input in any order depending on how the query optimizer decides. Concatenation is not commutative Your results can be different between two executions of the query. The solution using xml path orders the items in the list, and it also seems to be the faster.

    Regards,

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • XML Path works great in SQL 2005. But right now, I need it in SQL 2000. Thanks.

    Give me a fish, you feed me for a day! Teach me to fish, you feed me for a life time.

  • The user defined function in the first post on http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=338&messageid=391111

    can do it on 2000.

    Andras


    Andras Belokosztolszki, MCPD, PhD
    GoldenGate Software

  • UDF works but very slow. So, I believe just got to live with it. Thanks everybody.

    Give me a fish, you feed me for a day! Teach me to fish, you feed me for a life time.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply