【问题标题】:How to parse text in oracle Query如何在 oracle Query 中解析文本
【发布时间】:2018-05-31 18:05:00
【问题描述】:

当我在 Oracle 上运行查询时,我在同一列上收到此结果:

文本1 - 文本2 - 文本3 - 文本4

(有很多列)

我应该从每个文本中计数

感谢您的帮助

查询:

     select ca.title
     from table_case ca,
    table_contract x, table_site_part sp, mtm_site_part24_contract5 mtm,
    table_queue q,
    table_user u_og,
    table_employee empl,
    table_site office,
    table_site s,
    table_address addr,
    table_contact_role co_r,
    table_contact co,
    table_gbst_elm gb,
    table_x_tt_ticket tt,
    table_case vc
    where ca.case_originator2user = u_og.objid
    and x.objid=mtm.contract2dir_sitepart
    and ca.case_currq2queue = q.objid
    and ca.CASESTS2GBST_ELM=gb.objid
    and u_og.objid = empl.employee2user
    and empl.SUPP_PERSON_OFF2SITE = office.objid
    and ca.case_reporter2site = s.objid
    and s.cust_primaddr2address = addr.objid
    and s.objid = co_r.contact_role2site
    and co_r.contact_role2contact = co.objid
    and sp.objid=mtm.dir_sitepart2contract
    and sp.all_site_part2site=s.objid
    and co_r.s_role_name = 'DEFAULT'
    and q.s_title = upper('OP_AD_WIN_TECH') 
    and ca.objid = tt.X_TT_TICKET2CASE (+)
    and tt.X_HANDLING_UNIT IS NULL
    and ca.CASE_VICTIM2CASE=vc.OBJID(+)
    and x.type not in ('UMTS','GSM','???/???','.','Time and Materials') 

结果:

      63CAGDIRO - VoIP/VDSL - No Service - All
      63CAGDIRO - VoIP/VDSL - No Service - All
      79WIIHAHG - VDSL - Quality Internet - Internet
      79WIIHAHG - VDSL - Quality Internet - Internet
      71GROGRO - VDSL - tv box error code general arg fail - TV
      71GROGRO - VDSL - tv box error code general arg fail - TV
      73LATLWHS - VDSL - No Service All - All services
      73LATLWHS - VDSL - No Service All - All services 

【问题讨论】:

  • 请在问题中添加您的查询和表结构。
  • 我已经添加了查询和结果谢谢
  • 你需要看CountGroup by
  • 我试了一下,但没有成功,你能告诉我吗?
  • 请向我们展示您到目前为止所做的尝试。

标签: php oracle


【解决方案1】:

如果您希望在多列中“转换”一个文本,您可以执行以下操作:

with 
text as (
    select '63CAGDIRO - VoIP/VDSL - No Service - All'  as txt from dual union all
    select '63CAGDIRO - VoIP/VDSL - No Service - All' from dual union all
    select '79WIIHAHG - VDSL - Quality Internet - Internet' from dual union all
    select '79WIIHAHG - VDSL - Quality Internet - Internet' from dual union all
    select '71GROGRO - VDSL - tv box error code general arg fail - TV' from dual union all
    select '71GROGRO - VDSL - tv box error code general arg fail - TV' from dual union all
    select '73LATLWHS - VDSL - No Service All - All services' from dual union all
    select '73LATLWHS - VDSL - No Service All - All services ' from dual),
pos as (
    select txt, instr(txt,'-',1,1) as p1, instr(txt,'-',1,2) as p2, 
                instr(txt,'-',1,3) as p3 from text
    )
select
     substr(t.txt, 1, p1 -1 )               col_name_1,
     substr(t.txt, p1 + 1, p2 - p1 - 2 )    col_name_2,
     substr(t.txt, p2 + 1, p3 - p2 - 2 )    col_name_3,
     substr(t.txt, p3 + 1, length(t.txt) )  col_name_4
  from text t , pos p 
  where t.txt = p.txt;

结果将是:

COL_NAME_1      COL_NAME_2      COL_NAME_3                          COL_NAME_4    
63CAGDIRO        VoIP/VDSL       No Service                          All           
63CAGDIRO        VoIP/VDSL       No Service                          All           
63CAGDIRO        VoIP/VDSL       No Service                          All           
63CAGDIRO        VoIP/VDSL       No Service                          All           
79WIIHAHG        VDSL            Quality Internet                    Internet      
79WIIHAHG        VDSL            Quality Internet                    Internet      
79WIIHAHG        VDSL            Quality Internet                    Internet      
79WIIHAHG        VDSL            Quality Internet                    Internet      
71GROGRO         VDSL            tv box error code general arg fail  TV            
71GROGRO         VDSL            tv box error code general arg fail  TV            
71GROGRO         VDSL            tv box error code general arg fail  TV            
71GROGRO         VDSL            tv box error code general arg fail  TV            
73LATLWHS        VDSL            No Service All                      All services  
73LATLWHS        VDSL            No Service All                      All services 

我注意到一个模式,在我看来,该列由“-”分隔。

【讨论】:

    【解决方案2】:

    我找到了解决办法:

    select  
    CASE
    WHEN ca.title LIKE '%Service%'   THEN 'Service'
    WHEN ca.title LIKE '%TV%' THEN 'NO TV'
    WHEN ca.title LIKE '%Quality%'  THEN 'Quality'
    WHEN ca.title LIKE '%bad%' THEN 'Bad Quality'
    END as Services,
    COUNT(*) AS group_by_count
    from ...
    GROUP BY 
    CASE
    WHEN ca.title LIKE '%Service%'   THEN 'Service'
    WHEN ca.title LIKE '%TV%' THEN 'NO TV'
    WHEN ca.title LIKE '%Quality%' THEN 'Quality'
    WHEN ca.title LIKE '%bad%' THEN 'Bad Quality'
    END
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多