Home » SQL & PL/SQL » SQL & PL/SQL » CTE Queries (11g)
CTE Queries [message #643154] |
Wed, 30 September 2015 11:07 |
|
amy.wilson
Messages: 39 Registered: September 2015 Location: Www
|
Member |
|
|
Hello everyone,
I have a table as follows
CREATE table INFORMATION
(
ID number(8) not null,
DIVISIONMGR VARCHAR2(20),
REGIONMGR VARCHAR2(20),
LOCALMGR VARCHAR2(20)
);
insert into INFORMATION values ('1', 'DIVISIONMGR1','RegionMGR1','LOCALMGR1');
insert into INFORMATION values ('2', 'DIVISIONMGR1','RegionMGR1','LOCALMGR2');
insert into INFORMATION values ('3', 'DIVISIONMGR1','RegionMGR1','LOCALMGR3');
insert into INFORMATION values ('4', 'DIVISIONMGR2','REGIONMGR2','LOCALMGR4');
insert into INFORMATION values ('5', 'DIVISIONMGR2','RegionMGR2','LOCALMGR5');
insert into INFORMATION values ('6', 'DIVISIONMGR2','RegionMGR2','LOCALMGR3');
insert into INFORMATION values ('7', 'DIVISIONMGR2','REGIONMGR3','LOCALMGR6');
....
....
NOTE :
1. Each Division has some regions
2. Each region has some LocalTeams
so relation is like
DIVISIONMGR1 --- RegionMGR1--- LOCALMGR1
--- LOCALMGR2
--- LOCALMGR3
DIVISIONMGR2 ---- REGIONMGR2 ---- LOCALMGR4
---- LOCALMGR5
---- LOCALMGR3 ( -- Here some times a LOCALMGR can be belongs to two different REGIONMGR's)
---- REGIONMGR3 --- LOCALMGR6
--- LOCALMGR5
--- LOCALMGR7
Like this I have 'N' number of DIVISIONS , REGIONS and LOCALTEAMS.
Now I would like to write a query, to calculate LEVELS for all DIVISIONS and REGIONS and LOCALTEAMS.
Please have a look into the above requirement and help me to resolve the above issue.
Thanks,
Amy.
*BlackSwan corrected {code} tags
[MC: Corrected once more, OP has modified his post after BlackSwan]
[Updated on: Wed, 30 September 2015 12:12] by Moderator Report message to a moderator
|
|
|
|
Re: CTE Queries [message #643157 is a reply to message #643155] |
Wed, 30 September 2015 12:51 |
|
amy.wilson
Messages: 39 Registered: September 2015 Location: Www
|
Member |
|
|
Here is the pictorial view of the table
ID DIVISIONMGR REGIONMGR LOCALMGR
--------------------------------------------------
1 DIVISIONMGR1 RegionMGR1 LOCALMGR1
2 DIVISIONMGR1 RegionMGR1 LOCALMGR2
3 DIVISIONMGR1 RegionMGR1 LOCALMGR3
4 DIVISIONMGR2 REGIONMGR2 LOCALMGR4
5 DIVISIONMGR2 REGIONMGR2 LOCALMGR5
6 DIVISIONMGR2 REGIONMGR2 LOCALMGR3 (( -- Here some times a LOCALMGR can be belongs to two different REGIONMGR's)
7 DIVISIONMGR5 REGIONMGR3 LOCALMGR6
8 DIVISIONMGR5 REGIONMGR3 LOCALMGR5
9 DIVISIONMGR5 REGIONMGR3 LOCALMGR7
....
...
LEVELS are nothing but LEVEL of the Employee.
for Example if we observe the above structure,
DIVISIONMGR1 and DIVISIONMGR2 and DIVISIONMGR3 etc... are parents. So their LEVEL is '0'
Like this , I would like to know how can we define the value of REGIONMGR1,REGIONMGR2 etc..
and LOCALMGR1,LOCALMGR2,LOCALMGR3...
like PARENT to CHILD
CHILD to GRAND CHILD..
SO can any one define which value and how can we define a values as LEVEL to CHILD and GRANDCHILD.
My Expected Output is as follows.
I am not sure how to define a LEVEL if same LOCALMGR belongs to SAME REGIONMGR's. Please advise.
ID DIVISIONMGR LEVEL ( Here I need values for Level. As I don't know how get.. just mention null for all)
----------------------------------------------------------
1 DIVISIONMGR1
2 DIVISIONMGR1
3 DIVISIONMGR1
4 DIVISIONMGR2
5 DIVISIONMGR2
6 DIVISIONMGR2
7 DIVISIONMGR5
8 DIVISIONMGR5
9 DIVISIONMGR5
10 RegionMGR1
11 RegionMGR1
12 RegionMGR1
13 REGIONMGR2
14 REGIONMGR2
15 REGIONMGR2
16 REGIONMGR3
17 REGIONMGR3
18 REGIONMGR3
19 LOCALMGR1
20 LOCALMGR2
21 LOCALMGR3
22 LOCALMGR5
23 LOCALMGR4
24 LOCALMGR7
25 LOCALMGR5
26 LOCALMGR6
27 LOCALMGR3
....
...
Please let me know if you need more information.
Thanks,
Amy
[Updated on: Wed, 30 September 2015 13:01] Report message to a moderator
|
|
|
|
Re: CTE Queries [message #643159 is a reply to message #643158] |
Wed, 30 September 2015 13:25 |
|
amy.wilson
Messages: 39 Registered: September 2015 Location: Www
|
Member |
|
|
I have created a sequence for ID values.
create sequence inf_sq
start with 1
increment by 1
minvalue 1
maxvalue 2000;
at the time of Table creation
CREATE table INFORMATION
(
ID number(8) default inf_seq.nextval,
DIVISIONMGR VARCHAR2(20),
REGIONMGR VARCHAR2(20),
LOCALMGR VARCHAR2(20)
);
I would like to calculate the LEVEL of each employee for the below Given data start from DIVISIONMGR1 to LOCALMGRn
The above given O/p should be as below
ID MGR LEVEL
--------------------- ( by mistake I mentioned DIVISIONMGR)
with data(ID,MGR)
as ( select id,manager from <tab> t1
---
not getting logic to write the query...
advise please.
[Updated on: Wed, 30 September 2015 13:27] Report message to a moderator
|
|
|
|
|
|
Re: CTE Queries [message #643237 is a reply to message #643164] |
Thu, 01 October 2015 08:41 |
|
amy.wilson
Messages: 39 Registered: September 2015 Location: Www
|
Member |
|
|
Can you please ignore above one and sugegst me how can we define LEVEL to
PARENT-->CHILDRENS
|
|
GRANDCHILDREN
I would like to write a query from Lower level to HIGH level first?
and then how can we write a query if we have a grand children who belongs to same Parent/Child
Please advise. As am new to Oracle I don't have any idea to write a ROOT/PATH queries.
[Updated on: Thu, 01 October 2015 08:41] Report message to a moderator
|
|
|
|
|
|
|
Re: CTE Queries [message #643251 is a reply to message #643250] |
Thu, 01 October 2015 13:43 |
|
amy.wilson
Messages: 39 Registered: September 2015 Location: Www
|
Member |
|
|
If I have data as follows. I just gave this tree for understanding purpose. I have many parents and have their children and their Grand children too.
I would like to define LEVEL from top to bottom or bottom to TOP. Please advice.
5 3
| |
| |
------------ ------------------------
| | | |
| | | |
7 2 1 10
| |
| |
6 --------------
| | |
| | |
8 9 4
If we observe the above picture,
5 and 3 are Parents
5 has 2 children i.e 7 and 2
3 has 2 children ie. 1 and 10
1 has one grand children i.e 1
10 has 3 grand children i.e 8,9,4
the expected O/P is :
+----+-----------+
| id | parent_id |
+----+-----------+
| 1 | 3 |
| 2 | 5 |
| 3 | 0 |
| 4 | 10 |
| 5 | 0 |
| 6 | 1 |
| 7 | 5 |
| 8 | 10 |
| 9 | 10 |
| 10 | 3 |
+----+-----------+
I have the above tree for around 1 million hierarchical records. So the query should work dynamically all parent--> Child and Child--> parent automaticcaly.
So not looking to hard code as below
WITH DATA (NAME,id)
AS (select '5',1 from dual union all
select '3',2 from dual union all
select '<>',3 from dual union all
select '<>',4 from dual union all
select '<>',5 from dual union all
etc..
Please advise any query like 'CTE' ( sql server)
The query should work all Parents and Chidlren and Grand Childrens. please have a look and advise.
[Updated on: Thu, 01 October 2015 13:48] Report message to a moderator
|
|
|
|
Re: CTE Queries [message #643253 is a reply to message #643251] |
Thu, 01 October 2015 13:51 |
gazzag
Messages: 1118 Registered: November 2010 Location: Bedwas, UK
|
Senior Member |
|
|
It appears that CTE depends on your Oracle version.
What does the following query return?
SQL> SELECT * FROM v$version;
|
|
|
|
|
|
Re: CTE Queries [message #643257 is a reply to message #643254] |
Thu, 01 October 2015 14:13 |
gazzag
Messages: 1118 Registered: November 2010 Location: Bedwas, UK
|
Senior Member |
|
|
Quote:I am not interested in writing the code which is hard coding.
No test case, no answer I'm afraid.
|
|
|
Re: CTE Queries [message #643258 is a reply to message #643256] |
Thu, 01 October 2015 14:27 |
|
amy.wilson
Messages: 39 Registered: September 2015 Location: Www
|
Member |
|
|
actual data :
I have existed data asfollows.
ID Name
-------------------
| a1 | 1 |
| b2 | 2 |
| c3 | 3 |
| a1.d4 | 4 |
| a1.e5 | 5 |
| a1.d4.f6 | 6 |
| a1.d4.g7 | 7 |
| a1.e5.h8 | 8 |
| a2.i9 | 9 |
| a2.i9.j10| 10 |
etc..
etc...
Expected O/P :
| Name | ID | PARENT_ID |
-----------------------------
| a1 | 1 | null |
| b2 | 2 | null |
| c3 | 3 | null |
| d4 | 4 | 1 |
| e5 | 5 | 1 |
| f6 | 6 | 4 |
| g7 | 7 | 4 |
| h8 | 8 | 5 |
| i9 | 9 | 2 |
| j10 | 10 | 9 |
...
---
code which we have written which is hardcodes for Individual Names.
WITH DATA (NAME,id)
AS (select 'a1',1 from dual union all
select 'b2',2 from dual union all
select 'c3',3 from dual union all
select 'a1.d4',4 from dual union all
select 'a1.e5',5 from dual union all
select 'a1.d4.f6',6 from dual union all
select 'a1.d4.g7',7 from dual union all
select 'a1.e5.h8',8 from dual union all
select 'a2.i9',9 from dual union all
SELECT 'a2.i9.j10',10 FROM dual)
,relation AS (SELECT NAME, substr(NAME,instr(NAME,'.',-1)+1) stripped_name
, substr(name,instr(name,'.',-1,2) + 1, instr(name,'.',-1) - instr(name,'.',-1,2) - 1) AS PARENT_name
, id
FROM DATA)
SELECT CHILD.NAME
,CHILD.ID
,parent.id as parent_id
FROM relation CHILD
LEFT OUTER JOIN relation PARENT ON (CHILD.parent_name = PARENT.stripped_name)
order by child.id;
|
|
|
Re: CTE Queries [message #643262 is a reply to message #643258] |
Fri, 02 October 2015 01:39 |
_jum
Messages: 577 Registered: February 2008
|
Senior Member |
|
|
Here is (nearly) Your code written as CTE:
WITH data (name,id) AS
(SELECT 'a1',1 FROM dual UNION ALL
SELECT 'a2',2 FROM dual UNION ALL
SELECT 'c3',3 FROM dual UNION ALL
SELECT 'a1.d4',4 FROM dual UNION ALL
SELECT 'a1.e5',5 FROM dual UNION ALL
SELECT 'a1.d4.f6',6 FROM dual UNION ALL
SELECT 'a1.d4.g7',7 FROM dual UNION ALL
SELECT 'a1.e5.h8',8 FROM dual UNION ALL
SELECT 'a2.i9',9 FROM dual UNION ALL
SELECT 'a2.i9.j10',10 FROM dual UNION ALL
SELECT 'a1.e5.h8.k11',11 FROM dual
)
,relation AS
(SELECT name, substr(name,instr(name,'.',-1)+1) stripped_name,
substr(name,instr(name,'.',-1,2) + 1, instr(name,'.',-1) - instr(name,'.',-1,2) - 1) parent_name,
id
FROM data)
,ancestors (aname, aid) AS
(SELECT name, id
FROM data
WHERE INSTR(name,'.')=0),
cte ( name, aname, parent_name, child_name, id, lv) AS
(SELECT aname, aname, '-', aname, aid, 1
FROM ancestors
UNION ALL
SELECT rel.name, aname, ct.child_name, rel.stripped_name, rel.id, lv+1
FROM relation rel
JOIN cte ct ON (ct.child_name = rel.parent_name ))
SELECT *
FROM cte;
NAME ANAME PARENT_NAME CHILD_NAME ID LV
----------------------------------------------------------
a1 a1 - a1 1 1
a2 a2 - a2 2 1
c3 c3 - c3 3 1
a1.e5 a1 a1 e5 5 2
a1.d4 a1 a1 d4 4 2
a2.i9 a2 a2 i9 9 2
a1.e5.h8 a1 e5 h8 8 3
a1.d4.g7 a1 d4 g7 7 3
a1.d4.f6 a1 d4 f6 6 3
a2.i9.j10 a2 i9 j10 10 3
a1.e5.h8.k11 a1 h8 k11 11 4
|
|
|
Re: CTE Queries [message #643272 is a reply to message #643262] |
Fri, 02 October 2015 08:18 |
|
amy.wilson
Messages: 39 Registered: September 2015 Location: Www
|
Member |
|
|
Hi,
the below one looks hard coded. Can you advise me how can we write your given code wich will work for all 500k records for parents,child and grand child. Please advise.
WITH data (name,id) AS
(SELECT 'a1',1 FROM dual UNION ALL
SELECT 'a2',2 FROM dual UNION ALL
SELECT 'c3',3 FROM dual UNION ALL
SELECT 'a1.d4',4 FROM dual UNION ALL
SELECT 'a1.e5',5 FROM dual UNION ALL
SELECT 'a1.d4.f6',6 FROM dual UNION ALL
SELECT 'a1.d4.g7',7 FROM dual UNION ALL
SELECT 'a1.e5.h8',8 FROM dual UNION ALL
SELECT 'a2.i9',9 FROM dual UNION ALL
SELECT 'a2.i9.j10',10 FROM dual UNION ALL
SELECT 'a1.e5.h8.k11',11 FROM dual
)
,relation as ...
Thanks
|
|
|
|
|
|
Re: CTE Queries [message #643333 is a reply to message #643288] |
Mon, 05 October 2015 08:01 |
|
amy.wilson
Messages: 39 Registered: September 2015 Location: Www
|
Member |
|
|
I will replace DATA with my table name.
As the table contains many values, we can't write the query
SELECT 'a1',1 FROM dual UNION ALL
SELECT 'a2',2 FROM dual UNION ALL
SELECT 'c3',3 FROM dual UNION ALL
SELECT 'a1.d4',4 FROM dual UNION ALL
select ...
for all values..
In this case how can we write the above same query which will applicable to all 500k records?
because I have a table as " CONTACTS".
in this DIV_MANAGER is the PARENT COLUMN and REG_MANAGER is CHILD Column and TEAM_MANAGER is GRAND CHILD COlumn.
Could you please advise the query by using above PARENT and CHILD and GRANDCHILD?
[Updated on: Mon, 05 October 2015 08:09] Report message to a moderator
|
|
|
|
|
|
Re: CTE Queries [message #643339 is a reply to message #643336] |
Mon, 05 October 2015 09:29 |
|
amy.wilson
Messages: 39 Registered: September 2015 Location: Www
|
Member |
|
|
(select row_number() over (order by id) rn,
id,
parent_id
from data
start with parent_id is null
connect by prior id = parent_id
)
select id name,
rn id,
(select rn from base where id = x.parent_id) "LEVEL"
from base x
order by rn
Can you please advise how can we mention Parent column and Child column in the above query..
CM: Fixed code tags, end tag should be [/code]
[Updated on: Mon, 05 October 2015 10:41] by Moderator Report message to a moderator
|
|
|
|
Re: CTE Queries [message #643343 is a reply to message #643341] |
Mon, 05 October 2015 11:02 |
|
amy.wilson
Messages: 39 Registered: September 2015 Location: Www
|
Member |
|
|
This query
represents the contacts table hierarchy that is represented for the given employee
We need to check to see if the primarys mapped to more than one position.Can be used to build the Hierarchy.
I would like to find the hierarchy of all employees either from PARENT - Child or CHILD to Parent.
Here is the query and some sample data. Please have a look and let me know the query to retrive the hierachy.
select distinct
DIV_NAME,
DIV_MGR,
REGI_NAME,
REGI_MGR,
TEAM_NAME,
TEAM_MGR,
PRIM_REPR,
fst_name,
last_name,
sp.name,
login,
count(*) over (partition by sc.row_id) dup_Positions
from CONTACT sc
left outer join POSITION sp on sp.pr_emp_id=sc.row_id
where PRIM_REPR is not null
or DIV_MGR is not null
or DIV_NAME is not null
or REGI_NAME is not null
or REGI_MGR is not null
or TEAM_NAME is not null
or TEAM_MGR is not null
order by
DIV_NAME,
DIV_MGR,
REGI_NAME,
REGI_MGR,
TEAM_NAME,
TEAM_MGR,
PRIM_REPR,
last_name
from the above will get the data for all DIV_MGR's, REGI_MGR's and TEAM_MGR infomation.
Now I would like to write a query to see the hierarchy of all employees wither from PARENT - CHILD or ChILD - PARENT LEvel.
Can any one please advise.
Thanks,
Amy
|
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Apr 26 06:41:24 CDT 2024
|