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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;

运行结果:

1
2
3
4

>
UP |
> v_csap_node0001,v_csap_node0002,v_csap_node0003,v_csap_node0004,v_csap_node0005,v_csap_node0006,v_csap_node0007,v_csap_node0008,v_csap_node0009,v_csap_node0010,v_csap_node0011,v_csap_node0012,v_csap_node0013,v_csap_node0014,v_csap_node0015,v_csap_node0016,v_csap_node0018,v_csap_node0019,v_csap_node0020,v_csap_node0021,v_csap_node0022,v_csap_node0023,v_csap_node0024,v_csap_node0025,v_csap_node0026,v_csap_node0027,v_csap_node0028,v_csap_node0029,v_csap_node0030
> STANDBY | v_csap_node0017 (2 rows)

可以看到是我想要的结果。
有人会问了,我不知道自己要ws_concat几个类别怎么办,先运行一下

1
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++好的童鞋可以试一下。