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 |
|
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 #643521 is a reply to message #643520] |
Fri, 09 October 2015 13:51 |
|
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 #643573 is a reply to message #643533] |
Mon, 12 October 2015 08:04 |
|
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 #643609 is a reply to message #643573] |
Tue, 13 October 2015 09:10 |
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 #643627 is a reply to message #643613] |
Wed, 14 October 2015 01:53 |
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 #643652 is a reply to message #643641] |
Thu, 15 October 2015 02:18 |
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.
|
|
|
Goto Forum:
Current Time: Thu Apr 25 18:29:47 CDT 2024
|