Search code examples
google-sheetsgoogle-query-language

Combine duplicate rows in column as comma separated values - Google Query


If i have 2 columns viz., ID & Name, ID column containing duplicates, and if i want to group by ID to get unique ID's but name column should be a comma-separated list, can this be possible in Google Query?

| ID   | Name |
===============
| 1001 | abc  |
---------------
| 1001 | def  |
---------------
| 1002 | kjg  |
---------------
| 1003 | aof  |
---------------
| 1003 | lmi  |
---------------
| 1004 | xyz  |
---------------

into

| ID   | Name      |
====================
| 1001 | abc, def  |
--------------------
| 1002 | kjg       |
--------------------
| 1003 | aof, lmi  |
--------------------
| 1004 | xyz       |
--------------------

Solution

  • try:

    =ARRAYFORMULA({QUERY(QUERY({A2:B, B2:B}, 
     "select Col1,max(Col2) 
      where Col1 is not null 
      group by Col1 
      pivot Col3"), 
     "select Col1 
      offset 1", 0), REGEXREPLACE(TRIM(
     TRANSPOSE(QUERY(TRANSPOSE(QUERY(QUERY({A2:B&",", B2:B}, 
     "select max(Col2) 
      where Col1 is not null 
        and Col2 <> ',' 
      group by Col1 
      pivot Col3"), 
     "offset 1", 0)),,999^9))), ",$", )})
    

    enter image description here

    however, this may not work for massive datasets due to TRIM (which is needed to remove empty spaces) and REGEXREPLACE (which is needed to remove the end comma) limitations. otherwise, without it, the formula can handle anything:

    =ARRAYFORMULA({QUERY(QUERY({A2:B, B2:B}, 
     "select Col1,max(Col2) 
      where Col1 is not null 
      group by Col1 
      pivot Col3"), 
     "select Col1 
      offset 1", 0), 
     TRANSPOSE(QUERY(TRANSPOSE(QUERY(QUERY({A2:B&",", B2:B}, 
     "select max(Col2) 
      where Col1 is not null 
        and Col2 <> ',' 
      group by Col1 
      pivot Col3"), 
     "offset 1", 0)),,999^9))})