Vertica的这些事(三)——Vertica中实现Oracle中的ws_concat功能

时间:2022-10-08 18:32:29

vertica中没有类似Oracle中的ws_concat函数功能,需要开发UDF,自己对C++不熟悉,所有只有想其他方法解决了。

上代码:

SELECT node_state,
MAX(DECODE(row_number, 1, a.node_name)) ||
NVL(MAX(DECODE(row_number, 2, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 3, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 4, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 5, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 6, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 7, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 8, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 9, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 10, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 11, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 12, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 13, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 14, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 15, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 16, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 17, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 18, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 19, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 20, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 21, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 22, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 23, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 24, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 25, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 26, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 27, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 28, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 29, ',' || a.node_name)), '') ||
NVL(MAX(DECODE(row_number, 30, ',' || a.node_name)), '') node_name
FROM (select node_state,node_name,row_number() over (partition by node_state order by node_name) row_number from nodes) a
group by node_state;``` 运行结果:

可以看到是我想要的结果。

有人会问了,我不知道自己要ws_concat几个类别怎么办,先运行一下

select node_state,node_name,row_number() over (partition by node_state order by node_name) row_number from nodes

看下row_number不就行了,或者把decode中的数字多写几个。当然最好的方法还是开发自定义程序包,引入到vertica中,直接使用函数实现,C++好的童鞋可以试一下。