Search code examples
mysqlsqljoingroup-byprocedure

Clustering data with same column and merging its parameters adn remove other entries


I have a database with following column and simple snippet of data :

p-id    Name      Size
20036   Prod_123   L
20043   Prod_123   M
20094   Prod_123   XL
20249   Prod_123   S
35188   Prod_826   L
45325   Prod_826   M
39407   Prod_826   XL
20691   Prod_826   XXL

I would like to cluster all the data with same product name and also would like to merge its column called size so that I will get all the values. So I want my output to look like

p-id    Name      Size
20036   Prod_123   L,M,XL,S
35118   Prod_826   L,M,XL,XXL

I want to do this with Procedure or if possible then with by query also.


Solution

  • Try to use

    SELECT name, GROUP_CONCAT(size) FROM <<table>> GROUP BY name;
    

    or

    SELECT min(p_id),name, GROUP_CONCAT(size) FROM <<table>> GROUP BY name;