1

I have a table like this:

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

Department is another table having dept_id and dept_name as columns. i want result like,

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

any help please?

William Robertson
  • 15,273
  • 4
  • 38
  • 44
learner1
  • 116
  • 1
  • 15
  • 1
    Possible duplicate of [Splitting string into multiple rows in Oracle](https://stackoverflow.com/questions/14328621/splitting-string-into-multiple-rows-in-oracle) –  Aug 20 '18 at 11:02

2 Answers2

1
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
Ychdziu
  • 435
  • 5
  • 10
1

You can do it as:

--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;

Edit:

based on which column i need to join? in one table i have comma separated ids and in other table i have just 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
XING
  • 9,608
  • 4
  • 22
  • 38
  • what if i have 1000+ rows, then how to prepare dataset? – learner1 Aug 20 '18 at 11:31
  • @learner1 You need not have to prepare dataset. I prepared it just to show you. You can directly use query which is marked as `Actaul Query`. – XING Aug 20 '18 at 11:47
  • hey i have to make one more change, instead of dept_id I want to display name of the department of corresponding id from tbl_dept table, is that possible? – learner1 Aug 20 '18 at 12:56
  • Yrs..thats possible. Make a join with the table and add the required column in your select statement. – XING Aug 20 '18 at 14:24
  • based on which column i need to join? in one table i have comma separated ids and in other table i have just ids – learner1 Aug 21 '18 at 06:14