带负值的SPLIT_PART [Postgres 9.5]

问题描述 投票:0回答:1

我需要在该查询上使用split_part函数:

CREATE TABLE client_group_by_group_test AS SELECT *, SPLIT_PART( groupe,
 ',', 1 ) AS group1, SPLIT_PART(SPLIT_PART(groupe,',',2),',',-1) AS 
 group2, SPLIT_PART(SPLIT_PART(groupe,'',3),'',-3) AS group3, 
 SPLIT_PART(groupe,'',-4) AS group4 FROM planification_client

但它给了我以下错误:

错误:字段位置必须大于零

那么,我怎样才能在这里处理负值?

这种reverse(split_part(reverse(col_A), '_'::text, 1))的声明能起作用吗?我正在引用that question

编辑:我完全坚持这个查询。

更多细节:我有一个列有“服务器名称”,另一个列有不同的组用逗号分隔。

server name| group                        |
-----------+------------------------------+
XPTERTBIEP9|GRNW_SPO_S_F_H, GRNW_SPO_S_I_J|

我需要得到的输出是,如果服务器有多个组,它们需要在group1,group2等不同的列中...

server name| group                        |group1        |group 2
-----------+------------------------------+--------------+--------------
XPTERTBIEP9|GRNW_SPO_S_F_H, GRNW_SPO_S_I_J|GRNW_SPO_S_F_H|GRNW_SPO_S_I_J
postgresql split postgresql-9.5
1个回答
1
投票

如果负数应该表示从结尾的偏移量,则两步法可能更好:

CREATE TABLE client_group_by_group_test 
AS 
SELECT ..., 
       agroups[1] as group1,
       agroups2[cardinality(agroups2) - 1] as groups2,
       agroups3[cardinality(agroups3) - 3] as groups3,
       agroups[cardinality(agroups) - 4] as group4
from (
   select *,
          string_to_array(groupe, ',') as agroups, 
          (string_to_array(string_to_array(groupe, ','), ',')[2]) as agroups2,
          (string_to_array(string_to_array(groupe, ','), ',')[3]) as agroups3,
   from planification_client
) t

请注意,您需要在最外面的SELECT列出所需的列以排除中间的“agroups”列。

© www.soinside.com 2019 - 2024. All rights reserved.