Home » SQL & PL/SQL » SQL & PL/SQL » CTE Queries (11g)
CTE Queries [message #643154] Wed, 30 September 2015 11:07 Go to next message
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 #643155 is a reply to message #643154] Wed, 30 September 2015 12:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Welcome to the forum.
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

Please post a complete test case for the output you showed us.
Also post the result you want from these data.
What does means "calculate LEVELS for all DIVISIONS and REGIONS and LOCALTEAMS"? What is "LEVELS" there?

Re: CTE Queries [message #643157 is a reply to message #643155] Wed, 30 September 2015 12:51 Go to previous messageGo to next message
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 #643158 is a reply to message #643157] Wed, 30 September 2015 13:07 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

We cannot test our queries without a complete test case so please post it.

Quote:
I am not sure how to define a LEVEL if same LOCALMGR belongs to SAME REGIONMGR's. Please advise.


If you don't know then how could you estimate if one query returns the correct result or not?

How the rows are ordered? What is the criteria for/definition of ID?

As far as I can see you want the result set containing all values from all columns.
The 2 other columns (ID and LEVEL) have to be specified if you want some result.
Without a specification, you cannot have any query.

Re: CTE Queries [message #643159 is a reply to message #643158] Wed, 30 September 2015 13:25 Go to previous messageGo to next message
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 #643160 is a reply to message #643159] Wed, 30 September 2015 13:30 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So post the statements for the new test case and its result.
Note that "default inf_seq.nextval" is invalid.

And if you still don't specify LEVEL then there is no chance you or anyone will be able to write a query.

Re: CTE Queries [message #643163 is a reply to message #643160] Wed, 30 September 2015 13:51 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Please find the expected result.

For understanding purpose I just showed you adding sequence in the table. I know that is Invalid..



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
 




ID    DIVISIONMGR      LEVEL 
----------------------------------------------------------
1     DIVISIONMGR1      0
2     DIVISIONMGR1      0
3     DIVISIONMGR1      0
4     DIVISIONMGR2      0
5     DIVISIONMGR2      0
6     DIVISIONMGR2      0                 
7     DIVISIONMGR5      0
8     DIVISIONMGR5      0
9     DIVISIONMGR5      0
10    RegionMGR1        1  ( As REGIONMGR1 is belongs to DIVISIONmgr1. here DIVISIONMGR1 'ID' is '1'from ID columns)
11    RegionMGR1        1
12    RegionMGR1        1
13    REGIONMGR2        4  ( As RegionMgr2 is belongs to DIvisionmgr2. But the ID of DIVISIONMGR2 is '4'from ID columns)
14    REGIONMGR2        4
15    REGIONMGR2        4
16    REGIONMGR3        7  ( As RegionMgr3 is belongs to DIvisionmgr3. But the ID of DIVISIONMGR3 is '7'from ID columns)
17    REGIONMGR3        7
18    REGIONMGR3        7
19    LOCALMGR1         10 ( As LOCALMGR1 belongs to REGIONMGR1 and ID of Regionmgr1 is '10' from ID columns)
20    LOCALMGR2         10
21    LOCALMGR3         10
22    LOCALMGR5         13 ( LOCALMGR5 belongs to REGIONMGR2 and ID of REGINMGR2 is '13' from ID column) the logic follows for all..
23    LOCALMGR4     
24    LOCALMGR7      
25    LOCALMGR5       
26    LOCALMGR6     
27    LOCALMGR3

....
...


Re: CTE Queries [message #643164 is a reply to message #643163] Wed, 30 September 2015 13:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
As REGIONMGR1 is belongs to DIVISIONmgr1. here DIVISIONMGR1 'ID' is '1'from ID columns)


And 2 and 3 so why 1?

Ad why DIVISIONMGR1 ID is 1 to 3 and DIVISIONMGR2 4 to 6 and not the opposite? Or why the number does not interleave for instance?
Once again, we need specification for ALL result columns.

And we still have no test case to work with.

Re: CTE Queries [message #643237 is a reply to message #643164] Thu, 01 October 2015 08:41 Go to previous messageGo to next message
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 #643242 is a reply to message #643237] Thu, 01 October 2015 10:09 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
Again this does not make any sense (at least to me). Give us right data set and expected result with proper sentences to explain the result.
At least this much information will do a lot of help for everyone including you.
Re: CTE Queries [message #643243 is a reply to message #643242] Thu, 01 October 2015 10:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

+1
Re: CTE Queries [message #643249 is a reply to message #643243] Thu, 01 October 2015 12:55 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Can you give sample query for Parent to CHILD and

Child to Grand Child..

How can define a Hierarchy,

1. for PARENT -> CHILD

2. CHILD to GRAND CHILD

3. IF Grand Child is belong to two different CHILD's ( we can treat CHILD as Parent here in this case)

Please advise a query .. am not sure ,how can we write a ROOT/ISLEAF in this scenario.

Please advice me queires taking the examples of EMP and DEPT tables as reference.

As I don't have any test cases I can't provide.

Thanks,
Amy
Re: CTE Queries [message #643250 is a reply to message #643249] Thu, 01 October 2015 12:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
Please advice me queires taking the examples of EMP and DEPT tables as reference.


Show us what you want as result for these tables.

Re: CTE Queries [message #643251 is a reply to message #643250] Thu, 01 October 2015 13:43 Go to previous messageGo to next message
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 #643252 is a reply to message #643251] Thu, 01 October 2015 13:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

1/ etc.. is not something we can execute.
2/ there is no prent-child relationship in your SELECT NAME,id
3/ you are changing the requirements at each post.
4/ We still don't know the result you want for ALL cases you posted.

Once more post a test case and the result you want for it.

Re: CTE Queries [message #643253 is a reply to message #643251] Thu, 01 October 2015 13:51 Go to previous messageGo to next message
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 #643254 is a reply to message #643252] Thu, 01 October 2015 13:59 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Michel,

I just posted that code .. for normal reference... means.. I am not interested in writing the code which is hard coding.. The expected O/P

[code]

+----+-----------+
| id | parent_id/LEVEL |
+----+-----------+
| 1 | 3 |
| 2 | 5 |
| 3 | 0 |
| 4 | 10 |
| 5 | 0 |
| 6 | 1 |
| 7 | 5 |
| 8 | 10 |
| 9 | 10 |
| 10 | 3 |
+----+-----------+
Re: CTE Queries [message #643255 is a reply to message #643253] Thu, 01 October 2015 14:00 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
I am using Oracle 11.2.0.3

Could you please advise me the query ..
Re: CTE Queries [message #643256 is a reply to message #643254] Thu, 01 October 2015 14:09 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

We have no test case to work with.

Re: CTE Queries [message #643257 is a reply to message #643254] Thu, 01 October 2015 14:13 Go to previous messageGo to next message
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
_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 Go to previous messageGo to next message
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 #643274 is a reply to message #643272] Fri, 02 October 2015 10:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Did you try to understand _jum's solution?
What does not work for you?

Re: CTE Queries [message #643287 is a reply to message #643274] Fri, 02 October 2015 13:14 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
In the given requirement I have given Parent ,Child and Grandchild information.

So we have added 'a',1'. But we have so many records and its not possible to add
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
...

remaining "Relation as" logic I am able to understand.

for each record.

I would request instead of adding the above statements to every record how this logic will applicable to the existed records in the table?

Could you please let me know this logic.
Re: CTE Queries [message #643288 is a reply to message #643287] Fri, 02 October 2015 13:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

In the same way.
Just replace "data" by your table in the second part of the factoring clause.

Re: CTE Queries [message #643333 is a reply to message #643288] Mon, 05 October 2015 08:01 Go to previous messageGo to next message
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 #643334 is a reply to message #643333] Mon, 05 October 2015 08:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
As the table contains many values, we can't write the query


You don't write, you have not, you must not, you use your current table, this part is just there because you didn't post a valid test case.

Quote:
Could you please advise the query by using above PARENT and CHILD and GRANDCHILD?


Can you please try n ow to do it?
Or post a valid test case with the real names.

Re: CTE Queries [message #643335 is a reply to message #643333] Mon, 05 October 2015 08:26 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
I'm wondering if OP needs to look at dynamic SQL?
Re: CTE Queries [message #643336 is a reply to message #643335] Mon, 05 October 2015 08:28 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
For that OP should know "what is a valid test case". Smile
Then may be OP should be guided for dynamic SQL.
Re: CTE Queries [message #643339 is a reply to message #643336] Mon, 05 October 2015 09:29 Go to previous messageGo to next message
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 #643341 is a reply to message #643339] Mon, 05 October 2015 09:47 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Can you please post a valid test case...

Re: CTE Queries [message #643343 is a reply to message #643341] Mon, 05 October 2015 11:02 Go to previous messageGo to next message
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
Re: CTE Queries [message #643344 is a reply to message #643343] Mon, 05 October 2015 11:03 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 Mon, 05 October 2015 16:47

Can you please post a valid test case...


Re: CTE Queries [message #643346 is a reply to message #643344] Mon, 05 October 2015 11:40 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
Test Case :

[code]

Here AGENCY is the Division_Name.

Manager of this Division is " Michel"

Case 1 : Under Michel 2 people are working for 2 regions.
for 1 region Scott is Manager
for 2nd region TIGER is Manager.



Case 2 : For Scott 2 people are working under him. 1. SyS 2. Admin
For TIGER 1 people working under him 1. SYSTEM

The pictorial View is as follows for division "AGENCY"


                
                          MICHEL  ( Divison manager)  --- His Level is "0"
                            |
                            |
                         ==========
                         |         |
                       SCOTT       TIGER  ( Region Managers) ---> ( these two guys levels are "1")
                       |              |
                       |              |
                   ==========       SYSTEM   ( these 3 guys are team managers)
                   |        |
                  SYS       Admin  



Here is our Test case O/P


ID  Employee              Division_Name  Division_MGR  Region_Mgr   Team_Mgr   Level
-------------------------------------------------------------------------------------
1   MICHEL                 Agency         MICHEL        SCOTT        SYS       0
2   SCOTT                  Agency         MICHEL        SCOTT       ADMIN      1
3   TIGER                  Agency         MICHEL        TIGER       SYSTEM     1 
4   SYS                    Agency         MICHEL        SCOTT       SYS        2      
5   ADMIN                  Agency         MICHEL        SCOTT       ADMIN      2
6   SYSTEM                 Agency         MICHEL        TIGER       SYSTEM     2

etc...


Here we can find easily the hierarchy of all the employees.

Let me know if this test case clears?

Thanks,
Amy


Re: CTE Queries [message #643347 is a reply to message #643346] Mon, 05 October 2015 11:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

This is not a test case.

Re: CTE Queries [message #643348 is a reply to message #643347] Mon, 05 October 2015 12:22 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
treat the above query O/P as expected result and advise me the query.
Re: CTE Queries [message #643349 is a reply to message #643348] Mon, 05 October 2015 12:24 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 Mon, 05 October 2015 18:03

Michel Cadot wrote on Mon, 05 October 2015 16:47

Can you please post a valid test case...

Re: CTE Queries [message #643350 is a reply to message #643349] Mon, 05 October 2015 12:32 Go to previous messageGo to next message
amy.wilson
Messages: 39
Registered: September 2015
Location: Www
Member
I din't write any test case. even I am not sure how to write a test case.

Can you advise a query and sample test cases. So it would be easy for me to post Test cases going forward.
Re: CTE Queries [message #643351 is a reply to message #643350] Mon, 05 October 2015 12:35 Go to previous messageGo to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Just read the link I gave you in my first, second and third posts.

Previous Topic: Checking the parameter inside Procedure (merged 2)
Next Topic: query correction required
Goto Forum:
  


Current Time: Fri Apr 26 06:41:24 CDT 2024