【问题标题】:Convert a series of Number values in Text in Oracle SQL Query在 Oracle SQL Query 中转换 Text 中的一系列 Number 值
【发布时间】:2021-10-05 12:10:57
【问题描述】:

在 Oracle 数据库中,我有字符串值 (VARCHAR2),例如 1、4、7、8。数字表示为 1=car, 2= bus, 3=BB, 4=SB, 5=Ba, 6=PA, 7=HB, 8=G 并想在我的查询结果中将上述示例转换为“car,SB,HB,G” 我尝试使用“解码”,但它不起作用。请告知如何使其工作。将不胜感激。

谢谢` 最初,我使用了以下查询:

    Select Clientid as C#, vehicletypeExclusions as vehicle  from 
    clients 

结果样本是:

   C#   Vehicle
   20   1,19,20,23,24,7,5
   22   1,19,20,23,24,7,5

我还尝试了以下方法,使我获得了车辆的空值:

  Select Clientid as C#,  Decode (VEHICLETYPEEXCLUSIONS, '1', 'car', 
  '3','bus', '5','ba' ,'7','HB', '8','G'
  , '9','LED1102', '10','LED1104', '13','LED8-2',
  '14','Flip4-12', '17','StAT1003', '19','Taxi-Min', '20','Tax_Sed', 
   '21','Sup-veh'  , '22','T-DATS', '23','T-Mini',
   '24','T-WAM') as vehicle_Ex  from clients >

【问题讨论】:

  • 请在您的问题中包含您当前的查询,以确保您得到适当的答案。谢谢!

标签: sql oracle oracle11g


【解决方案1】:

这是一个选项。在代码中读取 cmets。第 1 - 13 行中的样本数据;查询从第 14 行开始。

SQL> with
  2  expl (id, name) as
  3    (select 1, 'car' from dual union all
  4     select 2, 'bus' from dual union all
  5     select 3, 'BB'  from dual union all
  6     select 4, 'SB'  from dual union all
  7     select 5, 'Ba'  from dual union all
  8     select 6, 'PA'  from dual union all
  9     select 7, 'HB'  from dual union all
 10     select 8, 'G'   from dual
 11    ),
 12  temp (col) as
 13    (select '1,4,7,8' from dual),
 14  -- split COL to rows
 15  spl as
 16    (select regexp_substr(col, '[^,]+', 1, level) val,
 17            level lvl
 18     from temp
 19     connect by level <= regexp_count(col, ',') + 1
 20    )
 21  -- join SPL with EXPL; aggregate the result
 22  select listagg(e.name, ',') within group (order by s.lvl) result
 23  from expl e join spl s on s.val = e.id;

RESULT
--------------------------------------------------------------------------------
car,SB,HB,G

SQL>

【讨论】:

    【解决方案2】:

    使用来自https://stackoverflow.com/a/68537479/429100的函数f_subst

    create or replace
    function f_subst(str varchar2, template varchar2, subst sys.odcivarchar2list) return varchar2
    as
        res varchar2(32767):=str;
    begin
        for i in 1..subst.count loop
            res:=replace(res, replace(template,'%d',i), subst(i));
        end loop;
        return res;
    end;
    /
    

    我已将ora_name_list_t(嵌套表)替换为sys.odcivarchar2list(varray)以简化此示例,但我建议您创建自己的集合,例如create type varchar2_table as table of varchar2(4000);

    例子:

    select
       f_subst(
          '1,4,7,8'
         ,'%d'
         ,sys.odcivarchar2list('car','bus','BB','SB','Ba','PA','HB','G')
       ) s 
    from dual; 
    
    S
    ----------------------------------------
    car,SB,HB,G
    

    【讨论】:

      【解决方案3】:

      假设您有一个查找表(将数字代码与描述相关联)和一个输入字符串表,我在测试中将其称为sample_inputs,如下所示:

      create table lookup (code, descr) as
          select 1, 'car' from dual union all
          select 2, 'bus' from dual union all
          select 3, 'BB'  from dual union all
          select 4, 'SB'  from dual union all
          select 5, 'Ba'  from dual union all
          select 6, 'PA'  from dual union all
          select 7, 'HB'  from dual union all
          select 8, 'G'   from dual
      ;
      
      create table sample_inputs (str) as
          select '1,4,7,8' from dual union all
          select null      from dual union all
          select '3'       from dual union all
          select '5,5,5'   from dual union all
          select '6,2,8'   from dual
      ;
      

      解决问题的一种策略是拆分输入 - 稍作修改以使其成为 JSON 数组,以便我们可以使用 json_table 拆分它 - 然后加入查找表并重新聚合。

      select s.str, l.descr_list
      from   sample_inputs s cross join lateral
             ( select listagg(descr, ',') within group (order by ord) as descr_list
               from   json_table( '[' || str || ']', '$[*]'
                                  columns code number path '$', ord for ordinality)
                      join lookup l using (code)
             ) l
      ;
      
      STR     DESCR_LIST                    
      ------- ------------------------------
      1,4,7,8 car,SB,HB,G                   
                                            
      3       BB                            
      5,5,5   Ba,Ba,Ba                      
      6,2,8   PA,bus,G   
      

      【讨论】:

      • 您好,我喜欢您的方法,但我不了解 JSON。另外,我不知道如何在数据库中创建 Json 表。谢谢
      • @user13058283 - JSON 本身就是一个完整的主题(甚至不限于 SQL 编程)。话虽如此:对于此解决方案,您无需在数据库中创建 JSON 表;函数 JSON_TABLE() 对字符串(特殊字符串,它们是有效的 JSON 字符串)进行操作。在这种情况下,输入是简单的数字数组,在 JSON 中具有类似 '[3,4,11,3,1]' 的语法(是的,就是这么简单!)。我使用的JSON_TABLE 函数返回各个元素以及它们在数组中的顺序。您可以看到为什么我必须将 '['']' 连接到输入字符串。
      • @user13058283 - 话虽如此,我欠你一个道歉。我没有注意到oracle 11g 标签。 JSON 函数是在 Oracle 12 中引入的。横向连接也是如此。我将把答案留在这里,它可能会帮助其他使用较新版本的 Oracle 有类似问题的人,但对您没有帮助。
      猜你喜欢
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多