9

I have two columns in oracle database

+---------+---------+
| Column1 | Column2 |
+---------+---------+
| A       | 1       |
| A       | 2       |
+---------+---------+

I want to retireive the data like i will get data as result

+---------+---------+
| Column1 | Column2 |
+---------+---------+  
| A       | 1,2     |
+---------+---------+

Please provide me the solution.

giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
  • I've edited your question to see lines in code mode, can you confirm that's what you meant... Oops, bluefeet trumped my edits. – boisvert Aug 27 '12 at 16:01
  • possible duplicate of [column values in a row](http://stackoverflow.com/questions/9482560/column-values-in-a-row) – Justin Cave Aug 27 '12 at 16:07
  • possible duplicate of [Building a comma-separated list of values in an Oracle SQL statement](http://stackoverflow.com/questions/5822700/building-a-comma-separated-list-of-values-in-an-oracle-sql-statement) – DCookie Aug 27 '12 at 17:39
  • "Please provide me the solution." nice one – giannis christofakis Feb 16 '18 at 11:08

4 Answers4

20

Tim Hall has a pretty canonical list of string aggregation techniques in Oracle.

Which technique you use depends on a number of factors including the version of Oracle and whether you are looking for a purely SQL solution. If you are using Oracle 11.2, I'd probably suggest using LISTAGG

SELECT column1, listagg( column2, ',' ) WITHIN GROUP( order by column2 )
  FROM table_name
 GROUP BY column1

If you are using an earlier version of Oracle, assuming you don't need a purely SQL solution, I would generally prefer using the user-defined aggregate function approach.

Justin Cave
  • 227,342
  • 24
  • 367
  • 384
4

All abow answers are correct and I want to add one case to solve small problem. In my case my_column1 type was nvarchar2 but text was number and the bellow code does not work and display me only whitespace:

select group_id, listagg( t.my_column1 || '-' || to_char(t.doc_date,'dd.mm.yyyy') ||  ' ') within group(order by doc_date) 
 from my_table t
 group by group_id

when I wrote like this it works.

select group_id, listagg( to_char(t.my_column1) || '-' || to_char(t.doc_date,'dd.mm.yyyy') ||  ' ') within group(order by doc_date) 
 from my_table t
 group by group_id

I hope my feedback would save someone's time

Ulug'bek
  • 2,762
  • 6
  • 31
  • 59
3

If you have got 10g, then you have to go through the function below:

CREATE OR REPLACE FUNCTION get_comma_separated_value (input_val  in  number)
  RETURN VARCHAR2
IS
  return_text  VARCHAR2(10000) := NULL;
BEGIN
  FOR x IN (SELECT col2 FROM table_name WHERE col1 = input_val) LOOP
    return_text := return_text || ',' || x.col2 ;
  END LOOP;
  RETURN LTRIM(return_text, ',');
END;
/

So, you can do like:

select col1, get_comma_separated_value(col1) from table_name

Fiddle here

If you have got oracle 11g, you can use listagg :

SELECT 
    col1,
    LISTAGG(col2, ', ') WITHIN GROUP (ORDER BY col2) "names"
FROM table_x
GROUP BY col1

Fiddle here for Listagg

For mysql, its gonna be simple:

SELECT col1, GROUP_CONCAT(col2) FROM table_name GROUP BY col1
hsuk
  • 6,770
  • 13
  • 50
  • 80
2

On my oracle version 10 it do the job:

SELECT column1, wm_concat( column2)
  FROM table_name
 GROUP BY column1