aggregate function to concatenate strings in Vertica

前端 未结 3 779
礼貌的吻别
礼貌的吻别 2021-01-13 02:43

have a table in vertica: test like this:

ID     |   name
1      |    AA
2      |    AB
2      |    AC
3      |    AD
3      |    AE
3      |    AF

3条回答
  •  没有蜡笔的小新
    2021-01-13 03:09

    First, you'll need to compile the udx for agg_concatenate.

    -- Shell commands
    cd /opt/vertica/sdk/examples/AggregateFunctions/
    g++ -D HAVE_LONG_INT_64 -I /opt/vertica/sdk/include -Wall -shared -Wno-unused-value -fPIC -o Concatenate.so Concatenate.cpp /opt/vertica/sdk/include/Vertica.cpp
    
    -- vsql commands
    CREATE LIBRARY AggregateFunctionsConcatenate AS '/opt/vertica/sdk/examples/AggregateFunctions/Concatenate.so';
    CREATE AGGREGATE FUNCTION agg_concatenate AS LANGUAGE 'C++' NAME 'ConcatenateFactory' LIBRARY AggregateFunctionsConcatenate;
    

    Then you can do a query like:

    select id, rtrim(agg_concatenate(name || ', '),', ') ag
    from mytable
    group by 1
    order by 1
    

    Uses rtrim to get rid of the last ', '.

    If you need the aggregate to be sorted a certain way, you may need to select/sort in an inline view or with first.

提交回复
热议问题