Adding dynamic 'start with' clause in Hierarchial query in Discoverer 4i
I have created a custom folder based upon the following hierarchial guery:
SELECT supervisor_name, level
FROM xxhr084_hr_emps_v
START WITH supervisor_name = 'TEST SUPER'
CONNECT BY prior person_id = supervisor_id
ORDER SIBLINGS by supervisor_name
I want the 'START WITH' value to be based upon a parameter i.e. the User enters the value of supervisor name at runtime.
However, I can't use a parameter within the custom folder and I can't generate the 'start with' condition in my workbook.
Anyone have a solution for populating the start with clause at run-time?
Thanks,
Kevin
Can you create the custom folder without a condition on supervisor_name - i.e. without the supervisor_name = 'X' starts with clause - in the EUL, and then define the condition on the supervisor_name field within the report?
SELECT supervisor_name, level
FROM xxhr084_hr_emps_v
CONNECT BY prior person_id = supervisor_id
ORDER SIBLINGS by supervisor_name
You can then restrict the results that the hierarchical query returns (from the custom folder) within the report, by adding a condition on supervisor_name.
Hope that makes some sense.
Similar Messages
-
Multiple Start with clause-Hiercharical Query
Hi,
Can we use more than 1 "start with" clause .In one of our requirement there can be a parent id can be null or not null
START WITH (gpo.orig_po_confirmation_no IS NULL OR gpo.orig_po_confirmation_no IS NOT NULL)
CONNECT BY PRIOR gpo.po_confirmation_no =gpo.orig_po_confirmation_no;When I executed I got below error
ORA-01436: CONNECT BY loop in user data
01436. 00000 - "CONNECT BY loop in user data"
*Cause:
*Action:Any suggestion to resolve the error
Regards,
AchyutAchyut K wrote:
Hi,
Can we use more than 1 "start with" clause .In one of our requirement there can be a parent id can be null or not nullNo you cant use more than one START WITH clause. But you can specify multiple CONDITION in the START WITH clause.
When I executed I got below error
ORA-01436: CONNECT BY loop in user data
01436. 00000 - "CONNECT BY loop in user data"
*Cause:
*Action:
To diagnose this we need to see the data. One quick try would be to using NOCYCLE in your CONNECT BY clause. -
'CONNECT BY PRIOR..START WITH' clause Usage
Hi All,
Could you please let me know the usage of 'connect by prior...start with' clause.
I only know that it helps for hierarchial retrival,but not aware of details.
Can someone provide the use with example for the same.
On searching on the net,I have seen numerous examples but none of them could explain it properly and everywhere the same SCOTT/TIGER schemas EMP and MGR table's example is given which is not enough explanatory.
Thanks in advance...
Aashish S.suppose u need to get all employees in a company in a hirerchical manner
ie presdent then mgrs
then employeess reporting to them
this can be done using connect by prior and start by
select empname from emp
connect by prior empno=mgrno
start with mgrno is null -
With clause in SQL query data model
Hello!
isn't it possible to use the with clause in sql query data models?
for example following query:
WITH
a_test as (
select dummy from dual
select *
from a_test
brings up a "XML Parsing Error: no element found"-error...
BR PaulI tried a slightly different query (see below) and the query worked fine and retrieved data.. I did not get any errors.
WITH
a_test as (
select 'dummy' from dual
select *
from a_test
This works as well.. retrieving the value of the parameter
WITH
a_test as (
select :Test_ID from dual
select *
from a_test
thanks,
BIPuser -
Start with Advanced mode in Query Panel
JDev 11.1.1.5.0 ADF BC
I have a query panel in my form with table made from View Criteria.
When runs, it starts with Basic mode.
I want this to be changed to start with Advanced mode straight away.
Is that possible?Hi,
Edit the ViewCriteria. In the UI Hints tab set Search Region Mode to Advanced.
Kevin
Message was edited by: KevinAngus - you beat me Timo! -
WITH clause and Custom Folders in Discoverer 10.1.2....
Hi ,
Is there any way to write a sql query using the WITH clause in Discoverer Administrator 10.1.2. custom folder....???
I mean something like this......
WITH sample_data as (select .........)
as
(select ..... from sample_data) which of course is valid in SQL*PLUS....
Thanks , a lot
SimonHi ,
sorry but i didn't make it working.....
I have written this WITH clause...
SQL> with sample_data as (select '13' as id , 'Y' as in_active from dual
2 union all
3 select '194' as id , 'Y' as in_active from dual
4 union all
5 select '792' as id , 'N' as in_active from dual)
6 select id , in_active from sample_data
7 /
ID IN_ACTIVE
13 Y
194 Y
792 NIn custom folder of Discoverer , i have written....
select id , in_active from (with select '13' as id , 'Y' as in_active from dual
union all
select '194' as id , 'Y' as in_active from dual
union all
select '792' as id , 'N' as in_active from dual)But an error occured....ORA-00903 invalid table name
Can somebody help me in this...?????
Thanks , a lot
Simon -
Adding h:dataTable with parameters in SQL query
Hi,
I've added a h:datatable to my web app in netbeans. I need this table to display records based on a sql query that contains a WHERE clause that has the currently logged-in user's id.
Further I need a column with buttons that each perform an operation based on the primary key of the selected row.
I have no Idea how to do any of this.
Could anyone please help where to start or point me to a good tutorial.
ThanksHi BalusC.
Thanks for the reply.
Coincidentally I discovered your blog shortly after posting this question, and it has been a great help, thanks!
I have another related problem now however. I am trying to display messages from the database, and then to allow a user to reply to these messages.
Everything displays correctly and when I click on the reply link the right text area becomes visible, but for some reason, the action event of the last command button does not fire.
Could you please help me figure out why it doesn't work.
I also don't know how to retrieve the message from the right text area input.
Thanks
Here's the code in my Home.jsp
<h:dataTable binding="#{Home.messageTable}" id="messageTable" value="#{Home.messages}" var="dataItem">
<h:column>
<f:facet name="header">
<h:outputText value=""/>
</f:facet>
<h:panelGroup layout="block" styleClass="message_main">
<h:outputText styleClass="message_label" value="FROM :"/>
<h:outputText styleClass="message_data" value="#{dataItem.sender}"/>
<h:outputText styleClass="message_label" value="DATE :"/>
<h:outputText styleClass="message_data" value="#{dataItem.dateString}"/>
<h:outputText styleClass="message_label" value="SUBJECT :"/>
<h:outputText styleClass="message_data" value="#{dataItem.subject}"/>
<h:outputText styleClass="message_label" value="MESSAGE :"/>
<h:outputText styleClass="message_body" value="#{dataItem.message}"/>
<h:commandLink styleClass="message_label" action="#{Home.selectMessage}" value="Reply" />
<h:panelGroup rendered="#{dataItem.selected}" layout="block" styleClass="reply_panel">
<h:outputText styleClass="message_label" value="Enter reply:"/>
<h:inputTextarea styleClass="message_body" />
<h:commandButton action="#{Home.sendReply}" styleClass="message_label" value="Send Reply" />
</h:panelGroup>
</h:panelGroup>
</h:column>
</h:dataTable>And the related methods
public String sendReply()
Message msg = (Message)messageTable.getRowData();
msg.sendReply(?);// Needs the string value of the correct text area
msg.setSelected(!msg.isSelected());
return null;
public String selectMessage()
Message msg = (Message)messageTable.getRowData();
msg.setSelected(!msg.isSelected());
return null;
} -
[10g] Need help with order by clause in hierarchical query
I have the following sample data:
CREATE TABLE bill_test1
( parent_part CHAR(25)
, child_part CHAR(25)
, line_nbr NUMBER(5)
, qty_per NUMBER(9,5)
INSERT INTO bill_test1 VALUES ('ABC-1','ABC-10',100,1);
INSERT INTO bill_test1 VALUES ('ABC-1','ABC-20',200,2);
INSERT INTO bill_test1 VALUES ('ABC-1','ABC-30',300,3);
INSERT INTO bill_test1 VALUES ('ABC-1','HARDWARE-1',401,10);
INSERT INTO bill_test1 VALUES ('ABC-1','HARDWARE-2',402,5);
INSERT INTO bill_test1 VALUES ('ABC-10','ABC-155',100,2);
INSERT INTO bill_test1 VALUES ('ABC-10','HARDWARE-1',200,1);
INSERT INTO bill_test1 VALUES ('ABC-155','RAW-2',100,4.8);
INSERT INTO bill_test1 VALUES ('ABC-155','HARDWARE-3',200,3);
INSERT INTO bill_test1 VALUES ('ABC-20','RAW-1',100,10.2);
INSERT INTO bill_test1 VALUES ('ABC-30','RAW-3',100,3);And the query below gives me exactly what I want, in the order I want it. However, I am wondering if there is a way to get this order without creating the SEQ column, since I don't need it in my results
SELECT part_nbr
, parent_part
, child_part
FROM (
SELECT CONNECT_BY_ROOT b.parent_part AS part_nbr
, b.parent_part
, b.child_part
, SYS_CONNECT_BY_PATH(b.line_nbr,' ') AS seq
FROM bill_test1 b
, dual
CONNECT BY parent_part = PRIOR child_part
WHERE part_nbr = 'ABC-1'
ORDER BY seq
Results of above query, except with SEQ included in SELECT (just to show what I'm sorting off of):
PART_NBR PARENT_PART CHILD_PART SEQ
ABC-1 ABC-1 ABC-10 100
ABC-1 ABC-10 ABC-155 100 100
ABC-1 ABC-155 RAW-2 100 100 100
ABC-1 ABC-155 HARDWARE-3 100 100 200
ABC-1 ABC-10 HARDWARE-1 100 200
ABC-1 ABC-1 ABC-20 200
ABC-1 ABC-20 RAW-1 200 100
ABC-1 ABC-1 ABC-30 300
ABC-1 ABC-30 RAW-3 300 100
ABC-1 ABC-1 HARDWARE-1 401
ABC-1 ABC-1 HARDWARE-2 402Hi,
As long as there's only one root, you can say ORDER SIBLINGS BY, but you can't do that in a sub-query (well, you can, but usually there's no point in doing it in a sub-query). If the CONNECT BY is being done in a sub-query, there is no guarantee that the main query will preserve the hierarchical order that the sub-query provides.
The query you posted doesn't require a suib-query, so you can say:
SELECT CONNECT_BY_ROOT b.parent_part AS part_nbr
, b.parent_part
, b.child_part
--, SYS_CONNECT_BY_PATH(b.line_nbr,' ') AS seq
FROM bill_test1 b
WHERE CONNECT_BY_ROOT b.parent_part = 'ABC-1'
CONNECT BY parent_part = PRIOR child_part
ORDER SIBLINGS BY b.line_nbr
;I said the query you posted doesn't require a sub-query. It also doesn't require dual, so I suspect what you posted is a simplification of what you're really doing, and that may need a sub-query. In particular, if you intend to GROUP BY part_nbr, then you need the sub-query. We can repeat the CONNECT_BY_ROOT expression in the WHERE clause (or, now that I think about it, use a START WITH clause instead of WHERE), but, for some reason, we can't use CONNECT_BY_ROOT in a GROUP BY clause; we need to compute CONNECT_BY_ROOT in a sub-query, give it a name (like part_nbr), and GROUP BY that column in a super-query.
This assumes that there is only one root node. ORDER SIBLINGS BY means just that: children of a common parent will appear in order, but the root nodes, who have no parents, will not necessarily be in order.
Here's what I meant by using START WITH instead of WHERE:
SELECT CONNECT_BY_ROOT b.parent_part AS part_nbr
, b.parent_part
, b.child_part
--, SYS_CONNECT_BY_PATH(b.line_nbr,' ') AS seq
FROM bill_test1 b
START WITH b.parent_part = 'ABC-1'
CONNECT BY parent_part = PRIOR child_part
ORDER SIBLINGS BY b.line_nbr
;This should be much more efficient, because it narrows down the results before you waste time getting their descendants.
Using a START WITH clause here is analagous to me sending you an e-mail, saying "Come to a meeting a my office at 3:00."
Using a WHERE clause here is analagous to me sending an e-mail to everyone in the company, saying "Come to a meeting a my office at 3:00", and then, as people get here, telling everyone except you that they can go back.
ORDER SIBLINGS BY was introduced in Oracle 9.
Edited by: Frank Kulash on Dec 9, 2010 2:39 PM
Added version with START WITH clause -
Hierarchical Query/START WITH/Remote Database error
Running on: EE 10.1.0.4.0 - 64bit
Remote DB: EE 11.1.0.6.0 - 64bit Production
Trying:
<PRE>
WITH
Moo
AS
SELECT
1
FROM
Dual@EPO11_Link A
CONNECT BY
LEVEL = 1
START WITH
EXISTS (SELECT * FROM Dual@EPO11_Link B WHERE A.Dummy = B.Dummy)
SELECT
FROM
Moo;
</PRE>
Getting:
ORA-00904: "A3"."$nso_col_1": invalid identifier
ORA-02063: preceding line from EPO11_LINK
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
- The CTE's query runs fine on its own.
- removing the @ from the first Dual@EPO11_Link , works.
- removing the @ from the second Dual@EPO11_Link , errors:
ORA-02016: cannot use a subquery in a START WITH on a remote database
02016. 00000 - "cannot use a subquery in a START WITH on a remote database"
*Cause:
*Action:
Error at Line: 10 Column: 24
- Removing the START WITH clause, works.
- Changing the START WITH clause's "=" to "IN", works.
- Changing the START WITH to EXISTS (SELECT * FROM Dual@EPO11_Link B WHERE A.Dummy = B.Dummy) results in:
ORA-00904: "A3"."DUMMY": invalid identifier
ORA-02063: preceding line from EPO11_LINK
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Am i missing something here?
Edited by: Brian Tkatch on Jan 19, 2012 1:27 PMSearching error messages books:
These have the ORA-02016:
8: http://docs.oracle.com/cd/A58617_01/server.804/a58312/newch228.htm#19589
9: http://docs.oracle.com/cd/A91202_01/901_doc/server.901/a90202/e1500.htm#1001663
These do not have the ORA-02016:
10R1: http://docs.oracle.com/cd/B14117_01/server.101/b10744/e1500.htm#ORA-02009
10R2: http://docs.oracle.com/cd/B19306_01/server.102/b14219/e1500.htm#ORA-02009
11R1: http://docs.oracle.com/cd/B28359_01/server.111/b28278/e1500.htm#ORA-02009
Starting with (heh) 10R1 a bunch of error messages were removed regarding hierarchical queries, including this one.
Is it allowed or not? -
Connect By query without a Start with
I have a database that takes a feed from a legacy system that has a rather illogical data structure that I need to tie together. The records chain together where the value in original_col equals the value of another records new_col. Using a connect by prior query I have the following results:-
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
with data_sample as (
SELECT 'A129994' AS ORIGINAL_COL ,'A129993' AS NEW_COL FROM DUAL
UNION
SELECT 'A129996' AS ORIGINAL_COL ,'A129994' AS NEW_COL FROM DUAL
UNION
SELECT 'A129999' AS ORIGINAL_COL ,'A129996' AS NEW_COL FROM DUAL
UNION
SELECT 'B129994' AS ORIGINAL_COL ,'B129993' AS NEW_COL FROM DUAL
UNION
SELECT 'B129996' AS ORIGINAL_COL ,'B129994' AS NEW_COL FROM DUAL
UNION
SELECT 'B129999' AS ORIGINAL_COL ,'B129996' AS NEW_COL FROM DUAL
UNION
SELECT 'C129994' AS ORIGINAL_COL ,'C129993' AS NEW_COL FROM DUAL
UNION
SELECT 'C129996' AS ORIGINAL_COL ,'C129994' AS NEW_COL FROM DUAL
select original_col, new_col, level as connect_by_level
CONNECT BY PRIOR original_col = new_col;
Results:-
ORIGINAL_COL NEW_COL CONNECT_BY_LEVEL
A129994 A129993 1
A129996 A129994 2
A129999 A129996 3
A129996 A129994 1
A129999 A129996 2
A129999 A129996 1
B129994 B129993 1
B129996 B129994 2
B129999 B129996 3
B129996 B129994 1
B129999 B129996 2
B129999 B129996 1
C129994 C129993 1
C129996 C129994 2
C129996 C129994 1The result I would like to have is just one chain for each set - however my issue is that I have no logical record for a START WITH clause.
A129994 A129993 1
A129996 A129994 2
A129999 A129996 3
B129994 B129993 1
B129996 B129994 2
B129999 B129996 3
C129994 C129993 1
C129996 C129994 2There is no specific record type that would be a start point such as a null parent, and the ID's used are in no logical order so I can't use the min value or a group b. Any pointers gratefully received.
Edited by: jweb on 12-Jul-2012 04:51I think you need found root for your tree
check following example
with data_sample as
(select 'A129994' as ORIGINAL_COL, 'A129993' as NEW_COL
from DUAL
union
select 'A129996' as ORIGINAL_COL, 'A129994' as NEW_COL
from DUAL
union
select 'A129999' as ORIGINAL_COL, 'A129996' as NEW_COL
from DUAL
union
select 'B129994' as ORIGINAL_COL, 'B129993' as NEW_COL
from DUAL
union
select 'B129996' as ORIGINAL_COL, 'B129994' as NEW_COL
from DUAL
union
select 'B129999' as ORIGINAL_COL, 'B129996' as NEW_COL
from DUAL
union
select 'C129994' as ORIGINAL_COL, 'C129993' as NEW_COL
from DUAL
union
select 'C129996' as ORIGINAL_COL, 'C129994' as NEW_COL from DUAL)
select original_col, new_col, level as connect_by_level
from (select ds.*,
case (select count(*)
from data_sample dsc
where dsc.original_col = ds.new_col
and rownum < 2)
when 0 then
1
else
0
end as is_root -- try find parent in table if parent not exists this record is root
from data_sample ds) ds
start with is_root = 1
connect by prior original_col = new_col;good luck -
OPEN CURSOR using a WITH clause in the select query
Hi,
I am using Oracle 9i. I have a requirement where I have a REFCURSOR as an OUT parameter for my procedure. I have declared the TYPE and created the procedure.
In the procedure, I am using OPEN <cursor_name> FOR <query>;
Ideally this works in most of the cases that I have tried earlier. However, in the current case I am using a WITH clause in my query to get the results.
I need help in understanding if the above mentioned syntax would not allow me to use the WITH clause in the query.What error do you get , seems to work ok for me on 10g
SQL> begin
2 open :cv for 'with x as (select * from emp) select * from x';
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> print :cv
EMPNO
ENAME
JOB MGR HIREDATE SAL COMM DEPTNO
7521
WARD
SALESMAN 7698 22-FEB-81 1250 500 30
7566
JONES
MANAGER 7839 02-APR-81 2975 20
EMPNO -
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 -
A slightly different running sum in a hierarchy query
Hi,
I've got stuck with this problem since quite some time, so pls help:
I am basically trying to rollup/aggregate values from the lower level nodes to the upper level nodes in a hierarchy. But this aggregate is a bit different than a plain rollup of values.
create table ent_rel (parent number, child number, child_amount number);
insert into ent_rel
select null, 100, null from dual
union all select 100, 101, null from dual
union all select 101, 102, 20 from dual
union all select 102, 103, null from dual
union all select 103, 104, null from dual
union all select 104, 105, null from dual
union all select 105, 106, 40 from dual
union all select 106, 107, null from dual
union all select 107, 108, 20 from dual
union all select 107, 109, 10 from dual
union all select 101, 203, null from dual
union all select 203, 205, 50 from dual
union all select 205, 207, null from dual
union all select 207, 209, null from dual
union all select 209, 210, 10 from dual
commit;
select RPAD(' ', (LEVEL - 1) * 2, '-') || child child
,level
,child_amount
from ent_rel
start with parent is null
connect by prior child = parent
CHILD LEVEL CHILD_AMOUNT
100 1
--101 2
----102 3 20
------103 4
--------104 5
----------105 6
------------106 7 40
--------------107 8
----------------108 9 20
----------------109 9 10
----203 3
------205 4 50
--------207 5
----------209 6
------------210 7 10
Now, I need a SQL query or even a simple PL/SQL code to get the DESIRED_AMOUNT field as shown below:
Pls note that there might be many more parallel branches to a node, but I've kept it simple just to get the concept.
CHILD LEVEL CHILD_AMOUNT DESIRED_AMOUNT
100 1 70
--101 2 70
----102 3 20 20
------103 4 40
--------104 5 40
----------105 6 40
------------106 7 40 40
--------------107 8 30
----------------108 9 20 20
----------------109 9 10 10
----203 3 50
------205 4 50 50
--------207 5 10
----------209 6 10
------------210 7 10 10
Thanks a lot in advance.
thanks and regards,
KnlHi,
Welcome to the forum!
Thanks for posting the CREATE TABLE and INSERT statements! That's very helpful.
It's also helpful if you format your code and output. When posting formatted text on this site, type these 6 characters:
\(all small letters, inside curly brackets) to keep the site from compressing the spaces.
You can do two CONNECT BY queries: one to get the desired amount, and the other like you're already doing, to get the indented child column. The first will differ in that it has no START WITH clause, which means every node in your table will be the root of a tree, and that there's an additional CONNECT BY condition to quit looking for descendants as soon as an amount is found. This corresponds to your apparant requirement that the desired_amount is the amount, if it is not NULL, and only otherwise is the sum of the desired_amounts of one's descendants.WITH desired_amount_tree AS
SELECT CONNECT_BY_ROOT child AS root
, child
, child_amount
FROM ent_rel
CONNECT BY PRIOR child = parent
AND PRIOR child_amount IS NULL
, got_desired_amount AS
SELECT root
, SUM (child_amount) AS desired_amount
FROM desired_amount_tree
GROUP BY root
select RPAD ( ' '
, (LEVEL - 1) * 2
) || e.child AS child
, level
, e.child_amount
, desired_amount
from ent_rel e
JOIN got_desired_amount d ON e.child = d.root
start with parent is null
connect by prior child = parent
It's usually more efficient to do join after doing the CONNECT BY. For better performance, try doing your original query by itself, and then joining its result set to got_desired_amount.
Of course, it would be a lot more efficient if we could eliminate the extra CONNECT BY query. That might be possible using MODEL or (starting in Oracle 11.2) a recursive sub-query. -
Hierarchy Query - Removing Child Nodes
Hopefully an easy one for someone practiced with hierarchy queries.
If I have a hierarchy:
A
B
CThe where clause restricts the result so that B is removed, I now have:
A
CI would like to make it so that if you cut off a node, the entire branch beneath it is cut. In this example, only A should be returned.
My experience with hierarchy queries is limited, and I've searched high-and-low for an example or discussion to make this happen using a standard method. I'm sure I can make it happen, but if there is an established/standard (aka efficient) way of making this happen I'd prefer to do that. Everything I have found references making C show up...that's obviously not what I'm having a problem doing.
Here's an example:
CREATE TABLE Z_HIERARCHY (
test_id NUMBER,
label VARCHAR2(50),
parent_id NUMBER);
INSERT INTO Z_HIERARCHY VALUES (1, 'A', 0);
INSERT INTO Z_HIERARCHY VALUES (2, 'B', 1);
INSERT INTO Z_HIERARCHY VALUES (3, 'C', 2);
COMMIT;
SELECT label, level
FROM z_hierarchy
START WITH parent_id = 0
CONNECT BY PRIOR test_id = parent_id;
Result:
A
B
C
SELECT label, level
FROM z_hierarchy
WHERE test_id <> 2
START WITH parent_id = 0
CONNECT BY PRIOR test_id = parent_id;
Result:
A
CWhat I want is simply A. C is dependent on a parent that isn't in the result set...don't want it shown either.
Thanks in advance!
RonHi, Ron,
So you want to pretend, just for this query, that the row with test_id=2 doesn't exist? Then do what Tubby suggested: run the query, not on the full z_hierarchy table, but on a copy of the table where that row doesn't exist.
You could also do something like this:
SELECT label
, LEVEL
FROM z_hierarchy
START WITH parent_id = 0
CONNECT BY parent_id = PRIOR test_id
AND test_id != 2
;but, depending on your requirements, you might need to put a similar condition in the START WITH clause. In general, Tubby's solution is best.
rmhardma wrote:
... No other built-in way to do this without doing the inline view, eh?Sure: you can always replace an in-line view with a WITH clause:
WITH good_rows_only AS
select *
FROM z_hierarchy
WHERE test_id != 2
SELECT label, level
from good_rows_only
START WITH parent_id = 0
CONNECT BY PRIOR test_id = parent_id;Pehaps you meant "without a sub-query". -
START WITH and CONNECT BY in Oracle SQL ( hierarchical relationship)
Hi, the original table as below
Customer_ID Account_ID Paying_Account_ID Parent_Account_ID Company_ID
158 158 158 158 0
159 159 158 158 0
160 160 158 158 0
181 181 181 181 0
183 183 183 183 0
24669 24669 24669 24669 0
24671 24671 24671 24669 0
24670 24670 24670 24669 0
3385127 3385127 3385127 24670 0To identify the hierarchical relationship of the data, which are PARENT_ACCOUNT_ID & ACCOUNT_ID, below is the query that I was used.
select lpad(' ', 2*level) || A.ACCOUNT_ID AS LEVEL_LABEL, CONNECT_BY_ISCYCLE "Cycle", LEVEL, A.* from ACCOUNT A
START WITH parent_account_id = account_id
CONNECT BY NOCYCLE PRIOR A.ACCOUNT_ID = A.PARENT_ACCOUNT_ID
AND account_id != parent_account_id
;This is the result from the query
Level_Label Level Cycle Customer_ID Account_ID Paying_Account_ID Parent_Account_ID Company_ID
158 1 0 158 158 158 158 0
159 2 0 159 159 158 158 0
160 2 0 160 160 158 158 0
181 1 0 181 181 181 181 0
183 1 0 183 183 183 183 0
24669 1 0 24669 24669 24669 24669 0
24671 2 0 24671 24671 24671 24669 0
24670 2 0 24670 24670 24670 24669 0
3385127 3 0 3385127 3385127 3385127 24670 0My questions is how can I modified the query in order to calcuate the values for:
My_Total_PR - Number of my child PR accounts which doest not include itself.
Total_PR - Total number of PR accounts in the overall structure
My_Total_NPR - Number of my child NPR accounts which doest not include itself.
Total_NPR - Total number of NPR accounts in the overall structure
*PR stand for payment responsible, for instance the payment responsible for Account 158 is 158 (Paying_Account_ID), so the Total_PR for 158 is 3 (158, 159, 160)
*NPR stand for Non payment responsible, for instance the payment responsible for Account 159 is 158 (Paying_Account_ID), so the Total_NPR for 159 is 1
This is the expected result, Any advice much appreciated. Thanks
Level_Label Level Cycle My_Total_PR Total_PR My_Total_NPR Total_NPR Paying_Account
158 1 0 2 3 0 0 158
159 2 0 0 0 0 1 158
160 2 0 0 0 0 1 158
181 1 0 0 1 0 0 181
183 1 0 0 1 0 0 183
24669 1 0 0 1 3 3 24669
24671 2 0 0 1 0 0 24671
24670 2 0 0 1 1 1 24670
3385127 3 0 0 1 0 0 3385127Edited by: user11432758 on 14-Feb-2012 01:00
Edited by: user11432758 on 14-Feb-2012 07:05Hi,
user11432758 wrote:
Hi below is the DDL statment, thanks
CREATE TABLE "SYSTEM"."ACCOUNT" ...
Don't create your own objects in the SYSTEM schema, or any schema that comes with the database. Create a separate schema, and put your objects into it. You'll have fewer security problems, and migrating to a new database will be easier.
Here's one way to can get the aggregates that you want:
WITH got_descendants AS
SELECT CONNECT_BY_ROOT account_id AS ancestor_id
, paying_account_id
, LEVEL AS lvl
FROM account
CONNECT BY NOCYCLE PRIOR account_id = parent_account_id
AND account_id != parent_account_id
SELECT ancestor_id
, COUNT (CASE WHEN lvl > 1
AND ancestor_id = paying_account_id THEN 1 END) AS my_total_pr
, COUNT (CASE WHEN ancestor_id = paying_account_id THEN 1 END) AS total_pr
, COUNT (CASE WHEN lvl > 1
AND ancestor_id != paying_account_id THEN 1 END) AS my_total_npr
, COUNT (CASE WHEN ancestor_id != paying_account_id THEN 1 END) AS total_npr
FROM got_descendants
GROUP BY ancestor_id
;Output:
` MY_ MY_
TOTAL TOTAL TOTAL TOTAL
ANCESTOR_ID _PR _PR _NPR _NPR
158 2 3 0 0
159 0 0 0 1
160 0 0 0 1
181 0 1 0 0
183 0 1 0 0
24669 0 1 3 3
24670 0 1 1 1
24671 0 1 0 0
3385217 0 1 0 0This gives you the right numbers, but how can we get them in an order that reflects the hierarchy, with the columns (such as lvl) that are derived from the hierarchy?
One way would be to do two CONNECT BY queries; one without a START WITH clause (like the one above) that gets the aggregates, and the other with a START WITH clause (like your original query), that is in the right order, and has columns such as level_label and level. We could join the result sets and get exactly what we want. I'll leave that as an exercise.
Here's another way, that gets the right results with only one CONNECT BY query:
WITH got_descendants AS
SELECT CONNECT_BY_ROOT account_id AS ancestor_id
, paying_account_id
, account_id
, LEVEL AS lvl
, CONNECT_BY_ISCYCLE AS cycle
, CASE
WHEN CONNECT_BY_ROOT account_id
= CONNECT_BY_ROOT parent_account_id
THEN ROWNUM
END AS r_num
FROM account
CONNECT BY NOCYCLE PRIOR account_id = parent_account_id
AND account_id != parent_account_id
ORDER SIBLINGS BY account_id -- Optional
, got_o_num AS
SELECT got_descendants.*
, MIN (r_num) OVER (PARTITION BY account_id) AS o_num
, MAX (lvl) OVER (PARTITION BY account_id) AS max_lvl
FROM got_descendants
SELECT LPAD ( ' '
, 2 * (MIN (max_lvl) - 1)
) || ancestor_id AS level_label
, MIN (max_lvl) AS "Level"
, MIN (cycle) AS "Cycle"
, COUNT (CASE WHEN lvl > 1
AND ancestor_id = paying_account_id THEN 1 END) AS my_total_pr
, COUNT (CASE WHEN ancestor_id = paying_account_id THEN 1 END) AS total_pr
, COUNT (CASE WHEN lvl > 1
AND ancestor_id != paying_account_id THEN 1 END) AS my_total_npr
, COUNT (CASE WHEN ancestor_id != paying_account_id THEN 1 END) AS total_npr
, MIN (paying_account_id) AS paying_account
FROM got_o_num
GROUP BY ancestor_id
ORDER BY MIN (o_num)
;Output:
` MY_ MY_
TOTAL TOTAL TOTAL TOTAL PAYING_
LEVEL_LABEL Level Cycle _PR _PR _NPR _NPR ACCOUNT
158 1 0 2 3 0 0 158
159 2 0 0 0 0 1 158
160 2 0 0 0 0 1 158
181 1 0 0 1 0 0 181
183 1 0 0 1 0 0 183
24669 1 0 0 1 3 3 24669
24670 2 0 0 1 1 1 24670
3385217 3 0 0 1 0 0 3385217
24671 2 0 0 1 0 0 24671This is exactly what you requested, except that you posted the row with level_label=' 24671' before the row with level_label=' 24671'. You may not care which of those comes first, but if that's important, explain why those rows need to be in descending order by account_id, while '159 and '160' are in ascending order. You'll need to change the ORDER SIBLINGS BY clause accordingly.
Maybe you are looking for
-
I am currently using firefox 6.0.2. and i am unable to use Sothink Web Video downloader that is compatiable for the crrent version of firefox. I've tried updating add-ons and looking for a suitable version of the web downloader , but im unable to fin
-
Errors On Upstream????
Hey guys, I am wondering if anyone else is having this issue. i have recently just moved to BT BB and excpet for a few wirless issue to start with i am very happy But.............................. i seem to have an amazing amount of CRC's errors on m
-
Initial Parameter DB_WRITER_PROCESSES Doesn't work
I set the initial parameter DB_WRITER_PROCESSES as 5 in init.ora file. But when I restarted the database, I found the parameter show as 1 and only DBW0 existed. I don't know the reason, would you please help me? Thanks and Best Regards, Su Qian
-
Hi Experts, I was trying to make a exit in BW but the code is not working properly. The problem is ...it is not populating ZVAR_SUPPLY_PLANT into I_T_VAR_RANGE , which is a SAP defined parameter.As because of that the code fails.How to make the value
-
How to display different screens when starting the Webdynpro app?
Hi All, When user starting up the Webdynpro app, it will first check some value in the persistent layer. If the check result is ok, it will show the normal app screen to the user; if the check failed, it will show the error screen. Now I write the ch