Vertica中的GROUP_CONCAT

时间:2016-06-20 23:12:48

标签: group-concat vertica

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

    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

我需要以获得以下结果的方式对其进行分组:

    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子句和强制分区,我无法进行正确的查询

3 个答案:

答案 0 :(得分:3)

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

select date, sum(raw) "raw", sum(unq) unq, rtrim(agg_concatenate(campaign || ', '),', ')
from mytest
group by 1
order by 1

我使用rtrim来摆脱最后的','。

如果您尚未创建它,则可以这样做:

-- 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;

答案 1 :(得分:1)

我知道以这种方式使用group_concat的唯一方法是分别查询数据并在最后组合它们。它不漂亮,我更喜欢我发布的其他方法,但这个方法更能直接回答你的问题。

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)

答案 2 :(得分:1)

如本link中所述:

Vertica 9.1.1-4现在具有称为LISTAGG的内置功能,该功能可以执行相同的操作...

使用“ LISTAGG”