【问题标题】:converting comma separated value to multiple rows将逗号分隔值转换为多行
【发布时间】:2018-08-20 10:22:45
【问题描述】:

我有一张这样的桌子:

ID NAME Dept_ID
1  a     2,3
2  b
3  c     1,2

Department 是另一个以 dept_id 和 dept_name 作为列的表。我想要这样的结果,

ID Name Dept_ID
 1   a    2
 1   a    3
 2   b
 3   c    1
 3   c    2

有什么帮助吗?

【问题讨论】:

标签: sql oracle split


【解决方案1】:

你可以这样做:

--Dataset Preparation 
with tab(ID, NAME,Dept_ID) as (Select 1, 'a', '2,3' from dual
                               UNION ALL
                               Select  2,  'b','' from dual
                               UNION ALL
                               Select 3,  'c' ,  '1,2' from dual)      
--Actual Query                      
select distinct ID, NAME, regexp_substr(DEPT_ID,'[^,]+', 1, level) 
from tab    
connect by  regexp_substr(DEPT_ID,'[^,]+', 1, level) is not null
order by 1;

编辑:

我需要加入哪一列?在一张桌子上我有逗号 分开的 ids 和在其他表中我只有 ids

with tab(ID, NAME,Dept_ID) as (Select 1, 'a', '2,3' from dual
                               UNION ALL
                               Select  2,  'b','' from dual
                               UNION ALL
                               Select 3,  'c' ,  '1,2' from dual) ,
      --Table Dept
      tbl_dept (dep_id,depname) as ( Select 1,'depa' from dual
                                       UNION ALL
                                      Select 2,'depb' from dual 
                                      UNION ALL
                                      Select 3,'depc' from dual      
                                    ) ,      
       --Seperating col values for join. Start your query from here using with clause since you already have the two tables.                            
       tab_1 as (select distinct ID, NAME, regexp_substr(DEPT_ID,'[^,]+', 1, level) col3 
                from tab  
                connect by  regexp_substr(DEPT_ID,'[^,]+', 1, level) is not null
                order by 1)
--Joining table.                
Select t.id,t.name,t.col3,dt.depname
from tab_1 t
left outer join tbl_dept dt
on t.col3 = dt.dep_id
order by 1

【讨论】:

  • 如果我有 1000+ 行,那么如何准备数据集?
  • @learner1 你不需要准备数据集。我准备它只是为了给你看。您可以直接使用标记为Actaul Query的查询。
  • 嘿,我必须再做一个更改,而不是 dept_id 我想显示 tbl_dept 表中相应 id 的部门名称,这可能吗?
  • Yrs..这是可能的。与表建立连接并在您的选择语句中添加所需的列。
  • 我需要加入哪一栏?在一个表中,我有逗号分隔的 id,而在另一张表中,我只有 id
【解决方案2】:
with tmp_tbl as(
  select
    1 ID,
    'a' NAME,
    '2,3' DEPT_ID
  from dual
  union all
  select
    2 ID,
    'b' NAME,
    '' DEPT_ID
  from dual
  union all
  select
    3 ID,
    'c' NAME,
    '1,2' DEPT_ID
  from dual)
select
  tmp_out.ID,
  tmp_out.NAME,
  trim(tmp_out.DEPT_ID_splited)
from(
  select
    tmp.ID,
    tmp.NAME,
    regexp_substr(tmp.DEPT_ID,'[^,]+', 1, level) DEPT_ID_splited
  from
    tmp_tbl tmp
  connect by
    regexp_substr(tmp.DEPT_ID,'[^,]+', 1, level) is not null) tmp_out
group by
  tmp_out.ID,
  tmp_out.NAME,
  tmp_out.DEPT_ID_splited
order by
  tmp_out.ID,
  tmp_out.DEPT_ID_splited

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多