SPLIT_PART 为负值 [Postgres 9.5]
SPLIT_PART with a negative value [Postgres 9.5]
我需要对该查询使用 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
但它给了我以下错误:
ERROR: field position must be greater than zero
那么,我该如何处理这里的负值?
这种reverse(split_part(reverse(col_A), '_'::text, 1))
的说法行得通吗?我指的是 that question.
编辑:我完全被这个查询困住了。
更多细节:我有一列 "server name" 和另一列不同的组用逗号分隔。
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
如果负数应该表示距末尾的偏移量,则两步法可能更好:
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" 列。
我需要对该查询使用 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
但它给了我以下错误:
ERROR: field position must be greater than zero
那么,我该如何处理这里的负值?
这种reverse(split_part(reverse(col_A), '_'::text, 1))
的说法行得通吗?我指的是 that question.
编辑:我完全被这个查询困住了。
更多细节:我有一列 "server name" 和另一列不同的组用逗号分隔。
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
如果负数应该表示距末尾的偏移量,则两步法可能更好:
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" 列。