我在数据库中有一个看起来像这样的记录(例如)。
ID, Name, Brand
1, 'Bike', 'Schwinn'
2, 'Car', 'Ford, Honda, Chevy'
3, 'Bike', 'Schwinn, Trex'
4, 'Car', 'Honda'
我需要将数据导出并创建多个记录,其中Brand有多个条目。我还需要增加输出的ID,所以我没有重复。 (我可以为此使用一个序列,并将其设置为更高的db中的最大值。)
我的输出看起来像
ID, Name, Brand
1, Bike, Schwinn
2, Car, Ford
Sequence.nextval, Car, Honda
Sequence.nextval, Car, Chevy
3, Bike, Schwinn
Sequence.nextval, Bike, Trex
4, Car, Honda
我想尝试用SQL
语句来解决这个问题。基本上我是通过直接csv
将此数据转储为SQL
文件。
我很难通过品牌专栏loop/split
尝试。
答案 0 :(得分:1)
您可以使用以下select语句:
with test_tab (ID, Name, Brand) as (
select 1, 'Bike', 'Schwinn' from dual union all
select 2, 'Car', 'Ford, Honda, Chevy' from dual union all
select 3, 'Bike', 'Schwinn, Trex' from dual union all
select 4, 'Car', 'Honda' from dual)
--------------------
-- End of Data Preparation
--------------------
select case when level <> 1 then <your_sequece>.nextval else id end as id,
name,
trim(regexp_substr(Brand, '[^,]+', 1, level)) BRAND
from test_tab
connect by regexp_substr(Brand, '[^,]+', 1, level) is not null
and prior Brand = Brand
and prior sys_guid() is not null;
输出将是:
ID NAME BRAND
---------------------
2 Car Ford
5 Car Honda
6 Car Chevy
4 Car Honda
1 Bike Schwinn
3 Bike Schwinn
7 Bike Trex
您可以将Insert语句写为
Insert into <destination_table>
select case when level <> 1 then <your_sequece>.nextval else id end as id,
name,
trim(regexp_substr(Brand, '[^,]+', 1, level)) BRAND
from <source_table>
connect by regexp_substr(Brand, '[^,]+', 1, level) is not null
and prior Brand = Brand
and prior sys_guid() is not null;
PS:如果ID是唯一的,您可以尝试将and prior Brand = Brand
替换为and prior ID = ID
以删除重复的循环值。
select case when level <> 1 then <your_sequece>.nextval else id end as id,
name,
trim(regexp_substr(Brand, '[^,]+', 1, level)) BRAND
from <source_table>
connect BY regexp_substr(Brand, '[^,]+', 1, level) is not null
and prior ID = ID
and prior sys_guid() is not null;