0

I'm creating a temp table from multiple sources and need to remove duplicate records from it. For example, my temp table will have data like:

RecordID     Color
1            Black
2            Black
2            Blue

I'd like to consolidate RecordID 2 down to:

RecordID     Color
1            Black
2            Black,Blue
GMB
  • 216,147
  • 25
  • 84
  • 135
MrPToast
  • 1
  • 1
  • 4
    Which RDBMS are you using? Please add the relevant tag to your question (mysql, oracle, sql-server...). – GMB Apr 06 '19 at 21:55
  • Possible duplicate of [mysql GROUP\_CONCAT](https://stackoverflow.com/questions/2516545/mysql-group-concat) – Alex Apr 06 '19 at 21:56

2 Answers2

0

You need to use aggregation. The actual aggregate function depends on your RDBMS.

In MySQL, use GROUP_CONCAT (the default separator is the comma):

SELECT RecordID, GROUP_CONCAT(Color) AS Color
FROM mytable
GROUP BY RecordID

In Oracle, use LISTAGG:

SELECT RecordID, LISTAGG(Color, ',') AS Color
FROM mytable
GROUP BY RecordID

In Postgres (>=9.0) or SQL-Server 2017, use STRING_AGG:

SELECT RecordID, STRING_AGG(Color, ',') AS Color
FROM mytable
GROUP BY RecordID
GMB
  • 216,147
  • 25
  • 84
  • 135
0

if its oracle then :

select "RecordID",listagg("Color",',' ) WITHIN GROUP (ORDER BY "RecordID","Color" ) 
Color
FROM Table1 group by "RecordID";

http://sqlfiddle.com/#!4/abaab5/5

Nikhil S
  • 3,786
  • 4
  • 18
  • 32