Vertica中的GROUP_CONCAT [英] GROUP_CONCAT in Vertica

查看:454
本文介绍了Vertica中的GROUP_CONCAT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有类似这样的数据:

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具有功能GROUP_CONCAT. Vertica还支持GROUP_CONCAT,但是由于OVER子句和强制分区,我无法进行正确的查询

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

推荐答案

假设您已经在sdk/examples目录中编译并创建了该函数,则应该能够做到:

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

我使用rtrim摆脱了最后一个'.'.

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;

这篇关于Vertica中的GROUP_CONCAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆