客户端具有以下表之一:
行索引 | id | 序列 | 值_文本 |
---|---|---|---|
1007_0_0 | 1007 | 1 | 800 |
1007_0_0 | 1007 | 2 | 1110 |
1007_0_0 | 1007 | 4 | 道路,修路 |
1007_0_0 | 1007 | 5 | 100 |
1007_0_1 | 1007 | 1 | 800 |
1007_0_1 | 1007 | 2 | 1115 |
1007_0_1 | 1007 | 4 | 道路,修路 |
1007_0_1 | 1007 | 5 | 100 |
1007_0_2 | 1007 | 1 | 800 |
1007_0_2 | 1007 | 2 | 1105 |
1007_0_2 | 1007 | 4 | 道路,修路 |
1007_0_2 | 1007 | 5 | 100 |
1007_0_3 | 1007 | 1 | 800 |
1007_0_3 | 1007 | 2 | 1120 |
1007_0_3 | 1007 | 4 | 道路,修路 |
1007_0_3 | 1007 | 5 | 100 |
1007_0 | 1007 | 6 | 珀丽 |
1007_0 | 1007 | 8 | 139 |
我想要实现的是将元组行添加到每个子元组(row_index)组中以进行旋转。
最终结果应该是这样的:
行索引 | id | 序列 | 值_文本 |
---|---|---|---|
1007_0_0 | 1007 | 6 | 珀丽 |
1007_0_0 | 1007 | 8 | 139 |
1007_0_0 | 1007 | 1 | 800 |
1007_0_0 | 1007 | 2 | 1110 |
1007_0_0 | 1007 | 4 | 道路,修路 |
1007_0_0 | 1007 | 5 | 100 |
1007_0_1 | 1007 | 6 | 珀丽 |
1007_0_1 | 1007 | 8 | 139 |
1007_0_1 | 1007 | 1 | 800 |
1007_0_1 | 1007 | 2 | 1115 |
1007_0_1 | 1007 | 4 | 道路,修路 |
1007_0_1 | 1007 | 5 | 100 |
1007_0_2 | 1007 | 6 | 珀丽 |
1007_0_2 | 1007 | 8 | 139 |
1007_0_2 | 1007 | 1 | 800 |
1007_0_2 | 1007 | 2 | 1105 |
1007_0_2 | 1007 | 4 | 道路,修路 |
1007_0_2 | 1007 | 5 | 100 |
1007_0_3 | 1007 | 6 | 珀丽 |
1007_0_3 | 1007 | 8 | 139 |
1007_0_3 | 1007 | 1 | 800 |
1007_0_3 | 1007 | 2 | 1120 |
1007_0_3 | 1007 | 4 | 道路,修路 |
1007_0_3 | 1007 | 5 | 100 |
如果下划线的数量告诉您什么是元组,什么是子元组,您可以擦除(超级)元组,将它们连接到匹配的子元组,然后在添加不同的子元组后缀后重新插入它们。
with tuple as (delete from your_table where regexp_count(row_index,'_')=1
returning *)
insert into your_table
select distinct
tuple.row_index||'_'||split_part(subtuple.row_index,'_',3)
,tuple.id
,tuple.seq
,tuple.value_text
from tuple join your_table subtuple
on regexp_count(subtuple.row_index,'_')=2
--and subtuple.row_index ~ ('^'||tuple.row_index)
and tuple.id=subtuple.id
returning *;
行索引 | id | 序列 | 值_文本 |
---|---|---|---|
1007_0_0 | 1007 | 6 | 珀丽 |
1007_0_0 | 1007 | 8 | 139 |
1007_0_1 | 1007 | 6 | 珀丽 |
1007_0_1 | 1007 | 8 | 139 |
1007_0_2 | 1007 | 6 | 珀丽 |
1007_0_2 | 1007 | 8 | 139 |
1007_0_3 | 1007 | 6 | 珀丽 |
1007_0_3 | 1007 | 8 | 139 |