我有一个关于IT网络的表格,其中包含以下列:
-gid是id
-cableA包含一个cat4排序的电线列表,用'+'分隔并具有geom
-cableB包含一个cat5排序的电线列表,用'+'分隔并具有geom
-cableC包含一个cat6排序的电线列表,用'+'分隔并具有geom
-secondC包含有关每个项目及其来源的详细信息
-geom是项目的地方
我目前的剧本:
create table a(gid integer
, cableA varchar
, cableB varchar
, cableC varchar
, secondC varchar,
geom varchar);
-- These wires have 197 as id, no wire of typeA, one wire of typeB, one wire of typeC, origin not present, g1 as geom
INSERT INTO a(gid, cableA, cableB, cableC, secondC, geom) VALUES
(197
, ''
, 'A'
, 'B'
, ''
, 'G1');
-- These wires have 2 as id, two wires of typeA, one wire of typeB, one wire of typeC, Origin of F is A/ Origin of G is B/ Origin of A is A/ Origin of B is A, g2 as geom
INSERT INTO a(gid, cableA, cableB, cableC, secondC, geom) VALUES (2
, 'F+G'
, 'A'
, 'B'
, 'A+B+A+A'
, 'G2');
-- These wires have 3 as id, 3 wires of typeA, one wire of typeB, one wire of typeC, Origin of U is A/ Origin of V is B/ Origin of W is A/ Origin of A is A/ Origin of B is A, g3 as geom
INSERT INTO a(gid, cableA, cableB, cableC, secondC, geom) VALUES (3
, 'U+V+W'
, 'A'
, 'B'
, 'A+B+A+A+A'
, 'G3')
这是我的疑问:
select
gid,
unnest(string_to_array(a.CableC,'+')) as split1,
case when a.secondC is not null
then unnest(string_to_array(a.secondC,'+')) else '' end as split2,
geom
from a
where cableC = 'B'
--I want and need
-- 197 / NULL / G1 because the secondC string is empty
-- 2 / A / G2 because 4th position in secondC
-- 3 / A / G3 because 5th position in secondC
目标是将所有GX和原点链接到电线。我不知道如何将位置保持在secondC列内并获得正确的开关。
感谢您的帮助
答案 0 :(得分:0)
您不需要unnest()
。首先,将所有cables
以及secondc
转换为数组:
select
gid,
string_to_array(format('%s+%s+%s', cablea, cableb, cablec), '+') as cables,
string_to_array(secondc, '+') as secondc,
geom
from a
gid | cables | secondc | geom
-----+-------------+-------------+------
197 | {"",A,B} | {} | G1
2 | {F,G,A,B} | {A,B,A,A} | G2
3 | {U,V,W,A,B} | {A,B,A,A,A} | G3
(3 rows)
接下来,获得' B'在cables
中找到secondc
中的相应元素:
select
gid,
secondc[array_position(cables, 'B')],
geom
from (
select
gid,
string_to_array(format('%s+%s+%s', cablea, cableb, cablec), '+') as cables,
string_to_array(secondc, '+') as secondc,
geom
from a
) s
gid | secondc | geom
-----+---------+------
197 | | G1
2 | A | G2
3 | A | G3
(3 rows)