Hierarchical Query question (2)
--Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production
I am trying to join two hierarchies together. The first is Center and each parent Center until the top. The second is Attribute, for each bottom level Center, and it all the way up. Both hierarchies are in the same TABLE. A Center is an Attribute if in the ParentAttribId COLUMN (conversely, the id COLUMN in that TABLE can only be Centers.) Ultimately, the data will be listed record by record, with all the parents as COLUMNs.
The Center hierarchy query works. The parent records-to-column effort will be done later, i don't care about that right now. What i am struggling with is how to join to get the Center's id listed in the Attribute hierarchy. The query fragment (in the second CTE) tries to retrieve Center.Id, which is not in scope, as it is only included in the START WITH clause. But how do i include it in the FROM clause (to bring it in scope) when it has nothing to do with the hierarchy (other than the starting point).
<PRE>
WITH
Center_List_Hierarchy
AS
SELECT
-- 4) List by each Center's hierarchy, the level and id.
-- Note: By definition, CONNECT_BY_ROOT ModelId == ModelId == :Model.
-- ModelId returned so as not to use :Model (in outer query).
CONNECT_BY_ROOT Id Center_Root,
LEVEL Center_Level,
ModelId Model,
Id
FROM
ADMINEPO.PP_Respcenter
CONNECT BY
-- 3) For each Center, go to (and include) the top, identified by Id = ParentId.
ModelId = PRIOR ModelId
AND Id = PRIOR ParentId
AND PRIOR Id <> PRIOR ParentId
START WITH
(ModelId, Id) IN
SELECT
Attribute.ModelId,
Attribute.Id
FROM
ADMINEPO.PP_Respcenter_Name Center_Name,
ADMINEPO.PP_AT_RespCenter Attribute
WHERE
-- 1) For this Model, find the id of the list attribute.
Center_Name.ModelId = :Model
AND Center_Name.LangId = :Name_Type_Default
AND UPPER(Center_Name.NAME) = 'CENTER LIST'
-- 2) Get all Centers associated with the attribute.
AND Attribute.ModelId = Center_Name.ModelId
AND Attribute.ParentAttribId = Center_Name.Id
Attribute_Hierarchy
AS
SELECT
-- 4) For this center, get each Attribute with its hierarchy.
Center.Id Center,
CONNECT_BY_ROOT Id Attribute_Root,
LEVEL Attribute_Level,
ModelId Model,
Id
FROM
ADMINEPO.PP_Respcenter
CONNECT BY
-- 3) For each Attribute, go to (and include) the top, identified by Id = ParentId.
ModelId = PRIOR ModelId
AND Id = PRIOR ParentId
AND PRIOR Id <> PRIOR ParentId
START WITH
(ModelId, Id) IN
SELECT
Attribute.ModelId,
Attribute.ParentAttribId
FROM
Center_List_Hierarchy Center,
ADMINEPO.PP_AT_RespCenter Attribute
WHERE
-- 1) Limit to bottom level Centers.
Center.Center_Level = 1
-- 2) Get its attributes.
AND Attribute.ModelId = Center.Model
AND Attribute.Id = Center.Id
</PRE>
Hi,
Sorry, I don't udnerstand.
Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
If you can show your probelm using commonly available tables, such as scott.emp or hr.employees (both of which contain hierarcies) then you don't have to post any sample data; just the results and the explanation.
Brian Tkatch wrote:
... What i am struggling with is how to join to get the Center's id listed in the Attribute hierarchy. The query fragment (in the second CTE) tries to retrieve Center.Id, which is not in scope, as it is only included in the START WITH clause. Isn't that Attribute_Root, below?
<PRE>
... Attribute_Hierarchy
AS
SELECT
-- 4) For this center, get each Attribute with its hierarchy.
Center.Id Center,
CONNECT_BY_ROOT Id Attribute_Root,
LEVEL Attribute_Level,
ModelId Model,
Id
FROM ...
</PRE>
Similar Messages
-
Hi all,
I'm working on database version (please avoid comments like "you should upgrade to a higher version" as this does not depend on me):
select * from v$version;
BANNER
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
PL/SQL Release 9.2.0.8.0 - Production
CORE 9.2.0.8.0 Production
TNS for HPUX: Version 9.2.0.8.0 - Production
NLSRTL Version 9.2.0.8.0 - Production
I have the following hierarchical data:
with mydata(emp_id, mgr_id, resp) as
select 1, null, 'Y' from dual union all
select 2, 1, 'N' from dual union all
select 3, 2, 'N' from dual union all
select 4, 1, 'Y' from dual union all
select 5, 4, 'N' from dual union all
select 6, 5, 'Y' from dual union all
select 7, 6, 'N' from dual union all
select 8, 1, 'N' from dual union all
select 9, 8, 'N' from dual union all
select 10, 9, 'Y' from dual
select emp_id, mgr_id, resp
from mydata
connect by mgr_id = prior emp_id
start with emp_id=1;
EMP_ID MGR_ID RESP
1 Y
2 1 N
3 2 N
4 1 Y
5 4 N
6 5 Y
7 6 N
8 1 N
9 8 N
10 9 Y
10 rows selected.
Every employee has a responsible (either self or the next in the upper hierarchy having RESP = 'Y')
The root must be always responsible.
To find which is the responsible of a certain employee ID I can run the following query:
i..e: responsible for emp_id = 7 is 6
select emp_id, mgr_id, resp
from mydata
where resp = 'Y'
and rownum <=1
connect by prior mgr_id = emp_id
start with emp_id=7;
EMP_ID
6
1 row selected.
i.e.: responsible for emp_id = 9 is 1
select emp_id
from mydata
where resp = 'Y'
and rownum <=1
connect by prior mgr_id = emp_id
start with emp_id=9;
EMP_ID
1
1 row selected.
i.e.: responsible for emp_id = 4 is self
select emp_id
from mydata
where resp = 'Y'
and rownum <=1
connect by prior mgr_id = emp_id
start with emp_id=4;
EMP_ID
4
1 row selected.
Which query is the faster to get all employees of a specific responsible level?
i.e: all employee not responsible depending on 1 should give:
EMP_ID MGR_ID RESP
2 1 N
3 2 N
8 1 N
9 8 N
Regards.
AlbertoHi, Alberto,
So, you want to quit looking farther down the tree when you come to a node with resp='Y', is that it?
Simply include resp = 'N' (or resp <> 'Y', or NVL (resp, 'Y' <> 'Y') in the CONNECT BY condition, like this:
select emp_id, mgr_id, resp
from mydata
connect by mgr_id = prior emp_id
and resp = 'N' -- *** NEW ***
start with emp_id=1; -
Bill of Materials style query ( hierarchical query question)
Hi,
I'm having difficulty trying to SQL query for the following.
*(1) Outline of proposed table design*
SERVERS
SERVER_ID
SERVER_DESC
etc.
SERVER-MAJORCOMPONENTS
SERVER_ID
COMPONENT_ID
SERVER-PARTS
COMPONENTS_ID
PARENT_COMPONENT_ID
COMPONENT_DESC
etc.
*(2) Text Description*
-- Server A is made up of components P1,P2 and S2.
-- P1 and P2 are major components and would therefore feature in SERVER-MAJORCOMPONENTS that lists all major components (P1,P2,P3 .....P256 etc.) that make up the server.
-- S2 is a minor component and therefore would not be listed in SERVER-MAJORCOMPONENTS, however it would be listed as a child component of component P2 in SERVER-PARTS.
-- A manufacturer issues a safety recall on part S2, based upon an input of a SERVER_ID, I need to know whether that server contains part S2.
Query input : SERVER_ID
Query output : (a) "no rows selected" if server does not contain component (b) count=1,component_id or whatever ... single row
*(3) ASCII ART*
----------> P1
SERVER A
----------> P2
x-------> S1
x--------> S2
Thank you in advance !Hierarhical structures Oracle can easily deal with are (using graph theory terminology) trees.
(defined as: all members except one - the root - have a single parent)
It seems you call the roots "groups" and all other elements "roles"
I find Sanjay's example perfectly suitable.
To describe the structure role and parent_role can do the job.
Suppose you have a table role_hierarchy containing data like:
====================
role | parent_role |
====================
1 | | group
2 | 1 | role
3 | 1 | role
3 | 10 | role
3 | 11 | role
4 | 2 | role
4 | 3 | role
5 | 2 | role
5 | 3 | role
5 | 10 | role
6 | 2 | role
6 | 10 | role
7 | 6 | role
8 | 7 | role
8 | 11 | role
9 | 7 | role
9 | 11 | role
10 | | group
11 | | group
This data represents three trees:
1 10 11
/ \ /| \ / |\
2 3 3 5 6 3 8 9
/|\ |\ /| | / \
4 5 6 4 5 4 5 7 4 5
| / \
7 8 9
8 9
If I understand correctly:
- every table row is owned by some (single) group
- a user is assigned some (single) role suppose 7
I don't have database access and have some connection problems right now so I can just describe it:
Using sys_connect_by_path function in Sanjay's example you'll be able to obtain two paths:
7-6-2-1 from the first and 7-6-10 from the second group
after constructing the string -1-10- containing the valid groups the user can access rows where
INSTR('-1-10-','-'||to_char(group_of_row)||'-') > 0Regards
Etbin -
A help with a hierarchical query
I've got two tables:
FOLDERS where relevant fields are: folderId, parentFolderId, folderQuota
and DOCUMENTS where the relevant fields are: folderId, size
FOLDERS is hierarchical - parentFolderId of a child's folder is set to folderId of the parent
folderQuota is nullable (quota not set)
Now, I need to execute a query with the following recursive logic
<i>function calcQuota (folderIdpar, isFirstpar) {
if (not isFirstpar) and (folderQuota of folder with folderIdpar is not null) return folderQuota;
return size of all documents where folderId = folderIdpar + calcQuota (folderId of all children, false);
}</i>
(I hope the pseudocode is understandable - the query is executed as <i>calcQuota(originalFolderId, true)</i>).
Now, my question is if I can achieve it with a single hierarchical query, or if I have to implement it as a recursive stored procedure.
Thanks!
P.S. I'm using Oracle XE (10g)OK,
I will need to create it (in real life it is created by an application), so I hope I will make it correct. If not, it should be easy to fix.
create table folders (
folder_id number primary key
parent_folder_id number,
quota number
create table documents (
document_id number primary key
folder_id number,
size number
INSERT INTO folders (folder_id, quota) VALUES (1, 1);
INSERT INTO folders (folder_id, parent_folder_id, quota) VALUES (2, 1, 2);
INSERT INTO folders (folder_id, parent_folder_id) VALUES (3, 1);
INSERT INTO folders (folder_id, parent_folder_id, quota) VALUES (4, 2, 4);
INSERT INTO folders (folder_id, parent_folder_id) VALUES (5, 2);
INSERT INTO folders (folder_id, parent_folder_id, quota) VALUES (6, 3, 8);
INSERT INTO folders (folder_id, parent_folder_id) VALUES (7, 3);
INSERT INTO documents (document_id, folder_id, size) VALUES (1, 1, 16);
INSERT INTO documents (document_id, folder_id, size) VALUES (2, 2, 32);
INSERT INTO documents (document_id, folder_id, size) VALUES (3, 3, 64);
INSERT INTO documents (document_id, folder_id, size) VALUES (4, 3, 128);
INSERT INTO documents (document_id, folder_id, size) VALUES (5, 4, 256);
INSERT INTO documents (document_id, folder_id, size) VALUES (6, 5, 512);
INSERT INTO documents (document_id, folder_id, size) VALUES (7, 6, 1024);
INSERT INTO documents (document_id, folder_id, size) VALUES (8, 7, 2048);
running the query for folder_id = 1 should return 1 + 2 + 64 + 128 + 8 + 2048 = 2251
running the query for folder_id = 2 should return 4 + 512 = 516
I have decided for this data, because it allows to track what values (folder.quota or document.size) is included in the result.
(it is the knapsack problem - see http://en.wikipedia.org/wiki/Knapsack_problem)
P.S. I'm leaving for three weeks, so I will come back after that. -
Problem with different execution paths in hierarchical query
Hello,
I have problems with the following query:
SELECT DISTINCT P.ID FROM PRODUCTELEMENTIMPL P WHERE ( ( LABEL = 'SomeLabel' AND PRODUCTELEMENTTYPE = 'SomeText' AND ( STATE = 'created' OR STATE = 'stored' OR STATE = 'archived' OR STATE = 'archivedRestored' ) ) ) START WITH P.ID = 42 CONNECT BY PRIOR P.ID = P.PARENT
We have two databases (an Oracle 10g XE and Oracle10g Enterprise). In the XE Database the query is executed very fast, but in the main installation it takes minutes. If I "explain" the query I get two different execution paths:
The fast:
ID PARENT_ID LEVEL SQL Kosten Anzahl Zeilen
0 - 1 SELECT STATEMENT 20 49
1 0 2 HASH UNIQUE 20 49
2 1 3 FILTER - -
3 2 4 CONNECT BY WITH FILTERING - -
4 3 5 TABLE ACCESS BY INDEX ROWID PRODUCTELEMENTIMPL (TABLE) - -
5 4 6 INDEX UNIQUE SCAN SYS_C0072201 (INDEX (UNIQUE)) 2 1
6 3 5 NESTED LOOPS - -
7 6 6 BUFFER SORT - -
8 7 7 CONNECT BY PUMP - -
9 6 6 TABLE ACCESS BY INDEX ROWID PRODUCTELEMENTIMPL (TABLE) 19 49
10 9 7 INDEX RANGE SCAN PRODUCTELEMENTIMPL_IDX1 (INDEX) 3 49
11 3 5 TABLE ACCESS FULL PRODUCTELEMENTIMPL (TABLE) 19 49
Slow:
ID PARENT_ID LEVEL SQL Kosten Anzahl Zeilen
0 1 SELECT STATEMENT 1 1
1 0 2 HASH UNIQUE 1 1
2 1 3 FILTER
3 2 4 CONNECT BY WITHOUT FILTERING
4 3 5 TABLE ACCESS BY INDEX ROW 3 1
ID PRODUCTELEMENTIMPL (TABLE)
5 4 6 INDEX UNIQUE SCAN SYS_C0 2 1
020528 (INDEX (UNIQUE))
6 3 5 TABLE ACCESS FULL PRODUCT 6628 1100613
ELEMENTIMPL (TABLE)
Any ideas how to avoid this full table scan?
bye
Roland SpatzeneggerHello,
thank you for your replies. The indices and table schemas are the "same", but only the content for the tables was mirrored.
We made some tests with dropping and/or analyzing the tables, but it didn't change anything.
The main problem is that the query takes 33s in the productive environment for searching in a couple of rows. At the moment it's faster to make
SELECT DISTINCT P.ID, P.STATE FROM PRODUCTELEMENTIMPL P WHERE ( ( LABEL = 'SomeLabel' AND PRODUCTELEMENTTYPE = 'SomeText' ) ) START WITH P.ID = 42 CONNECT BY PRIOR P.ID = P.PARENT
and to test in the application if the state-values match ;-)
If I add the hint /*+ no_filtering */ in the test environment, I get the same "slow" execution path as in the production environment. So the question is, what prevents the filtering in "connect by"?
(I think in the fast version it filters only the results of the hierarchical query, in the slow version it first filters the whole table and joins/merge it with the hierachical result).
bye
Roland Spatzenegger -
Hi
Question:
In a hierarchical query, is it possible for a row to have more than one immediate ancestor?
Answer:
No
No? Surely, it's yes?
Thanks,
JasonAs Frank pointed out already hierarhical most often means a tree (data structure) to deal with.
There must usually be just one boss (the root) in which case the answer is no.
Something to read: http://en.wikipedia.org/wiki/Tree_(data_structure)
You can find out Solomon spoke about a generalization therein.
Related to forum troubles:
If I login first thing after reaching forum, the behaviour is rather consistent - I'm allowed to post answers, otherwise ...
Regards
Etbin -
Depth first in hierarchical query
Does anybody know how to make a depth first run through of a
hierarchical query using START WITH ... CONNECT BY PRIOR???Allright, here comes the details:
Imagine that you need to perform a depth first run of a table
that looks like this:
id
parent_id
priority
depth
attribute1
attribute2
id is primary key and parent_id foreign key to id, i.e. it
references id. The priority is forth numbered from 1 to n if n
children are attached to the same parent. The depth indicates
the depth in the tree the row is. Example rows:
1 null 1 0 foo bar ...
2 1 2 1 foobar barfoo ...
3 1 1 1 barbar foofoo ...
4 null 2 0 foo bar ...
5 2 1 2 foofoobar barfoofoo ...
6 2 3 2 foobarfoo barfoobar ...
7 2 2 2 foofoofoo barbarbar ...
8 6 1 3 foooooooooo baaaaaaaaar ...
9 1 3 1 fooooo baaaar ...
The desired result should then be (assume that all attributes
are selected):
1 null 1 0 foo bar ...
3 1 1 1 barbar foofoo ...
2 1 2 1 foobar barfoo ...
5 2 1 2 foofoobar barfoofoo ...
7 2 2 2 foofoofoo barbarbar ...
6 2 3 2 foobarfoo barfoobar ...
8 6 1 3 foooooooooo baaaaaaaaar ...
9 1 3 1 fooooo baaaar ...
4 null 2 0 foo bar ...
My attempt was to try something like:
SELECT id, parent_id, priority, attribute1, attribute2, ...
FROM table
CONNECT BY PRIOR id = parent_id START WITH id = ?
ORDER BY depth, priority
I know that the depth can be obtained from CONNECT BY PRIOR, but
I use the depth in other SQLs, so just assumes that you have
it...
This SELECT ofcause only selects from a given id and down, but
that is also ok; we can just start with id = 1. And the SELECT
needs to be given the root id, since that is the way the
application works...
But this does not work, since it groups by depth thus return all
children (c1...cN) at depth i for parent_id x before the
children of a child in (c1...cN) at depth i+1.
So, I guess the question is... If the above SELECT can be used
showhow, then
SELECT id, parent_id, priority, attribute1, attribute2, ...
FROM table
CONNECT BY PRIOR id = parent_id START WITH id = ?
ORDER BY __WHAT_GOES_HERE___, priority
or if not, please state a SELECT statement that does the trick!
Regards
Sxren -
Hierarchical query to combine two groupings into one broad joint grouping
Hi there,
I would like to know if anyone knows a way to solve the problem below with a SQL querie, maybe using some hierarchical queries or window functions (or anything else in SQL for that matter).
My environment is:
Oracle Database 11g Release 11.2.0.2.0 - 64bit
The problem is this:
I have a list of items that are grouped together in two different grouping ways (two columns).
This gives the ability for items to be linked to other items in two ways:
1. Directly if both have same value on GROUP1 and/or GROUP2;
2. indirectly if they have an item in common with at least one match on either GROUP1 or GROUP2.
The idea is to start from this dataset:
WITH T AS
SELECT 1 AS ITEM_ID, 'A' AS GROUP1, 100 AS GROUP2 FROM DUAL UNION
SELECT 2 AS ITEM_ID, 'A' AS GROUP1, 100 AS GROUP2 FROM DUAL UNION
SELECT 3 AS ITEM_ID, 'A' AS GROUP1, 101 AS GROUP2 FROM DUAL UNION
SELECT 4 AS ITEM_ID, 'B' AS GROUP1, 100 AS GROUP2 FROM DUAL UNION
SELECT 5 AS ITEM_ID, 'B' AS GROUP1, 102 AS GROUP2 FROM DUAL UNION
SELECT 6 AS ITEM_ID, 'C' AS GROUP1, 103 AS GROUP2 FROM DUAL UNION
SELECT 7 AS ITEM_ID, 'D' AS GROUP1, 101 AS GROUP2 FROM DUAL
SELECT * FROM T;
And end up with this dataset with a one single joint grouping:
WITH T AS
SELECT 1000 AS JOINT_GROUP_ID, 1 AS ITEM_ID FROM DUAL UNION
SELECT 1000 AS JOINT_GROUP_ID, 2 AS ITEM_ID FROM DUAL UNION
SELECT 1000 AS JOINT_GROUP_ID, 3 AS ITEM_ID FROM DUAL UNION
SELECT 1000 AS JOINT_GROUP_ID, 4 AS ITEM_ID FROM DUAL UNION
SELECT 1000 AS JOINT_GROUP_ID, 5 AS ITEM_ID FROM DUAL UNION
SELECT 1000 AS JOINT_GROUP_ID, 7 AS ITEM_ID FROM DUAL UNION
SELECT 2000 AS JOINT_GROUP_ID, 6 AS ITEM_ID FROM DUAL
SELECT * FROM T;The relationships are:
Item 1 is linked to Item 2 by GROUP1 and GROUP2;
Item 1 is linked to Item 3 by GROUP1 only;
Item 1 is linked to Item 4 by GROUP2 only;
Item 1 is linked to Item 5 through Item 4 by GROUP1;
Item 1 is linked to Item 7 through Item 3 by GROUP2;
Item 6 is not linked to any other item since it does not match on GROUP1 nor GROUP2 with any other item.
NOTEs:
- JOINT_GROUP_ID values could be any sequential value. I used 1000 and 2000 just to avoid confusion with the other IDs and group values used to picture the problem.
- The level of relationship is not restricted to 2 like the example above. There could be deeper relationships.
This seems to me like something that could be solved with a hierarchical query, but I could not get my head around it to solve the problem.
Hope one of you guys can help me on this.
Chears.Hi Bruno,
You are correct. Frank's solution does not work. You can do this using CONNECT BY on smaller problems, but it will be very inefficient for larger problems wirth significant looping. I wrote a quick blog article on this subject after reading your question this morning. This is actually an example of a very general class of problems and I already had three SQL solutions. I'll put the CONNECT BY one here, and you can look at my blog if you want more details (I include a diagram so I can't just post it here).
Data
item_groups
SQL> SELECT *
2 FROM item_groups
3 /
ITEM_ID GROUP1 GROUP2
01 A 100
02 A 100
03 A 101
04 B 100
05 B 102
06 C 103
07 D 101
08 E 104
09 E 105
10 F 106
10 rows selected.Query
WITH links_v AS (
SELECT t_fr.item_id node_id_fr,
t_to.item_id node_id_to,
t_fr.item_id || '-' || Row_Number() OVER (PARTITION BY t_fr.item_id ORDER BY t_to.item_id) link_id
FROM item_groups t_fr
JOIN item_groups t_to
ON t_to.item_id > t_fr.item_id
AND (t_to.group1 = t_fr.group1 OR t_to.group2 = t_fr.group2)
), nodes_v AS (
SELECT item_id node_id
FROM item_groups
), tree AS (
SELECT link_id, CONNECT_BY_ROOT (link_id) root_id
FROM links_v
CONNECT BY NOCYCLE (node_id_fr = PRIOR node_id_to OR node_id_to = PRIOR node_id_fr OR
node_id_fr = PRIOR node_id_fr OR node_id_to = PRIOR node_id_to)
), group_by_link AS (
SELECT DISTINCT Min (root_id) OVER (PARTITION BY link_id) group_id, link_id
FROM tree
), linked_nodes AS (
SELECT g.group_id, l.node_id_fr node_id
FROM group_by_link g
JOIN links_v l
ON l.link_id = g.link_id
UNION
SELECT g.group_id, l.node_id_to
FROM group_by_link g
JOIN links_v l
ON l.link_id = g.link_id
SELECT l.group_id "Network", l.node_id "Node"
FROM linked_nodes l
UNION ALL
SELECT '00 (unlinked)', node_id
FROM nodes_v n
WHERE n.node_id NOT IN (SELECT node_id FROM linked_nodes)
ORDER BY 1, 2Output
Network Node
00 (unlinked) 06
10
01-1 01
02
03
04
05
07
08-1 08
09 -
Hi,
I have a hierarchical query which takes 2 seconds to execute. I need to get this down to milli seconds.
The table has around 8000 records. The query will never return more than 20 or so records. There is only ever 2 levels to the query.
I am quite surprised at this because it is a very simple query no table joins etc and I would have though 8000 records was nothing for Oracle.
Select id, parent_id, col1, col2, col3, col4, col5, col6
from my_table
where id=500
start with parent_id is null
connect by prior id = parent_id;
I have even tried initializing the start with say with 0 and making it a not null column...... indexing the columns used in the start with + connect by
I have tried various indexing stratergies. Does anyone have any similar experience? I am using Oracle 9i.
Thanks in advanceHow can I utilise the above query in a view if at all? Will I be able to pass in differnt ID's to the START WITH...?Certainly, you can. For example, you can use packaged
public variable to pass a parameter or Oracle CONTEXT:
SQL> create or replace package pass_param is
2 empno emp.empno%TYPE;
3 function get_empno return emp.empno%TYPE;
4 end;
5 /
 
Package created.
 
SQL> create or replace package body pass_param is
2 function get_empno return emp.empno%TYPE
3 is
4 begin
5 return empno;
6 end;
7 end;
8 /
 
Package body created.
 
SQL> create or replace view emp_v as
2 select ename from emp
3 start with empno = pass_param.get_empno
4 connect by prior empno = mgr
5 /
 
View created.
 
SQL> exec pass_param.empno := 7839;
 
PL/SQL procedure successfully completed.
 
SQL> select * from emp_v;
 
ENAME
KING
JONES
SCOTT
ADAMS
FORD
SMITH
BLAKE
ALLEN
WARD
MARTIN
TURNER
JAMES
CLARK
MILLER
 
14 rows selected.
 
SQL> exec pass_param.empno := 7698;
 
PL/SQL procedure successfully completed.
 
SQL> select * from emp_v;
 
ENAME
BLAKE
ALLEN
WARD
MARTIN
TURNER
JAMES
 
6 rows selected. or:
SQL> create or replace package set_param
2 is
3 procedure set_empno(empno in number);
4 end;
5 /
 
Package created.
 
SQL> create or replace package body set_param
2 is
3 procedure set_empno(empno in number)
4 is
5 begin
6 dbms_session.set_context('empnamespace','empno',empno);
7 end;
8 end;
9 /
 
Package body created.
 
SQL> create or replace context empnamespace using set_param;
 
Context created.
 
SQL> create or replace view emp_v as
2 select ename from emp
3 start with empno = sys_context('empnamespace','empno')
4 connect by prior empno = mgr
5 /
 
View created.
 
SQL> exec set_param.set_empno(7698);
 
PL/SQL procedure successfully completed.
 
SQL> select * from emp_v;
 
ENAME
BLAKE
ALLEN
WARD
MARTIN
TURNER
JAMES
 
6 rows selected.
 
SQL> exec set_param.set_empno(7839);
 
PL/SQL procedure successfully completed.
 
SQL> select * from emp_v;
 
ENAME
KING
JONES
SCOTT
ADAMS
FORD
SMITH
BLAKE
ALLEN
WARD
MARTIN
TURNER
JAMES
CLARK
MILLER
 
14 rows selected.Rgds. -
Too many results in hierarchically query
Hello all,
I'm searching for an idea to stop getting results 2, 3 and more times out of the following query
select
t.lvl,
t.syswflvl,
t.upper,
LPAD(' ', (lvl)*8)||t.code code,
LPAD(' ', (lvl)*8)||t.bezeichnung bezeichnung,
t.chk,
t.rang
from (
select '0' lvl, '0-'||to_char(syswftable,'0000000') syswflvl, '0- 0000000' upper, syscode code, bezeichnung, '' chk, 0 rang from wftable where (select count(wfm.syswftable) from wfm where wfm.syswftable = wftable.syswftable) > 0 union
select '1' lvl, '1-'||to_char(syswfm,'0000000') syswflvl, '0-'||to_char(syswftable,'0000000') upper, syscode code, kurzbez bezeichnung, anzeigefilter chk, 1 rang from wfm union
select '2' lvl, '2-'||to_char(syswfa,'0000000') syswflvl, '1-'||to_char(syswfm,'0000000') upper, syscode code, kurzbez bezeichnung, bedingung chk, rang from wfa union
select '3' lvl, '3-'||to_char(syswfc,'0000000') syswflvl, '2-'||to_char(syswfa,'0000000') upper, syscode code, kurzbez bezeichnung, bedingung chk, rang from wfc union
select '4' lvl, '4-'||to_char(syswfg,'0000000') syswflvl, '3-'||to_char(syswfc,'0000000') upper, syscode code, kurzbez bezeichnung, bedingung chk, rang from wfg
) t
where ((t.chk not like '%and 0%'
and trim(t.chk) not like '0%')
or t.chk is null)
and upper not like '%-'
connect by nocycle prior syswflvl = upper
order siblings by upper, syswflvl, rang
What happens is, that I get the results from level 0 one times, from level 1 two times, from level 2 three times etc.
What I'm try to achive is to get only the whole thing once.
Hope you can see what my problem is ;-)
Regards
CarstenThe effect of not having a start with clause in a hierarchical query is that a hierarchy is produced starting at every possible entry-point. So, you'll get the hierachy from node 1, the hierarchy from all nodes 2, the hierarchy from all nodes 3 and so one. Whether it starts with the correct one or not is not really relevant, as you already figured out that you get too much....
So, just believe it, add a start with clause...and your problems are gone. -
Value of the start in the Select with in a Hierarchical Query
Exist any way for put in the select the value of the start with in a Hierarchical Query?
An example:
I'll need sth like
CTH@> select n code, level, np code_parent, 1 code_first_parent
2 from demo
3 start with n=1
4 connect by np = prior n
5 ;
CODE LEVEL CODE_PARENT CODE_FIRST_PARENT
1 1 1
2 2 1 1
3 3 2 1
4 4 3 1
5 5 4 1
6 6 5 1
7 7 6 1
8 8 7 1
9 9 8 1
10 10 9 1
-- Naturally it couldn´t be a constant value
The query
select n,d, level nivel
, np, prior n
from demo
start with n=1
connect by np = prior n
--Table and inserts
create table demo
( n number,
d varchar2(5),
np number);
insert into demo values (1,'A', null);
insert into demo values (2,'B',1);
insert into demo values (3,'C',2);
insert into demo values (4,'D',3);
insert into demo values (5,'E',4);
insert into demo values (6,'F',5);
insert into demo values (7,'G',6);
insert into demo values (8,'H',7);
insert into demo values (9,'I',8);
insert into demo values (10,'J',9);
insert into demo values (11,'K', null);
insert into demo values (12,'L',11);
insert into demo values (13,'M',12);
insert into demo values (14,'N',13);
insert into demo values (15,'O',14);
insert into demo values (16,'P',15);
Message was edited by:
cthOn 10g
connect_by_root(n)Best regards
Maxim -
[Oracle 8i] Need help pruning branches from a hierarchical query
My problem is that my hierarchical query seems only to trim out the values that don't meet my criteria, but still includes their children. When my query hits a record that does not meet my criteria, I want it to stop there. I've tried including the criteria in just the 'where' clause of the query, and have also put the criteria in the 'connect by' clause as well, but nothing has fixed it. Please keep in mind I'm using Oracle 8i, so I can't use some of the 'nicer' statements for hierarchical queries that they introduced in 9. I'm stuck with 'Start With...Connect By'.
I have sample tables/data that I can post if someone needs to see that to help me, but to start with, here's my current query:
SELECT *
FROM (
SELECT
LEVEL
, c_bill.comp_part_nbr AS c_part_nbr
, (select c_part.part_desc
FROM part c_part
WHERE c_part.part_nbr=c_bill.comp_part_nbr) AS c_part_desc
, (SELECT c_part.part_type
FROM part c_part
WHERE c_part.part_nbr=c_bill.comp_part_nbr) AS c_part_type
, c_bill.qty_per AS c_qty_per_p
, c_bill.qty_per_type AS c_qty_per_type
, (SELECT c_part.qty_on_hand
FROM part c_part
WHERE c_part.part_nbr=c_bill.comp_part_nbr) AS c_qty_on_hand
, c_bill.oper_nbr AS rqd_at_op
, c_bill.comp_off_adj AS rqd_offset
, c_bill.bom_doc_nbr AS p_part_nbr
, (SELECT p_part.qty_on_hand
FROM part p_part
WHERE p_part.part_nbr=c_bill.bom_doc_nbr) AS p_qty_on_hand
FROM
BILL c_bill
WHERE
(c_bill.status = 'RL')
AND (c_bill.view_code IN ('M','G'))
AND (c_bill.end_eff_dt > SYSDATE)
AND (c_bill.begn_eff_dt <= SYSDATE)
START WITH c_bill.bom_doc_nbr=RPAD(?,25)
CONNECT BY PRIOR c_bill.comp_part_nbr=c_bill.bom_doc_nbr
AND c_bill.view_code IN ('M','G')
AND c_bill.status = 'RL'
AND c_bill.end_eff_dt > SYSDATE
AND c_bill.begn_eff_dt <= SYSDATE
) a
WHERE c_part_type = 'M'The outside criterion of part_type='M' isn't my problem. Where I'm actually seeing my issue rear its ugly head is in the criterion:
(c_bill.view_code IN ('M','G'))What I'll have happen is that one of the children or grandchildren of the part number I'm querying for (my parameter), will be of some view code that's not 'M' or 'G'. In my sample data below, I have a level 4 part that is part of the 'H' view code, which I don't want, nor do I want it's children. However, its child is in the 'G' view code, and my query returns it anyway.
In my sample data below, I'm assuming that the parameter = 'XYZ-100'
CREATE TABLE part
part_nbr varchar(25) not null,
part_desc varchar(25) not null,
part_type char(1) not null,
qty_on_hand double(13,4) not null
CONSTRAINT part_pk
PRIMARY KEY (part_nbr),
CONSTRAINT check_part_type
CHECK (part_type IN ('M','P','X','Y')),
CONSTRAINT check_qty_on_hand
CHECK (qty_on_hand >= 0)
CREATE TABLE bill
row_added_ts char(20) not null,
bom_doc_nbr varchar(25) not null,
comp_part_nbr varchar(25) not null,
qty_per double(9,5) not null,
qty_per_type char(1) not null,
oper_nbr char(4) not null,
comp_off_adj double(3,0),
status char(2),
view_code char(1) not null,
end_eff_dt date() not null,
begn_eff_dt date() not null
CONSTRAINT bill_pk
PRIMARY KEY (row_added_ts),
CONSTRAINT check_qty_per_type
CHECK (qty_per_type IN ('0','1','2','3')),
CONSTRAINT check_status
CHECK (status IN ('IN', 'RL')),
); Values for those tables:
INSERT INTO part
VALUES ('xyz-1', 'purchased part', 'P', 5);
INSERT INTO part
VALUES ('xyz-2', 'purchased part', 'P', 1);
INSERT INTO part
VALUES ('xyz-3', 'purchased part', 'P', 1);
INSERT INTO part
VALUES ('xyz-3a', 'manufactured part', 'M', 1);
INSERT INTO part
VALUES ('xyz-4', 'purchased part', 'P', 1);
INSERT INTO part
VALUES ('xyz-9-1', 'manufactured part', 'M', 0);
INSERT INTO part
VALUES ('xyz-9a', 'manufactured part', 'M', 0);
INSERT INTO part
VALUES ('raw-1', 'purchased raw material', 'P', 212);
INSERT INTO part
VALUES ('raw-2', 'purchased raw material', 'P', 75.5);
INSERT INTO part
VALUES ('XYZ-100', 'manufactured part', 'M', 0);
INSERT INTO part
VALUES ('(OPEN)', '(not in use)', 'Y', 0);
INSERT INTO part
VALUES ('XYZ-100-1', 'manufactured part', 'M', 0);
INSERT INTO part
VALUES ('XYZ-100-2', 'manufactured part', 'M', 1);
INSERT INTO part
VALUES ('XYZ-100-3', 'manufactured part', 'M', 0);
INSERT INTO part
VALUES ('XYZ-100-4', 'manufactured part', 'M', 2);
INSERT INTO part
VALUES ('XYZ-100-A', 'manufactured part', 'M', 0);
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100','xyz-1',3,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100','XYZ-100-1',1,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-1','xyz-1',2,'1','****',1,'RL','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-1','XYZ-100-2',3,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-2','xyz-2',6,'1','****',2,'RL','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-2','xyz-4',6,'1','****',2,'IN','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-2','xyz-100-3',1,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-3','xyz-3',8,'1','****',1,'RL','M','01-Jan-2050','01-Jan-2000');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-3','xyz-3a',8,'1','****',1,'RL','M','01-Jan-2000','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-3','XYZ-100-4',4,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-3','XYZ-100-A',2,'1','****',2,'RL','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008071153100150000','XYZ-100-3','(OPEN)',2,'1','****',0,'RL','E','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008071153100150000','XYZ-100-3','xyz-9-1',2,'1','****',0,'RL','H','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-4','raw-1',8.75,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008072153100150000','XYZ-100-A','raw-2',3.75,'1','****',0,'RL','M','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008075911100150000','xyz-9-1','xyz-9a',1,'1','****',0,'RL','G','01-Jan-2050','01-Jan-1900');
INSERT INTO bill
VALUES ('2008087711100150000','xyz-9a','raw-2',3.75,'1','****',0,'RL','G','01-Jan-2050','01-Jan-1900');Sample data displayed in table format:
--PART table (from insert statements above)
part_nbr part_desc part_type qty_on_hand
xyz-1 purchased part P 5
xyz-2 purchased part P 1
xyz-3 purchased part P 1
xyz-3a manufactured part M 1
xyz-4 purchased part P 1
xyz-9-1 manufactured part M 0
xyz-9a manufactured part M 0
raw-1 purchased raw material P 212
raw-2 purchased raw material P 75.5
XYZ-100 manufactured part M 0
(OPEN) (not in use) Y 0
XYZ-100-1 manufactured part M 0
XYZ-100-2 manufactured part M 1
XYZ-100-3 manufactured part M 0
XYZ-100-4 manufactured part M 2
XYZ-100-A manufactured part M 0
--BILL table (from insert statements above)
row_added_ts bom_doc_nbr comp_part_nbr qty_per qty_per_type oper_nbr comp_off_adj status view_code end_eff_dt begn_eff_dt
2008072153100150000 XYZ-100 xyz-1 3 1 **** 0 RL G 01-Jan-2050 01-Jan-1900
2008072223100150000 XYZ-100 XYZ-100-1 1 1 **** 0 RL M 01-Jan-2050 01-Jan-1900
2008072411100150000 XYZ-100-1 xyz-1 2 1 **** 1 RL M 01-Jan-2050 01-Jan-1900
2008072459100150000 XYZ-100-1 XYZ-100-2 3 1 **** 0 RL M 01-Jan-2050 01-Jan-1900
2008072578100150000 XYZ-100-2 xyz-2 6 1 **** 2 RL M 01-Jan-2050 01-Jan-1900
2008072694100150000 XYZ-100-2 xyz-4 6 1 **** 2 IN G 01-Jan-2050 01-Jan-1900
2008072786100150000 XYZ-100-2 xyz-100-3 1 1 **** 0 RL M 01-Jan-2050 01-Jan-1900
2008072865100150000 XYZ-100-3 xyz-3 8 1 **** 1 RL M 01-Jan-2050 01-Jan-2000
2008073100100150000 XYZ-100-3 xyz-3a 8 1 **** 1 RL M 01-Jan-2000 01-Jan-1900
2008073159100150000 XYZ-100-3 XYZ-100-4 4 1 **** 0 RL M 01-Jan-2050 01-Jan-1900
2008073346100150000 XYZ-100-3 XYZ-100-A 2 1 **** 2 RL M 01-Jan-2050 01-Jan-1900
2008073478100150000 XYZ-100-3 (OPEN) 2 1 **** 0 RL E 01-Jan-2050 01-Jan-1900
2008073529100150000 XYZ-100-3 xyz-9-1 2 1 **** 0 RL H 01-Jan-2050 01-Jan-1900
2008073798100150000 XYZ-100-4 raw-1 8.75 1 **** 0 RL M 01-Jan-2050 01-Jan-1900
2008073811100150000 XYZ-100-A raw-2 3.75 1 **** 0 RL M 01-Jan-2050 01-Jan-1900
2008075911100150000 xyz-9-1 xyz-9a 1 1 **** 0 RL G 01-Jan-2050 01-Jan-1900
2008087711100150000 xyz-9a raw-2 3.75 1 **** 0 RL G 01-Jan-2050 01-Jan-1900--What I want to get with my query (branches pruned off my tree)
LEVEL C_PART_NBR C_PART_DESC C_PART_TYPE C_QTY_PER_P C_QTY_PER_TYPE C_QTY_ON_HAND RQD_AT_OP RQD_OFFSET P_PART_NBR P_QTY_ON_HAND
1 XYZ-100-1 manufactured part M 1 1 0 **** 0 XYZ-100 0
2 XYZ-100-2 manufactured part M 3 1 1 **** 0 XYZ-100-1 0
3 xyz-100-3 manufactured part M 1 1 0 **** 0 XYZ-100-2 1
4 XYZ-100-4 manufactured part M 4 1 2 **** 0 XYZ-100-3 0
4 XYZ-100-A manufactured part M 2 1 0 **** 2 XYZ-100-3 0--What I actually get with my query (includes children of items that don't meet query criteria)
LEVEL C_PART_NBR C_PART_DESC C_PART_TYPE C_QTY_PER_P C_QTY_PER_TYPE C_QTY_ON_HAND RQD_AT_OP RQD_OFFSET P_PART_NBR P_QTY_ON_HAND
1 XYZ-100-1 manufactured part M 1 1 0 **** 0 XYZ-100 0
2 XYZ-100-2 manufactured part M 3 1 1 **** 0 XYZ-100-1 0
3 xyz-100-3 manufactured part M 1 1 0 **** 0 XYZ-100-2 1
4 XYZ-100-4 manufactured part M 4 1 2 **** 0 XYZ-100-3 0
4 XYZ-100-A manufactured part M 2 1 0 **** 2 XYZ-100-3 0
5 xyz-9a manufactured part M 1 1 0 **** 0 xyz-9-1 0Edited by: user11033437 on Jul 30, 2009 7:27 AM (grammar) -
Hierarchical Query with Rollup Sum (CONNECT BY with GROUP BY ROLLUP)
Hi all,
Imagine the following scenario: i have an ACCOUNT table which holds accounts and their hierarchy (currently 5 levels), and a BALANCE table which holds balance records for the accounts. Only CHILD accounts (level 5) have records in the BALANCE table. Simple example:
CREATE TABLE accounts (account_code VARCHAR2(30), parent_account VARCHAR2(30), account_desc VARCHAR2(400));
CREATE TABLE balances (account_code VARCHAR2(30), balance_amount NUMBER(18,2));
INSERT INTO ACCOUNTS VALUES ('TOT',NULL,'Total');
INSERT INTO ACCOUNTS VALUES ('ANA1','TOT','General Expenses');
INSERT INTO ACCOUNTS VALUES ('4801001','ANA1','Small Expenses');
INSERT INTO ACCOUNTS VALUES ('4801002','ANA1','Transportation');
INSERT INTO ACCOUNTS VALUES ('ANA2','TOT','Health Expenses');
INSERT INTO ACCOUNTS VALUES ('4802001','ANA2','Healthcare');
INSERT INTO ACCOUNTS VALUES ('4802002','ANA2','Facilities');
INSERT INTO BALANCES VALUES ('4801001', 2000);
INSERT INTO BALANCES VALUES ('4801002', 1000);
INSERT INTO BALANCES VALUES ('4802001', 3000);
INSERT INTO BALANCES VALUES ('4802002', 4000);What i need in this scenario is to run a hierarchical query, where for each node i compute the sum of all its children (In LEAF nodes which are the child accounts, this sum is the value in BALANCES itself). Final Result would be:
TOT -> 10000
ANA1 -> 3000
4801001 -> 2000
4801001 -> 1000
ANA2 -> 7000
4802001 -> 3000
4802002 -> 4000I have tried various ways, and found out a workaround which works for a fixed amount of levels, basically it builds the hierarchy and computes the SYS_CONNECT_BY_PATH, then splits this as a regular expression and uses GROUP BY ROLLUP to compute the higher levels. Then i assemble it again, now with the computed values. Below is the example query:
select level
, NVL (vfinal.child_account,'TOTAL') ||' - '||
( SELECT account_desc
FROM accounts
WHERE account_code = vfinal.child_acct ) account_name
, to_char(sum_bal, 'fm999g999g999g990') as rolled_up_balance
from
select coalesce( princ.lvl3, princ.lvl2, princ.lvl1 ) child_acct
, DECODE ( princ.lvl2 , NULL
, NULL
, DECODE ( princ.conta_lvl3, NULL
, princ.conta_lvl1,princ.conta_lvl2 ) ) parent_acct
, sum(princ.balance_amount) sum_bal
from (
select hier.lvl1
, hier.lvl2
, hier.lvl3
, hier.parent_account
, hier.account_code child_acc
, bal.balance_amount
from ( select level
, sys_connect_by_path( account_code, '/' ) hierarchy_acct
, REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,3) lvl3
, REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,2) lvl2
, REGEXP_SUBSTR(sys_connect_by_path( account_code, '/' ),'[^/]+',1,1) lvl1
, account_code
, parent_account
from accounts acc
where level <= 3
start with parent_account is null
connect by nocycle prior account = parent_account
order siblings by parent_account
) hier
, balances bal
where bal.cod_conta = hier.account_code
) princ
where princ.lvl1 is not null
group by rollup ( princ.lvl1
, princ.lvl2
, princ.lvl3 )
order by princ.conta_lvl1
, princ.conta_lvl2
, princ.conta_lvl3
) vfinal
where child_acct is not null
start with parent_acct is null
connect by nocycle prior child_acct = parent_acctAll said and done, what i need is to do the same thing for infinite levels, because this query has 3 fixed levels. Do you know how can i structure a new query where, independently of the number of levels, the parent sums are all rolled up like this?
Thanks a lot in advance! Best Regards!
Thiago
Edited by: Thiago on Sep 6, 2011 11:31 AM
Edited by: Thiago on Sep 6, 2011 1:01 PMHi,
Thiago wrote:
Hi all,
Imagine the following scenario: i have an ACCOUNT table which holds accounts and their hierarchy (currently 5 levels), and a BALANCE table which holds balance records for the accounts. Only CHILD accounts (level 5) have records in the BALANCE table. Simple example:
CREATE TABLE accounts (account_code VARCHAR2(30), parent_account VARCHAR2(30), account_desc VARCHAR2(400));
CREATE TABLE balances (account_code VARCHAR2(30), balance_amount NUMBER(18,2));
INSERT INTO ACCOUNTS ('TOT',NULL,'Total');
INSERT INTO ACCOUNTS ('ANA1','TOT','General Expenses');
INSERT INTO ACCOUNTS ('4801001','ANA1','Small Expenses');
INSERT INTO ACCOUNTS ('4801002','ANA1','Transportation');
INSERT INTO ACCOUNTS ('ANA2','TOT','Health Expenses');
INSERT INTO ACCOUNTS ('4802001','ANA2','Healthcare');
INSERT INTO ACCOUNTS ('4802002','ANA2','Facilities');
INSERT INTO BALANCES ('4801001', 2000);
INSERT INTO BALANCES ('4801001', 1000);
INSERT INTO BALANCES ('4802001', 3000);
INSERT INTO BALANCES ('4802001', 4000);
Thanks for posting the CREATE TABLE and INSERT statements. Remember why you do it: so that the people who want to help you can re-create the problem and test their ideas. If the statments don't work, then they are not so useful. None of the INSERT statements you posted work: they all need a VALUES keyword. Please test those statments before you post them.
Also, make sure that the reuslts you post correspond to the sample data you post. In your sample data, there are no rows in balances for account_codes '4801002' or '4802002'.
I think you want something like this:
WITH connect_by_results AS
SELECT CONNECT_BY_ROOT account_code AS root_account_code
, account_code
FROM accounts
-- NOTE: No START WITH clause
CONNECT BY parent_account = PRIOR account_code
SELECT c.root_account_code || ' -> '
|| TO_CHAR (SUM (b.balance_amount)) AS txt
FROM connect_by_results c
LEFT OUTER JOIN balances b ON c.account_code = b.account_code
GROUP BY c.root_account_code
; -
Need to populate a hierarchical query results in detail block of WIPTXCFM
Hi All,
I would need to customize WIPTXCFM to populate all layered sub assembly schedule numbers against a Final Assembly schedule number.
Requirement is as soon as User enter a FA schedule number; it should populate all levels Sub Assembly Schedule Numbers under this.
I have thought of 1 approach; need suggestion if there is any better way to achieve this to improve the performance.
My Approach: In custom WIPTXCFM form once the FA schedule number is entered; I can fetch all below level data(e.g Level1, level2 ...etc.)
using a cursor with hierarchical query. Then loop through the cursor and insert them in the block after the FA record.
So would look for your suggestion if there is any other better way to achieve this.
Thanks in adv.
Regards.880860 wrote:
Hi All,
I would need to customize WIPTXCFM to populate all layered sub assembly schedule numbers against a Final Assembly schedule number.
Requirement is as soon as User enter a FA schedule number; it should populate all levels Sub Assembly Schedule Numbers under this.
I have thought of 1 approach; need suggestion if there is any better way to achieve this to improve the performance.
My Approach: In custom WIPTXCFM form once the FA schedule no is entered; I can fetch all below level data(e.g Level1, level2 ...etc.)
using a cursor with hierarchical query. Then loop through the cursor and insert them in the block after the FA record.
As per my findings; this hierarchical query takes longer to fetch the below levels data, around 1.5 mins.
Hello 880860,
If your are talking about EBS customization you can post at {forum:id=475}.
Hope this helps -
Problem with Hierarchical query
Gurus,
I have a problem with hierarchical query, which I am pasting below.
select sys_connect_by_path (Fname,'/')"PATH",Fname,id,level
,(SELECT COUNT(ID)-1 FROM (SELECT CONNECT_BY_ROOT LNAME LNAME,ID FROM CMT_PERSON
START WITH ID = 'emplo000000000126009'
CONNECT BY PRIOR ID=MANAGER_ID)
GROUP BY FNAME)"COUNT"
from CMT_PERSON
WHERE
LEVEL <= 4
----And ID='emplo000000000001877'
CONNECT BY PRIOR id=manager_id
----AND NOT LEVEL > 3
START WITH ID='emplo000000000126009'
As per the result, count is getting repeated for all the levels. That is, count is coming 16100 for every level, Can you please help where exactly I am going wrong
RegardsYou do not say anything about what count you want to get?
A wild guess could be:
select
sys_connect_by_path (p1.fname, '/') "PATH",
p1.fname,
p1.id,
level,
(select count (id) - 1
from
(select connect_by_root p2.lname lname, p2.id
from cmt_person p2
start with p2.id = p1.id
connect by prior p2.id = p2.manager_id)
) "COUNT"
from cmt_person p1
where level <= 4
connect by prior p1.id = p1.manager_id
start with p1.id = 'emplo000000000126009';Since your inner query simply starts with the hardcoded employee id, naturally it will give you the same count.
My guess is your inner query should start with the person id from the outer query?
If that is not the case - please state in plain english what you are trying to accomplish ;-)
(Oh, and please paste code within tags so we can read it more easily...)
Maybe you are looking for
-
Forward to voicemail prompts to sign in
I apologize for the trivial issue, but I am a Unity Connection newbie trying to fix an issue. After a call is ignored and forwarded to voicemail, Unity prompts the caller to enter a PIN rather than leave a message for the called user. When the PIN is
-
Hi! I have problem with asant build command. I am trying to do the J2EE ejb/converter example on Windows 2000 command line and I am getting the following error: Buildfile: build.xml init: prepare: [mkdir] Created dir: G:\Sun\j2eetutorial14\examples\e
-
Address Book Server will not sync all fields with iOS client
Hello, I am getting really frustrated with my Mac Mini Server. Apparently, the Address Book Server DOES NOT sync all fields. Only standard vCard-fields seem to synced. I have not successfully synced user-defined fields, such as an additional date. An
-
Hi, My Hp PSC 1215 all in 1 USB printer was working fine yesterday.. Today there are no printers in my printer list so I tried to add it back on and got the following error 'An error occurred while trying to add the selected printer client-error-requ
-
Crystal Reports 2008 Error Installing Service Pack 1
Hello, I am trying to install Service Pack 1 for Crystal Reports 2008 on Windows 2008 Server as an adminstrator and I get the following error message: "The feature you are trying to use is on a network resource that is unavailable. Click OK to try a