Home » SQL & PL/SQL » SQL & PL/SQL » split data with levels (11.2.0.3)
split data with levels [message #643519] Fri, 09 October 2015 13:22 Go to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Hi All,

I have created a table as follows.


create table user_info
(id varchar2(15),
 entity_name varchar2(1000),
);



I have a sequence as follows

create sequence user_seq
start with 1
increment by 1
minvalue 1
maxvalue 9999
[/code]

i have inserted a data as follows.

ID            Entity_Name
-------------------------
1              State1
2              State1, Dist1
3              state2, Dist1, Vlg1
4              State3, Dist2, Vilg2
5              State3, Dist2, Vilg3
6              State4, Dist3
7              ,Dist4
8              , , VIlg5
9              , dist5
10             , , US
11             UK
12             UAE,IND
13              , PAK
...
      



Here we can observe there are 3 string values which are separated by ','
i.e 1st value,2nd value,3rd Value.
some where we can see , ,vilg1 which means here 1st string is null and 2d string is null and 3rd string values is Vilg1.

so here we can say STR1 is Parent of Str2 abd Str2 is parent of Str3

We need to define the levels and need the output for above data as follows.

Expected O/P
ID            Entity_Name    LVL
----------------------------------
1              State1         0
2              State1         0
3              Dist1          1
4              state2         0
5              Dist1          1
6              Vlg1           2
7              State3         0
8              Dist2          1
9              Vilg2          2
10             State3         0
11             Dist2          1
12             Vilg3          2
13             State4         0
14             Dist3          1
15             Dist4          1
16             null 
17             null
18             VIlg5          2
19             null
20             dist5          1
21             null
22             US             1
23             UK             0
24             UAE            0
25             IND            1
26             null
27             PAK            0
...
..


Need to split the comma separated string --, Value1, Value2,Value3
value1 as Parent to Value2
value 2 is parent to Value3
if the value is like" ,value2" that means Value1 is null and Value2 level is 1

if the values is like ", ,value3" then need to split the value as "null
null
value3 and the level of value3 is 2"

Can any one advise query to fulfill this requiremnt.

Thanks,
Amy


Re: split data with levels [message #643520 is a reply to message #643519] Fri, 09 October 2015 13:39 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
i have inserted a data as follows.


And we can't without the INSERT statements you did not post.

Quote:
Here we can observe there are 3 string values which are separated by ','
i.e 1st value,2nd value,3rd Value.
...
so here we can say STR1 is Parent of Str2 abd Str2 is parent of Str3


What are STR1, Str2 and Str3?

Quote:
Expected O/P


Explain where ID comes from.

Solution is there and subsequent posts.

Remember: With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Re: split data with levels [message #643521 is a reply to message #643520] Fri, 09 October 2015 13:51 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Str1,STr2,Str3 are like Value1,value2,value3.

Please find below Insert statements.

Insert into user_info values('1','State1');
Insert into user_info values('2',"State1, Dist1");
Insert into user_info values('3',"state2, Dist1, Vlg1");
Insert into user_info values('4',"State3, Dist2, Vilg2");
Insert into user_info values('5',"State3, Dist2, Vilg3");
Insert into user_info values('6',"State4, Dist3");
Insert into user_info values('7'," ,Dist4");
Insert into user_info values('8'," , , VIlg5");
Insert into user_info values('9'," , dist5");
Insert into user_info values('10'," , , US");
Insert into user_info values('11',"UK"");
Insert into user_info values('12',"UAE,IND");
Insert into user_info values('13',", PAK");
Re: split data with levels [message #643522 is a reply to message #643521] Fri, 09 October 2015 13:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Try to do it with the solution I pointed you to.

Re: split data with levels [message #643523 is a reply to message #643521] Fri, 09 October 2015 13:54 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
I have written a query to split the strings. But I didn't the logic how can I define the levels?

WITH DATA AS
      ( SELECT entity_name FROM user_info
    )
  SELECT trim(regexp_substr(entity_name, '[^,]+', 1, LEVEL)) entity_name
  FROM DATA
 CONNECT BY instr(entity_name, ',', 1, LEVEL - 1) > 0



can you guide me how to defin the levels.

Re: split data with levels [message #643524 is a reply to message #643523] Fri, 09 October 2015 13:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Michel Cadot wrote on Fri, 09 October 2015 20:53

Try to do it with the solution I pointed you to.

Re: split data with levels [message #643525 is a reply to message #643524] Fri, 09 October 2015 14:19 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
It looks good Michel,

          A
       /     \
      B       C
    /   \   / | \
    D    E  F  G  H
         |
         I



from the above pictorial view I would like to see the data
as


Father       SON    
====================
A             B
A             C
B             D
B             E
C             F
C             G
C             H
I             E


in this my expected O/P is


Name         Level
==================
A             0
B             1
C             1
D             2
E             2
F             2
G             2 
H             2
I             3


and one more here is , If I is CHILD of E and F then

my required O/P is as follows.


Name         Level
==================
A             0
B             1
C             1
D             2
E             2
F             2
G             2 
H             2
I             I_E
I             I_F



Please let me know how can we write a query for this

Thanks
Re: split data with levels [message #643526 is a reply to message #643525] Fri, 09 October 2015 14:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

This new requirements have nothing to do with the first ones.
So think more about them, I bet there will be more changes in a while.

[Updated on: Fri, 09 October 2015 14:24]

Report message to a moderator

Re: split data with levels [message #643527 is a reply to message #643526] Fri, 09 October 2015 14:37 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Smile

No Michel.

This is the only change I require...

advise me a query to satisfy the requirement.

Thanks
Re: split data with levels [message #643533 is a reply to message #643527] Sat, 10 October 2015 01:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Post matching test case.

Note that in your graph I has only one parent.
Note in your original question a value can be repeated in the result not in your last one.
What are the actual requirements?

Restart to explain ALL the requirements from the beginning with a representing test case with an explanation of ALL columns of ALL lines in result.

Re: split data with levels [message #643573 is a reply to message #643533] Mon, 12 October 2015 08:04 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Michel,

Here am posting complete structure. Please have a look and advise.

I have a table called USER_PARENT and the structure is as follows.
 
create table user_parent
(
divname   varchar2(30),
regionName  varchar2(30),
Teamname varchar2(30)
);
 


We are inserting the data as follows.

insert into user_parent values('D1',R1','T1');
insert into user_parent values('D1',R1','T2');
insert into user_parent values('D1',R2','T3');
insert into user_parent values('D1',R2','T4');
insert into user_parent values('D2',R3','T5');
insert into user_parent values('D3',R6','T9');
insert into user_parent values('D4',R6','T10');
...
...



now the in the USER_PARENt as follows
DIVNAME    RegionName    TeamName
------------------------------------------------------------
D1                   R1                   T1
D1                   R1                   T2
D1                   R2                    T3
D1                   R2                    T4
D2                   R3                    T5
D3                   R6                    T9
D4                   R6                    T10
...
....


Now I have created a table as follows.
Create table Parent_Info
(
id   varchar2(20),
NAME varchar2(30),
Parent_ID     varchar2(30)
);


create sequence parent_info_seq
start with 1
increment by 1
minvalue 1
maxvalue 999999


Expected O/P in Parent_Info is as follows. ID values will be inserted from "parent_info_seq.nextval)
ID    NAME              Parent
=====================
1       D1                     0
2       D2                     0
3       D3                     0
4       D4                     0
5       R1                     1 ( R1 has parent D1 and D1's ID is 1 here ..)
6       R2                     1 ( R2 has parent D1 and D1's ID is 1 here..)
7       R3                     2 ( R3 has parent D2 and D2's ID is 2 here)
8       R6                     R6_D3 ( R6 has two parents --> D3 and D4. so to define the difference we need to mention R6 _ ( respective parent)
9       R6                     R6_D4 ( R6 has two parents --> D3 and D4. so to define the difference we need to mention R6 _ ( respective parent)
10     T1                     5  ( T1 has Parent R1 and R1 ID value is 5 here)
11     T2                     5  ( T2 has parent R1 and R1 ID value is 5 Here)
12      T3                    6 ( T3 has parent R2 and R2 ID value is 6 here)
13      T4                    6   (T4 has parent R2 and R2 ID value is 6 here)
14      T5                    7 ( T5 has parent R3 and the ID of R3 is 7 here)
15      T9                    T9_R6 ( T9 has parent R6. so define the difference need to mention T9_ (respective parent)
16       T10                  T10_R6 ( T10 has parent R6. So to make the difference need to mention T10_ respective parent)
......
...
....


Please advise me a query to retrive data as above..



CM: fixed a code tag

[Updated on: Mon, 12 October 2015 08:06] by Moderator

Report message to a moderator

Re: split data with levels [message #643578 is a reply to message #643573] Mon, 12 October 2015 13:15 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Hi All,

Can any one please advise me a query ..

Re: split data with levels [message #643582 is a reply to message #643573] Mon, 12 October 2015 14:19 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
All,


can any one advise me either a Procedure/Function/ to satisfy the above requirement.

Thanks,
Amy.
Re: split data with levels [message #643608 is a reply to message #643582] Tue, 13 October 2015 08:54 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Hi Experts,

Please advise me the query/procedure/function for the above given requirement.

Thanks,
Amy.
Re: split data with levels [message #643609 is a reply to message #643573] Tue, 13 October 2015 09:10 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
Hi Amy,

can you please make any feedback on your previous thread(s), especially this one http://www.orafaq.com/forum/t/198793/? Or, was it just a waste of time as your real problem differs from the posted one?

I wonder whether this is not also the same case, although this time you came with data which do not have hierarchy in one way and with the table structure of PARENT_TABLE table, in which it would be really tough to make reasonable queries. Anyway, if you wish to fill it in this way, I would use three INSERT statements. Here are the first two:
-- Insert the rows based on DIVNAME column
insert into parent_info ( name, parent_id )
select distinct divname, '0'
from user_parent;

-- Insert the rows based on REGIONNAME column
insert into parent_info ( name, parent_id )
select distinct name, parent_id
from ( select up.regionname name,
         case when count( distinct pi.id ) over ( partition by up.regionname ) = 1
              then pi.id
              else up.regionname||'_'||up.divname
         end parent_id
       from user_parent up
         inner join parent_info pi on pi.name = up.divname);

I will let the last one on you - should be just an analogy of the second one.

(supposing that the values in USER_PARENT are not the same in different columns - any two of DIVNAME, REGIONNAME, TEAMNAME columns are without intersecting values)

Good luck with querying PARENT_TABLE data (I hope it was designed for direct reports of its rows only and you will not have to transform its content in any way).
Re: split data with levels [message #643613 is a reply to message #643609] Tue, 13 October 2015 12:57 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Hi ,

Thanks for your response.

1st query is returning the records as expected. But the 2nd query is nor working and not returning any records.

Please advise.
Re: split data with levels [message #643627 is a reply to message #643613] Wed, 14 October 2015 01:53 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
Hi,

What "returning the records as expected" exactly means? Did you run the INSERT statement in whole (I suppose that ID column is filled by trigger from the posted sequence) and checked the PARENT_INFO content afterwards or did you just run the SELECT query alone?

The second query depends on the PARENT_INFO table content filled by the first query, so if nothing was inserted into by the first query, nothing can be fetched by the second query (and it would be a real challenge to pick up autogenerated IDs without actually having rows where they are stored).
Re: split data with levels [message #643641 is a reply to message #643627] Wed, 14 October 2015 12:44 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Hi,

I am getting the O/P as follows.



from the 2nd query 
In the parent_ID column it is displaying regionname||'_'||up.divname value ( whihc is character value). 
But here in this column it needs to display ID value ( which is number) of the DIVNAME..

Can you have a look and let me know where do I need to do the modification.



Re: split data with levels [message #643652 is a reply to message #643641] Thu, 15 October 2015 02:18 Go to previous message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
I am quite lost, as in your post #643573, both ID and PARENT_ID columns of the PARENT_INFO table are declared with VARCHAR2 data type. The expected result in the same post below contains "R6_D3" in the last column. So now you want something different?

If you want to have always "PI.ID" and not "UP.REGIONNAME||'_'||UP.DIVNAME" in the PARENT_ID column, just change the expression in the query to the required one (you found it there, so what is problem with replacing it? - most probably "PI.ID" instead of the whole CASE expression).

By the way, you still did not feedback to your previous thread here: http://www.orafaq.com/forum/t/198793/. According to constantly changing requirements, I am afraid it will never end. Maybe someone else will endure following them.

Good luck.
Previous Topic: fine tuning my sql query for a grouping report
Next Topic: I need to process a string with arithmetic operators
Goto Forum:
  


Current Time: Thu Apr 25 18:29:47 CDT 2024