Friday, August 29, 2014
   
Text Size
Login

MySQL Group_concat equivalent in T-SQL and Oracle

MySQL is just the best
Unfortunately I make a living using Microsoft and Oracle products. I shouldn't say unfortunately as I don't see myself doing any other job and it beats daytime television any day.

I use this quite a lot so I thought I'd put an article here somewhere. Based on the following concept:

  1.  RowID     column_to_return_as_string    
  2.  --------- -------------------------- 
  3.  1         Me 
  4.  2         Myself 
  5.  3         I 
  6.   
  7.  -- to be returned as 
  8.  RowID     my_field_name    
  9.  --------- -------------------------- 
  10.  1         Me,Myself,I 

Note that the following queries include the nested version because I find myself needing to group concatenate more often from another table then using data from the same table.

MySQL

  1.  SELECT 
  2.       ( 
  3.       SELECT  
  4.            GROUP_CONCAT(column_to_return_as_string)  
  5.       FROM  
  6.            a_pretend_table 
  7.       WHERE 
  8.            a_condition=true 
  9.       ) AS my_field_name 
  10.  FROM 
  11.       another_pretend_table 

T-SQL

  1.  SELECT 
  2.       STUFF( 
  3.         (  
  4.        SELECT  
  5.            ',' + column_to_return_as_string  
  6.        FROM  
  7.            a_pretend_table  
  8.        WHERE 
  9.            a_condition=true 
  10.        FOR XML PATH ('') 
  11.         ),1,1,'' 
  12.       ) AS my_field_name 
  13.  FROM 
  14.       another_pretend_table 

Oracle

  1.  SELECT 
  2.       ( 
  3.       SELECT  
  4.            wmsys.wm_concat(column_to_return_as_string)  
  5.       FROM  
  6.            a_pretend_table 
  7.       WHERE 
  8.            a_condition=true 
  9.       ) AS my_field_name 
  10.  FROM 
  11.       another_pretend_table 

Comments   

Lucas
# Lucas Thu, 21st June 2012
Hi Sir,

For the T-SQL, is there a way to do it with out the FOR XML PATH (''), I am trying to GROUP_CONCAT 2 cell [1111] and [2222] which all belong to ID column as
ID
1111
2222
My desired out put will be [1111,2222],rig ht now I am getting TAG_SEAL>111122 22, which have something to do with the FOR XML PATH (''). And if I get rid of it, it give me error "Subquery returned more than 1 value."

Your help is much appreciated !!!
Like | Dislike | 0 Reply | Reply with quote | Quote
Add Comment

Name:

Email:

Website:

Message:


Latest Posts

  • Joes Word Cloud (JWC)

    • Sat 23-Aug-14
      Salut Joe, Thank you again for helping me. I had installed V2.2 for Joomla 2.5. Finally, as you ...
      Nathalie  
    • Wed 20-Aug-14
      Salut Nathalie! I could only access the under construction page of your website, so I'm not 100% sure.
      Webmaster  
    • Tue 19-Aug-14
      Hi Joe, Thank you for your prompt reply. Below, an "image" of the cloud on my website : "pour ante ...
      Nathalie  
    • Mon 18-Aug-14
      Hi Nathalie, Thanks for the message. The module does not see Virtuemart text but that sounds like ...
      Webmaster  
    • Mon 18-Aug-14
      Hi Joel, Fist of all, thank you for the module. What such a work ! I have a problem with Virtuemart module.
      Nathalie