Search code examples
group-bynvarcharsybase-asa

Build a list of grouped values


I'm new to this page and this is the first time i post a question. Sorry for anything wrong. The question may be old, but i just can't find any answer for SQL AnyWhere.
I have a table like

Order |  Mark  
======|========   
1     | AA  
2     | BB  
1     | CC  
2     | DD  
1     | EE  

I want to have result as following

Order | Mark  
1     | AA,CC,EE  
2     | BB,DD  

My current SQL is

Select Order, Cast(Mark as NVARCHAR(20))  
From #Order  
Group by Order  

and it just give me with result completely the same with the original table.

Any idea for this?


Solution

  • You can use the ASA LIST() aggregate function (untested, you might need to enclose the order column name into quotes as it is also a reserved name):

    SELECT Order, LIST( Mark )
    FROM #Order
    GROUP BY Order;
    

    You can customize the separator character and order if you need.

    Note: it is rather a bad idea to

    • name your table and column name with like regular SQL clause (Order by)
    • use the same name for column an table (Order)