【问题标题】:there is a way to transpose two strings and have a table as result?有没有办法转置两个字符串并得到一个表格作为结果?
【发布时间】:2021-07-09 23:20:47
【问题描述】:

我有接下来的两个字符串:

String_Cod   = 14521;65412;65845
String_Flags = 1;0;1


for code 14521 the flag is 1
for code 65412 the flag is 0
for code 65845 the flag is 1
in this order always

结果一定是这样的

我从这个查询开始:

select regexp_substr(to_char(:STRING_COD),'[^;]+', 1, level)
from dual
connect BY regexp_substr(to_char(:STRING_COD), '[^;]+', 1, level)
is not null

select regexp_substr(to_char(:STRING_FLAGS),'[^;]+', 1, level)
from dual
connect BY regexp_substr(to_char(:STRING_FLAGS), '[^;]+', 1, level)
is not null

但我不知道如何继续加入两者并获得我需要的结果。

谁能给个建议?

问候

【问题讨论】:

    标签: oracle19c oracle-apex-19.1 oracle21c


    【解决方案1】:

    您可以在每个查询中将级别添加为另一列,并将它们连接在一起:

    select c.cod, f.flag
    from (
      select level as n, regexp_substr(to_char('14521;65412;65845'),'[^;]+', 1, level) as cod
      from dual
      connect BY regexp_substr(to_char('14521;65412;65845'), '[^;]+', 1, level)
      is not null
    ) c
    join (
      select level as n, regexp_substr(to_char('1;0;1'),'[^;]+', 1, level) as flag
      from dual
      connect BY regexp_substr(to_char('1;0;1'), '[^;]+', 1, level)
      is not null
    ) f
    on f.n = c.n
    

    which - 带有外部连接 - 将允许不同数量的元素;或者更简单地说,正如您建议的那样,它们将始终匹配,对两个提取物使用相同的级别:

    select regexp_substr(to_char('14521;65412;65845'),'[^;]+', 1, level) as cod,
      regexp_substr(to_char('1;0;1'),'[^;]+', 1, level) as flag
    from dual
    connect BY regexp_substr(to_char('14521;65412;65845'), '[^;]+', 1, level)
    is not null
    
    COD   | FLAG
    :---- | :---
    14521 | 1   
    65412 | 0   
    65845 | 1   
    

    db<>fiddle

    这种扩展值列表的方法还假设您在任何一个列表中都不能有空元素。 Read more.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 2013-06-18
      • 1970-01-01
      相关资源
      最近更新 更多