【问题标题】:Split variable length string into multiple columns将可变长度字符串拆分为多列
【发布时间】:2014-09-01 20:36:37
【问题描述】:

我有 2 张桌子 A 和 B。表A 中的数据有一个字符串,我想根据"#___-"(abc、xyz、efg 等)将其拆分为不同的列,并将其复制到表B 中。表A的字符串大小会因每一行而异

表 A:

Create table A ( data varchar(100));

Insert table A
  values ('101,#abc-sds)dfgd)3453)#xyz-hju)dddjfj)eieei)2323');
Insert into A
  values ('102,#abc-ddeff)errr)3434)#xyz-bnhb)hehe)333)#abc-dew)weerr)2343)#efg-3434)34');

Create table B ( id number, abc varchar2(50), xyz varchar2(50),
  efg varchar2(50));

表B的输出:

  ID  | ABC              |  XYZ                    |  EFG
  ---------------------------------------------------------------
  101 | sds)dfgd)3453)   | hju)dddjfj)eieei)2323   | 
  102 | ddeff)errr)3434) | bnhb)hehe)333)          | 3434)34
  102 | dew)weerr)2343)  | bnhb)hehe)333)          | 3434)34

我尝试了正则表达式,但由于字符串大小可能会有所不同,并且由于重复出现,我无法进一步提供任何帮助。

【问题讨论】:

  • 不明白如何拆分,标记是什么,显示更多示例以及如何拆分。
  • 您想将一个字符串分成多行和列吗?为什么 102 的 xyz/efg 值在两行中重复 - 如果#abc 是下一个“行”的开始,那么一行不会得到 xyz 而另一行是 efg?您的正则表达式尝试取得了多大的进展?

标签: sql regex oracle


【解决方案1】:

如果你问我想你在问什么,你需要Dynamic SQL、Regular Expressions和Pivoting。 动态 SQL 生成代码,该代码将使用适当的列创建一个表(因为它们以 #___- 命名,并且它们的数量是任意的)。 转置将一列值转置为每个 ID 的行。

我习惯使用 SQL Server,而不是 Oracle,而且我使用的 Regex 功能是 UDF,因为 SQL Server 没有本机 Regex 功能。所以我的回答只会有很大帮助,但可能会让你知道如何解决这个问题。

我想你可以在 Oracle 中构建一个正则表达式 UDF,我们的正则表达式 UDF 返回一个结果集,我可以 join 或在 SQL Server 的情况下为 cross/outer apply 它给我Value varchar 整个匹配和@987654325 @ 与捕获的子匹配:

create table #A (data varchar(100));

Insert into #A
  values ('101,#abc-sds)dfgd)3453)#xyz-hju)dddjfj)eieei)2323');
Insert into #A
  values ('102,#abc-ddeff)errr)3434)#xyz-bnhb)hehe)333)#abc-dew)weerr)2343)#efg-3434)34');

declare @cols nvarchar(max), @q nvarchar(max)


    -- Gets found columns comma-separated in @cols:
select @cols = isnull(@cols + ', ', '') + substring(r.Value, 2, len(r.Value)-2)
from #A
cross apply dbo.RegexFind(data, '#(\w+)-', 1, 1, 1) r
group by r.Value


    -- Dynamically create #B with @cols and insert
set @q = N'create table #B(id int, ' + replace(@cols, ', ', ' varchar(255), ') + ' varchar(255))
insert into #B
select p.*
from (
    select r.Value,
        a.SubMatches.value(''(//submatch[@index=0]/@value)[1]'', ''varchar(255)'') col,
        a.SubMatches.value(''(//submatch[@index=1]/@value)[1]'', ''varchar(255)'') val
    from #A
        -- Find the ID at the start
    cross apply dbo.RegexFind(data, ''^\d+'', 1, 1, 1) r
        -- Find any and all #___- occurrences and the text after it
    outer apply dbo.RegexFind(data, ''#(\w+)-([^#]+)'', 1, 1, 1) a
) s
pivot ( -- Pivot dynamically on the found @cols
    min(val) for col in (' + @cols + ')
) p
select * from #B'

exec sp_executesql @q

使用您的表 #A 的示例内容,这给出了以下 SQL(@q 的内容):

create table #B(id int, abc varchar(255), efg varchar(255), xyz varchar(255))
insert into #B
select p.*
from (
    select r.Value,
        a.SubMatches.value('(//submatch[@index=0]/@value)[1]', 'varchar(255)') col,
        a.SubMatches.value('(//submatch[@index=1]/@value)[1]', 'varchar(255)') val
    from #A
        -- Find the ID at the start
    cross apply dbo.RegexFind(data, '^\d+', 1, 1, 1) r
        -- Find any and all #___- occurrences and the text after it
    outer apply dbo.RegexFind(data, '#(\w+)-([^#]+)', 1, 1, 1) a
) s
pivot ( -- Pivot dynamically on the found @cols
    min(val) for col in (abc, efg, xyz)
) p
select * from #B

注意虽然#B这个临时表只存在于动态SQL里面,我不知道Oracle是怎么处理的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多