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:
copyraw
RowID     column_to_return_as_string   
--------- --------------------------
1         Me
2         Myself
3         I

-- to be returned as
RowID     my_field_name   
--------- --------------------------
1         Me,Myself,I
  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,

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
copyraw
SELECT
     (
	SELECT 
		GROUP_CONCAT(column_to_return_as_string) 
	FROM 
		a_pretend_table
	WHERE
		a_condition=true
     ) AS my_field_name
FROM
     another_pretend_table
  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
copyraw
SELECT
     STUFF(
       ( 
	 SELECT 
		',' + column_to_return_as_string 
	 FROM 
		a_pretend_table 
	 WHERE
		a_condition=true
	 FOR XML PATH ('')
       ),1,1,''
     ) AS my_field_name
FROM
     another_pretend_table
  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
copyraw
SELECT
     (
	SELECT 
		wmsys.wm_concat(column_to_return_as_string) 
	FROM 
		a_pretend_table
	WHERE
		a_condition=true
     ) AS my_field_name
FROM
     another_pretend_table
  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 
Category: Databases :: Article: 403

Add comment

Your rating:

Submit

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Accreditation

Badge - Zoho Creator Certified Developer Associate
Badge - Zoho Deluge Certified Developer
Badge - Certified Zoho CRM Developer

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF

Please publish modules in offcanvas position.