Vertica SQL中的Concat GROUP BY

时间:2014-01-10 20:06:18

标签: sql vertica

我需要以逗号分隔的id列表作为凌乱的第三方api的字段:s这是我想要实现的简化版本。

| id | name |
|====|======|
| 01 | greg |
| 02 | paul |
| 03 | greg |
| 04 | greg |
| 05 | paul |

SELECT name, {some concentration function} AS ids
FROM table
GROUP BY name

返回

| name | ids        |
|======|============|
| greg | 01, 03, 04 |
| paul | 02, 05     |

我知道MySQL具有CONCAT_GROUP功能,我希望在没有安装更多功能的情况下解决这个问题,因为环境。也许我可以使用OVER语句解决这个问题?

3 个答案:

答案 0 :(得分:9)

您必须将OVER()NVL()一起使用(您必须为每个名称扩展连接超过10个实例):

CREATE TABLE t1 (
  id int,
  name varchar(10)
);

INSERT INTO t1
SELECT 1 AS id, 'greg' AS name
UNION ALL
SELECT 2, 'paul'
UNION ALL
SELECT 3, 'greg'
UNION ALL
SELECT 4, 'greg'
UNION ALL
SELECT 5, 'paul';

COMMIT;

SELECT name,
    MAX(DECODE(row_number, 1, a.id)) ||
    NVL(MAX(DECODE(row_number, 2, ',' || a.id)), '') ||
    NVL(MAX(DECODE(row_number, 3, ',' || a.id)), '') ||
    NVL(MAX(DECODE(row_number, 4, ',' || a.id)), '') ||
    NVL(MAX(DECODE(row_number, 5, ',' || a.id)), '') ||
    NVL(MAX(DECODE(row_number, 6, ',' || a.id)), '') ||
    NVL(MAX(DECODE(row_number, 7, ',' || a.id)), '') ||
    NVL(MAX(DECODE(row_number, 8, ',' || a.id)), '') ||
    NVL(MAX(DECODE(row_number, 9, ',' || a.id)), '') ||
    NVL(MAX(DECODE(row_number, 10, ',' || a.id)), '') id
FROM
    (SELECT name, id, ROW_NUMBER() OVER(PARTITION BY name ORDER BY id) row_number FROM t1) a
GROUP BY a.name
ORDER BY a.name;

<强>结果

 name |  id
------+-------
 greg | 1,3,4
 paul | 2,5

答案 1 :(得分:4)

请参阅Vertica安装附带的vertica示例中的Concatenate UDAF 那是mysql的等价物。你可以直接安装它。

more /opt/vertica/sdk/examples/AggregateFunctions/Concatenate.cpp

-- Shell comppile
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

-- Create LIBRARY
CREATE LIBRARY AggregateFunctionsConcatenate AS '/opt/vertica/sdk/examples/AggregateFunctions/Concatenate.so';
CREATE AGGREGATE FUNCTION agg_group_concat AS LANGUAGE 'C++' NAME 'ConcatenateFactory' LIBRARY AggregateFunctionsConcatenate;


in the Concatenate.cpp
replace : input_len*10
with : 65000

您必须在代码中替换此值。

65000是varchar可以获得的最大长度。并且由于vertica不会将所有65000用于小于65000字符的值,您可以。

答案 2 :(得分:2)

从长远来看,最简单的方法是在https://github.com/vertica/Vertica-Extension-Packages/tree/master/strings_package的github上使用官方的Vertica UDF,它提供了group_concat功能。安装程序可以在README中找到,甚至提供了示例。