Suppose we have data something like this:
date | campaign | raw | unq
------------+----------+-----+-----
2016-06-01 | camp1 | 5 | 1
2016-06-01 | camp2 | 10 | 1
2016-06-01 | camp3 | 15 | 2
2016-06-02 | camp4 | 5 | 3
2016-06-02 | camp1 | 5 | 1
I need to group it in such a way as to obtain the following result:
date | campaigns | raw | unq
------------+---------------------+----- +-----
2016-06-01 | camp1, camp2, camp3 | 30 | 4
2016-06-02 | camp4, camp1 | 10 | 4
Mysql for these purposes has a function GROUP_CONCAT. Vertica also supports GROUP_CONCAT but I cannot make proper query due to the OVER clause and mandatory partitioning
Assuming you've compiled and created the function in the sdk/examples
directory, you should be able to do:
select date, sum(raw) "raw", sum(unq) unq, rtrim(agg_concatenate(campaign || ', '),', ')
from mytest
group by 1
order by 1
I use rtrim to get rid of the last ', '.
If you haven't created it, you can do so:
-- 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;