sqlvertica

Concat GROUP BY in Vertica SQL


I need to get a comma separated list of ids as a field for a messy third party api :s This is a simplified version of what I am trying to achieve.

| id | name |
|====|======|
| 01 | greg |
| 02 | paul |
| 03 | greg |
| 04 | greg |
| 05 | paul |

SELECT name, {some concentration function} AS ids
FROM table
GROUP BY name

Returning

| name | ids        |
|======|============|
| greg | 01, 03, 04 |
| paul | 02, 05     |

I know MySQL has the CONCAT_GROUP function and I was hoping to solve this problem without installing more functions because of the environment. Maybe I can solve this problem using an OVER statement?


Solution

  • A really old question, but as an update, you can use listagg() function

    id name
    01 greg
    02 paul
    03 greg
    04 greg
    05 paul

    SELECT name, listagg(id) AS ids FROM table GROUP BY name

    That will return the desire output:

    name ids
    greg 01, 03, 04
    paul 02, 05