GROUP_CONCAT in Vertica

不羁的心 提交于 2019-12-10 21:41:56

问题


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


回答1:


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;



回答2:


The only way that I know to use group_concat this way would be to do just query the data separately and combine them at the end. It isn't pretty and I prefer the other method I posted, but this one is a more direct answer to your question.

with camps as (
  select date, group_concat(campaign) over (partition by date) campaigns
  from mytest
), sums as (
  select date, sum(raw) "raw", sum(unq) unq
  from mytest
  group by date
)
select c.date, "raw", unq, campaigns
from camps c
join sums s on (c.date = s.date)



回答3:


As mentioned in this link:

Vertica 9.1.1-4 now has a built-in function called LISTAGG that does the same thing...

Use "LISTAGG"



来源:https://stackoverflow.com/questions/37933223/group-concat-in-vertica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!