Home » SQL & PL/SQL » SQL & PL/SQL » Hierarchical query, rolling up value from child to parent (Oracle Database 10g Enterprise Edition Release 10.2.0.4.0)
Hierarchical query, rolling up value from child to parent [message #619782] Fri, 25 July 2014 01:08 Go to next message
hendrif2
Messages: 6
Registered: July 2014
Junior Member
Hi

I have the resultset below :-

drop table tStatus;

create table tStatus(
 id number
 , pid number
 , name varchar2(20)
 , status varchar2(1)
 , result varchar2(4)
);


insert into tStatus values(1,null, 'A', 'N', null);
insert into tStatus values(2, 1, 'B', 'Y', 'PASS');
insert into tStatus values(3, 2, 'C', 'Y', 'FAIL');

commit;

select * from tStatus
connect by prior id = pid
start with pid is null;

        ID        PID NAME                 STATUS RESULT
---------- ---------- -------------------- ------ ------
         1            A                    N             
         2          1 B                    Y      PASS   
         3          2 C                    Y      FAIL   



I would like to rollup a child's Result value to it's parent element if the Status column is set to "Y".
If the Status column is not "Y", then it should not rollup any further, I have added example output
to assist with explanation.

This is an example of the desired output for the information above:-


        ID        PID NAME                 STATUS RESULT
---------- ---------- -------------------- ------ ------
         1            A                    N      FAIL   
         2          1 B                    Y      FAIL   
         3          2 C                    Y      FAIL   



Another example :-

 truncate table tStatus;

 insert into tStatus values(1,null, 'A', 'N', null);
 insert into tStatus values(2, 1, 'B', 'N', 'PASS');
 insert into tStatus values(3, 2, 'C', 'Y', 'FAIL');

commit;

select * from tStatus
connect by prior id = pid
start with pid is null;

        ID        PID NAME                 STATUS RESULT
---------- ---------- -------------------- ------ ------
         1            A                    N             
         2          1 B                    N      PASS   
         3          2 C                    Y      FAIL   



Desired output :-

        ID        PID NAME                 STATUS RESULT
---------- ---------- -------------------- ------ ------
         1            A                    N             
         2          1 B                    N      FAIL   
         3          2 C                    Y      FAIL   



Any help would be greatly appreciated.

Regards
Faadiel
Re: Hierarchical query, rolling up value from child to parent [message #619801 is a reply to message #619782] Fri, 25 July 2014 02:16 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Is ID always increasing (no PID greater than ID)?
Can there be several pid NULL in the table?

Re: Hierarchical query, rolling up value from child to parent [message #619805 is a reply to message #619801] Fri, 25 July 2014 02:19 Go to previous messageGo to next message
hendrif2
Messages: 6
Registered: July 2014
Junior Member
Hi Michel

ID always increases (sequence).
There can be several null PID indicating multiple root level structures.
Re: Hierarchical query, rolling up value from child to parent [message #619808 is a reply to message #619805] Fri, 25 July 2014 02:26 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Last question, is FAILED (if any) always the last row of a branch (there can be no other row after the first FAILED, we also mean there can be no more than one FAILED in a branch)?

[Edit: "no more than one" not "more than one", word missing]

[Updated on: Fri, 25 July 2014 02:27]

Report message to a moderator

Re: Hierarchical query, rolling up value from child to parent [message #619810 is a reply to message #619808] Fri, 25 July 2014 02:33 Go to previous messageGo to next message
hendrif2
Messages: 6
Registered: July 2014
Junior Member
There can be multiple FAILED elements in a branch. If 1 of the elements of a branch is FAILED, then the entire branch is regarded as FAILED. (Hope that makes sense). A FAILED element of a branch supersedes/dominates a PASSED element.
Re: Hierarchical query, rolling up value from child to parent [message #619811 is a reply to message #619810] Fri, 25 July 2014 02:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I merged both examples in one test case:
create table tStatus(
 id number
 , pid number
 , name varchar2(20)
 , status varchar2(1)
 , result varchar2(4)
);

insert into tStatus values(1,null, 'A', 'N', null);
insert into tStatus values(2, 1, 'B', 'Y', 'PASS');
insert into tStatus values(3, 2, 'C', 'Y', 'FAIL');

insert into tStatus values(4,null, 'A', 'N', null);
insert into tStatus values(5, 4, 'B', 'N', 'PASS');
insert into tStatus values(6, 5, 'C', 'Y', 'FAIL');

commit;

I added 2 columns in the output to show how the query works: the original result and the previous status:
SQL> select id, pid,name, status, result,
  2         decode(prior status,
  3                'N', result,
  4                last_value(nullif(result,'PASS') ignore nulls)
  5                  over (partition by connect_by_root id order by id desc))
  6           result,
  7         prior status
  8  from tStatus
  9  connect by prior pid = id
 10  start with id not in (select nvl(pid,-999) from tstatus)
 11  order by connect_by_root id, id
 12  /
        ID        PID NAME                 S RESU RESU P
---------- ---------- -------------------- - ---- ---- -
         1            A                    N      FAIL Y
         2          1 B                    Y PASS FAIL Y
         3          2 C                    Y FAIL FAIL
         4            A                    N           N
         5          4 B                    N PASS FAIL Y
         6          5 C                    Y FAIL FAIL

Re: Hierarchical query, rolling up value from child to parent [message #619813 is a reply to message #619811] Fri, 25 July 2014 02:43 Go to previous messageGo to next message
hendrif2
Messages: 6
Registered: July 2014
Junior Member
Michel, thanks for the assistance, much appreciated.
Re: Hierarchical query, rolling up value from child to parent [message #619814 is a reply to message #619811] Fri, 25 July 2014 02:56 Go to previous messageGo to next message
hendrif2
Messages: 6
Registered: July 2014
Junior Member
Michel,

I am getting a different result from the query

        ID        PID NAME                 STATUS RESULT RESULT PRIORSTATUS
---------- ---------- -------------------- ------ ------ ------ -----------
         1            A                    N             FAIL               
         2          1 B                    Y      PASS   FAIL               
         3          2 C                    Y      FAIL   FAIL               
         4            A                    N             FAIL               
         5          4 B                    N      PASS   FAIL               
         6          5 C                    Y      FAIL   FAIL               


Re: Hierarchical query, rolling up value from child to parent [message #619815 is a reply to message #619811] Fri, 25 July 2014 03:13 Go to previous messageGo to next message
hendrif2
Messages: 6
Registered: July 2014
Junior Member
Hi
I found the problem, it is the version of Oracle that we running on.

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production

        ID        PID NAME                 STATUS RESULT RESULT PRIORSTATUS
---------- ---------- -------------------- ------ ------ ------ -----------
         1            A                    N             FAIL   Y           
         2          1 B                    Y      PASS   FAIL   Y           
         3          2 C                    Y      FAIL   FAIL               
         4            A                    N                    N           
         5          4 B                    N      PASS   FAIL   Y           
         6          5 C                    Y      FAIL   FAIL               



as expected.

Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit

        ID        PID NAME                 STATUS RESULT RESULT PRIORSTATUS
---------- ---------- -------------------- ------ ------ ------ -----------
         1            A                    N             FAIL               
         2          1 B                    Y      PASS   FAIL               
         3          2 C                    Y      FAIL   FAIL               
         4            A                    N             FAIL               
         5          4 B                    N      PASS   FAIL               
         6          5 C                    Y      FAIL   FAIL               



I narrowed the problem down to 10g not liking
last_value(nullif(result,'PASS') ignore nulls) over (partition by connect_by_root id order by id desc)
Re: Hierarchical query, rolling up value from child to parent [message #619858 is a reply to message #619815] Fri, 25 July 2014 10:09 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Sorry for late, I was off, yes, I also found there is a bug in 10.2.0.4 as I didn't version your version I posted a copy of my 11.2.0.1 session.
In 10.2.0.4, you can use the following workaround:
SQL> with 
  2    data as (
  3      select id, pid, name, status, result, prior status pstatus, connect_by_root id rid
  4      from tStatus
  5      connect by prior pid = id
  6      start with id not in (select nvl(pid,-999) from tstatus)
  7    )
  8  select id, pid, name, status,
  9         decode(pstatus, 
 10                'N', result, 
 11                last_value(nullif(result,'PASS') ignore nulls)
 12                  over (partition by rid order by id desc))
 13           result,
 14         pstatus
 15  from data
 16  order by rid, id
 17  /
        ID        PID NAME                 S RESU P
---------- ---------- -------------------- - ---- -
         1            A                    N FAIL Y
         2          1 B                    Y FAIL Y
         3          2 C                    Y FAIL
         4            A                    N      N
         5          4 B                    N FAIL Y
         6          5 C                    Y FAIL

Also I concentrate on FAIL status an omit the case where all is PASS, I think (but didn't check) a little bit of modification is required in this case.

Edit: my query also assumes that there can't be any Y status before a N one (in the order from pid=null to leaf displayed here). This also leads to a question (I didn't check) does the query works if all status are Y?
So the importance of a good test case.

[Updated on: Fri, 25 July 2014 10:14]

Report message to a moderator

Re: Hierarchical query, rolling up value from child to parent [message #619861 is a reply to message #619858] Fri, 25 July 2014 10:25 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Michel, just a friendly suggestion, how about you upgrading from 11.2.0.1 to the latest version Smile Is this idea already on the cards? Or do you manage to have multiple databases with different versions in your laptop, of course it would make the system incredibly slow. I saw John recently upgrading to 12c. Just to see you sharing your wealth of knowledge on latest version and it's features.

Michel, just a thought, if you think it is good idea to move my post to community hangout, we could carry on with further discussions about good and bad with latest Oracle version. One such topic is already in suggestions and feedback being actively driven by Ross and John. What's your take?
Re: Hierarchical query, rolling up value from child to parent [message #619864 is a reply to message #619861] Fri, 25 July 2014 10:33 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
how about you upgrading from 11.2.0.1 to the latest version


It is my laptop and the reason is just laziness. Smile

Quote:
Or do you manage to have multiple databases with different versions in your laptop, of course it would make the system incredibly slow


Yes, I have 8.1.7, 9.2.0.8, 10.2.0.4 (yes I have 10.2.0.5 patchset but for same reason didn't apply it), 11.2.0.1 (and also 11.1.0.7 but I never activate it as there is now almost no more question on this version).

And this is the reason I unfortunately have not 12c because it is a 32-bit laptop.

Re: Hierarchical query, rolling up value from child to parent [message #619866 is a reply to message #619864] Fri, 25 July 2014 10:40 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
32 bit?

And I was afraid I am the only one using 8 years old personal computer with 32 bit OS. And it made me so upset when Oracle released 12c for Windows only on 64 bit system Sad
Re: Hierarchical query, rolling up value from child to parent [message #619950 is a reply to message #619866] Sat, 26 July 2014 09:19 Go to previous message
Kevin Meade
Messages: 2103
Registered: December 1999
Location: Connecticut USA
Senior Member
How would you guys expect to do an IN-MEMORY database when you can only directly reference 3GB of memory?
Previous Topic: How to Restrict User from Connecting to Database Through Specific hostname
Next Topic: All Child tables of given table
Goto Forum:
  


Current Time: Thu Apr 25 15:38:51 CDT 2024