niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Databases » Oracle
aggregation over two hierarchies
Post new topic   Reply to topic Page 1 of 1 [11 Posts] View previous topic :: View next topic
Author Message
mikharakiri_nospaum@yahoo
*nix forums beginner


Joined: 05 Feb 2005
Posts: 46

PostPosted: Fri Feb 25, 2005 4:05 pm    Post subject: Re: aggregation over two hierarchies Reply with quote

navrsale wrote:
Quote:
thanks for observation. Here is a more complex example of data in the
JOB table. Is this directed acyclic graph? Or cyclic as there is
closure between 12-10-5-9-11-12 nodes and 16-15-12-11-9-0-14-16?

It's acyclic as directed graph and cyclic if you ignore directions.

Quote:
http://img23.exs.cx/my.php?loc=img23&image=directedgraph11th.png

Isn't it

1,2, and 10 merge into 5?
-----^^^^^^

Quote:
Each node must be visited and the value prorated in recursive
fashion:
foreach node
-start with a node, for example 16
-visit each hierarchy on which 16 depends, in this case hierarchies
for
14 and 15, SUM their values and the current value of node 16, and
that
will be new, prorated value for node 16
-repeat this recursively for each sub-hierarchy
until all nodes are prorated

Is it hierarchical total that you are trying to calculate? In graph
terms its a flow from one node into the other. For all paths from one
node into the other, calculate aggregate value along the each path (a
flow along the path), and add the path values together.
Back to top
navrsale
*nix forums beginner


Joined: 22 Feb 2005
Posts: 5

PostPosted: Thu Feb 24, 2005 10:33 am    Post subject: Re: aggregation over two hierarchies Reply with quote

thanks for observation. Here is a more complex example of data in the
JOB table. Is this directed acyclic graph? Or cyclic as there is
closure between 12-10-5-9-11-12 nodes and 16-15-12-11-9-0-14-16?

http://img23.exs.cx/my.php?loc=img23&image=directedgraph11th.png

Each node must be visited and the value prorated in recursive fashion:
foreach node
-start with a node, for example 16
-visit each hierarchy on which 16 depends, in this case hierarchies for
14 and 15, SUM their values and the current value of node 16, and that
will be new, prorated value for node 16
-repeat this recursively for each sub-hierarchy
until all nodes are prorated
Back to top
GreyBeard
*nix forums Guru Wannabe


Joined: 10 Mar 2005
Posts: 182

PostPosted: Wed Feb 23, 2005 9:19 pm    Post subject: Re: aggregation over two hierarchies Reply with quote

On Wed, 23 Feb 2005 10:45:52 -0800, Rauf Sarwar wrote:

Quote:
snip
(but bare in mind that
these days employers put emphasis on character more than on technical
skills)

That makes lot's of sense....

"Your resume says that you have excellent character and personality but
have absolutely no technical skills whatsoever?... Good, we recently
fired our highly skilled DBA because his attitude sucked so that we can
hire someone like you to manage our mission critical database."

Regards
/Rauf

Seen it happen.

Then they send the newbie to classes (thank you for the revenue) and
expect newbie to become competent after a week {bwaaah ha ha ha)

lol/FGB
Back to top
mikharakiri_nospaum@yahoo
*nix forums beginner


Joined: 05 Feb 2005
Posts: 46

PostPosted: Wed Feb 23, 2005 8:19 pm    Post subject: Re: aggregation over two hierarchies Reply with quote

navrsale wrote:
Quote:
In my application there are two given tables:

JOB table:

job_id open closed splitfrom mergedto
=================================================
AAAA 1/1/90 1/1/00
BBBB 1/1/80 1/1/00
CCCC 1/1/00 1/1/01 AAAA EEEE
DDDD 1/1/00 1/1/01 BBBB EEEE
EEEE 1/1/01
FFFF 1/1/01 CCCC

create table job (
job_id char(4) NOT NULL,
open_dte date not null,
close_dte date,
split_from char(4),
merge_to char(4),
PRIMARY KEY (job_id),
FOREIGN KEY (split_from) REFERENCES job(job_id),
FOREIGN KEY (merge_to) REFERENCES job(job_id)
)
/
INSERT INTO job VALUES( 'AAAA', to_date('01/01/90', 'MM/DD/RR'),
to_date('01/01/00', 'MM/DD/YY'), null, null )
/
INSERT INTO job VALUES( 'BBBB', to_date('01/01/80', 'MM/DD/RR'),
to_date('01/01/00', 'MM/DD/YY'), null, null )
/
INSERT INTO job VALUES( 'CCCC', to_date('01/01/00', 'MM/DD/RR'),
to_date('01/01/01', 'MM/DD/YY'), 'AAAA', 'EEEE' )
/
INSERT INTO job VALUES( 'DDDD', to_date('01/01/00', 'MM/DD/RR'),
to_date('01/01/01', 'MM/DD/YY'), 'BBBB', 'EEEE' )
/
INSERT INTO job VALUES( 'EEEE', to_date('01/01/01', 'MM/DD/RR'),
null,
null,
null )
/
INSERT INTO job VALUES( 'FFFF', to_date('01/01/01', 'MM/DD/RR'),
null,
'CCCC',
null )
/
commit
/


and JOB_REPORT table:

job_id job_name YR1 YR2 YR3 YR4 YR5
======================================================
AAAA name1 6 5 4 3 2
BBBB name2 5 4 3 2 1
CCCC name3 3 2 1 2 3
DDDD name4 7 6 5 4 3
EEEE name5 10 9 8 7 6
FFFF name6 3 4 5 6 7


create table job_report (
job_id char(4) NOT NULL,
job_name char(5) not null,
yr1 integer,
yr2 integer,
yr3 integer,
yr4 integer,
yr5 integer
PRIMARY KEY (job_id)
)
/

INSERT INTO job_report VALUES( 'AAAA', 'name1', 6,5,4,3,2 )
/
INSERT INTO job_report VALUES( 'BBBB', 'name2', 5,4,3,2,1 )
/
INSERT INTO job_report VALUES( 'CCCC', 'name3', 3,2,1,2,3 )
/
INSERT INTO job_report VALUES( 'DDDD', 'name4', 7,6,5,4,3 )
/
INSERT INTO job_report VALUES( 'EEEE', 'name5', 10,9,8,7,6 )
/
INSERT INTO job_report VALUES( 'FFFF', 'name6', 3,4,5,6,7 )
/
commit
/


JOB table describes different jobs and their history (some jobs are
split into another job and some jobs are merged into a new job). For
example this hierarchical query describes history inside JOB table:

column change_history format a50
column participant format a12
set pagesize 66
accept p_job_id prompt 'enter job id, 0 for all: '

select distinct DEPTH,
CHANGE_HISTORY,
substr( change_history, instr( change_history, '/',
-1,
1 ) + 1 ) PARTICIPANT,
decode( DEPTH - 1, 0, null, EVENT ) EVENT
from (
select level DEPTH,
sys_connect_by_path( job_id, '/' ) CHANGE_HISTORY,
'SPLIT' EVENT
from job
start with DECODE( '&p_job_id', '0', '1', job_id ) = decode(
'&p_job_id', '0', '1', '&p_job_id')
connect by prior split_from = job_id
union all
select level DEPTH,
sys_connect_by_path( job_id, '/' ) CHANGE_HISTORY,
'MERGE' EVENT
from job
start with DECODE( '&p_job_id', '0', '1', job_id ) = decode(
'&p_job_id', '0', '1', '&p_job_id' )
connect by prior job_id = merge_to
)
order by 2;

which gives this result:

DEPTH CHANGE_HISTORY PARTICIPANT EVENT
--------- ----------------------- ------------ -----
1 /AAAA AAAA
1 /BBBB BBBB
1 /CCCC CCCC
2 /CCCC/AAAA AAAA SPLIT
1 /DDDD DDDD
2 /DDDD/BBBB BBBB SPLIT
1 /EEEE EEEE
2 /EEEE/CCCC CCCC MERGE
2 /EEEE/DDDD DDDD MERGE
1 /FFFF FFFF
2 /FFFF/CCCC CCCC SPLIT
3 /FFFF/CCCC/AAAA AAAA SPLIT


So here is the problem: a query must be created which combines above
"history query" with table JOB_REPORT to produce following result:

job_id job_name YR1 ....
==========================================
AAAA name1 6
BBBB name2 5
CCCC name3 9 (*
DDDD name4 12 (**
EEEE name5 31 (***
FFFF name6 12 (****

if the query is for all JOB_IDs

or just

job_id job_name YR1 ....
==========================================
EEEE name5 31

if the query is for specific JOB_ID (EEEE in this example). Query
must
show data for every YR (YR1, YR2, YR3, YR4, YR5)

remark (* : 9 is sum of 3 and 6 where 3 is the number of people who
worked on job CCCC, and where 6 is the number of people who worked on
job AAAA from which job CCCC was split. In this example I only showed
column YR1 but the same logic is applicable for columns YR2, YR3,YR4
and YR5 as well

remark (**: 12 is the sum of 7 and 5 where 7 is the number of people
who worked on job DDDD, and where 5 is the number of people who
worked
on job BBBB from which job DDDD was split.

remark (***: 31 is the sum 10+9+12 where 10 is the number of people
who
worked on job EEEE and 9 is explained in remark (* and 12 is
explained
in remark (**. I.e. CCCC and DDDD were merged into EEEE

remark (****: 12 is the sum 3+9 where 3 is the number of people who
worked on job FFFF and 9 is explained in remark (*. I.e. FFFF was
split
from job CCCC.

Basically, in this example data, job CCCC was created by splitting
from
job AAAA, and job DDDD by splitting from job BBBB. CCCC and DDDD were
then merged into EEEE, while at the same time job FFFF was split from
CCCC. Query must process each row in JOB_REPORT table and aggregate
the
number of employees when these numbers represent the "history" of
particular JOB_ID to correct existing value in JOB_REPORT table (to
"prorate" the number of employees in JOB_REPORT table based on the
history of given JOB_ID represented inside JOB table).

In line with the other replies, I can afford only 5 minutes onto this
problem. The key to the solution might be the idea that there aren't
really 2 hierarchies in your case. (And generally, I've never seen 2
hierarchies at once). What you have is directed acyclic graph, where
each node is a job, and each directed job connects 2 adjacent nodes.
This generalization covers merges and splits nicely. Therefore, I
suggest reorganizing your schema (by means of physical redesign, or
merely by creating views) into 2 tables: job_nodes and job_links.
Back to top
Frank van Bortel
*nix forums Guru


Joined: 19 Apr 2005
Posts: 804

PostPosted: Wed Feb 23, 2005 7:46 pm    Post subject: Re: aggregation over two hierarchies Reply with quote

Rauf Sarwar wrote:
Quote:
snip

(but bare in mind that
these days employers put emphasis on character more than on technical
skills)


That makes lot's of sense....

"Your resume says that you have excellent character and personality but
have absolutely no technical skills whatsoever?... Good, we recently
fired our highly skilled DBA because his attitude sucked so that we can
hire someone like you to manage our mission critical database."

Regards
/Rauf

lol - BDBAFH?

(before anyone wonders: familiar with the Bastard Operator From Hell,
or BOFH? Now work out what this acronym stands for Smile )
--
Regards,
Frank van Bortel
Back to top
Rauf Sarwar
*nix forums Guru


Joined: 03 May 2005
Posts: 353

PostPosted: Wed Feb 23, 2005 5:45 pm    Post subject: Re: aggregation over two hierarchies Reply with quote

<snip>
Quote:
(but bare in mind that
these days employers put emphasis on character more than on technical
skills)

That makes lot's of sense....

"Your resume says that you have excellent character and personality but
have absolutely no technical skills whatsoever?... Good, we recently
fired our highly skilled DBA because his attitude sucked so that we can
hire someone like you to manage our mission critical database."

Regards
/Rauf
Back to top
DA Morgan
*nix forums Guru


Joined: 06 Mar 2005
Posts: 1042

PostPosted: Wed Feb 23, 2005 4:23 pm    Post subject: Re: aggregation over two hierarchies Reply with quote

navrsale wrote:
Quote:
DA Morgan <damorgan@x.washington.edu> wrote in message news:<1109136771.990778@yasure>...

navrsale wrote:

In my application there are two given tables:

I'm with Sybrand. Unless you are planning on paying someone to
do your job for you ... you need to do the work and we will
help you if you get stuck. This does not look like that.


When I used to visit Dejanews groups in the past I used to offer my
help
w/out asking any monies. But then, it was in the 90s...maybe things
changed in the meantime...anyways, you can keep my hierarchical
subquery as a bonus. Regarding employment, send me the link to your
resume. I know many players in employment game and they might help you
(but bare in mind that
these days employers put emphasis on character more than on technical
skills)

I'm quite a character so that shouldn't be a problem. But no thanks. I
already have more than enough work.
--
Daniel A. Morgan
University of Washington
damorgan@x.washington.edu
(replace 'x' with 'u' to respond)
Back to top
navrsale
*nix forums beginner


Joined: 22 Feb 2005
Posts: 5

PostPosted: Wed Feb 23, 2005 2:52 pm    Post subject: Re: aggregation over two hierarchies Reply with quote

DA Morgan <damorgan@x.washington.edu> wrote in message news:<1109136771.990778@yasure>...
Quote:
navrsale wrote:
In my application there are two given tables:

I'm with Sybrand. Unless you are planning on paying someone to
do your job for you ... you need to do the work and we will
help you if you get stuck. This does not look like that.

When I used to visit Dejanews groups in the past I used to offer my
help
w/out asking any monies. But then, it was in the 90s...maybe things
changed in the meantime...anyways, you can keep my hierarchical
subquery as a bonus. Regarding employment, send me the link to your
resume. I know many players in employment game and they might help you
(but bare in mind that
these days employers put emphasis on character more than on technical
skills)
Back to top
DA Morgan
*nix forums Guru


Joined: 06 Mar 2005
Posts: 1042

PostPosted: Wed Feb 23, 2005 4:35 am    Post subject: Re: aggregation over two hierarchies Reply with quote

navrsale wrote:
Quote:
In my application there are two given tables:

I'm with Sybrand. Unless you are planning on paying someone to
do your job for you ... you need to do the work and we will
help you if you get stuck. This does not look like that.
--
Daniel A. Morgan
University of Washington
damorgan@x.washington.edu
(replace 'x' with 'u' to respond)
Back to top
Sybrand Bakker
*nix forums Guru


Joined: 03 Apr 2005
Posts: 1766

PostPosted: Tue Feb 22, 2005 8:24 pm    Post subject: Re: aggregation over two hierarchies Reply with quote

On 22 Feb 2005 13:00:01 -0800, "navrsale" <navrsalemile@yahoo.ca>
wrote:

Quote:
Query must process each row in JOB_REPORT table and aggregate the
number of employees when these numbers represent the "history" of
particular JOB_ID to correct existing value in JOB_REPORT table (to
"prorate" the number of employees in JOB_REPORT table based on the
history of given JOB_ID represented inside JOB table).

thanks,
mile

How much do you pay? Do you realize Usenet is a volunteer business and
you can't expect people to sit down for half an hour or more to write
your query?


--
Sybrand Bakker, Senior Oracle DBA
Back to top
navrsale
*nix forums beginner


Joined: 22 Feb 2005
Posts: 5

PostPosted: Tue Feb 22, 2005 8:00 pm    Post subject: aggregation over two hierarchies Reply with quote

In my application there are two given tables:

JOB table:

job_id open closed splitfrom mergedto
=================================================
AAAA 1/1/90 1/1/00
BBBB 1/1/80 1/1/00
CCCC 1/1/00 1/1/01 AAAA EEEE
DDDD 1/1/00 1/1/01 BBBB EEEE
EEEE 1/1/01
FFFF 1/1/01 CCCC

create table job (
job_id char(4) NOT NULL,
open_dte date not null,
close_dte date,
split_from char(4),
merge_to char(4),
PRIMARY KEY (job_id),
FOREIGN KEY (split_from) REFERENCES job(job_id),
FOREIGN KEY (merge_to) REFERENCES job(job_id)
)
/
INSERT INTO job VALUES( 'AAAA', to_date('01/01/90', 'MM/DD/RR'),
to_date('01/01/00', 'MM/DD/YY'), null, null )
/
INSERT INTO job VALUES( 'BBBB', to_date('01/01/80', 'MM/DD/RR'),
to_date('01/01/00', 'MM/DD/YY'), null, null )
/
INSERT INTO job VALUES( 'CCCC', to_date('01/01/00', 'MM/DD/RR'),
to_date('01/01/01', 'MM/DD/YY'), 'AAAA', 'EEEE' )
/
INSERT INTO job VALUES( 'DDDD', to_date('01/01/00', 'MM/DD/RR'),
to_date('01/01/01', 'MM/DD/YY'), 'BBBB', 'EEEE' )
/
INSERT INTO job VALUES( 'EEEE', to_date('01/01/01', 'MM/DD/RR'), null,
null,
null )
/
INSERT INTO job VALUES( 'FFFF', to_date('01/01/01', 'MM/DD/RR'), null,
'CCCC',
null )
/
commit
/


and JOB_REPORT table:

job_id job_name YR1 YR2 YR3 YR4 YR5
======================================================
AAAA name1 6 5 4 3 2
BBBB name2 5 4 3 2 1
CCCC name3 3 2 1 2 3
DDDD name4 7 6 5 4 3
EEEE name5 10 9 8 7 6
FFFF name6 3 4 5 6 7


create table job_report (
job_id char(4) NOT NULL,
job_name char(5) not null,
yr1 integer,
yr2 integer,
yr3 integer,
yr4 integer,
yr5 integer
PRIMARY KEY (job_id)
)
/

INSERT INTO job_report VALUES( 'AAAA', 'name1', 6,5,4,3,2 )
/
INSERT INTO job_report VALUES( 'BBBB', 'name2', 5,4,3,2,1 )
/
INSERT INTO job_report VALUES( 'CCCC', 'name3', 3,2,1,2,3 )
/
INSERT INTO job_report VALUES( 'DDDD', 'name4', 7,6,5,4,3 )
/
INSERT INTO job_report VALUES( 'EEEE', 'name5', 10,9,8,7,6 )
/
INSERT INTO job_report VALUES( 'FFFF', 'name6', 3,4,5,6,7 )
/
commit
/


JOB table describes different jobs and their history (some jobs are
split into another job and some jobs are merged into a new job). For
example this hierarchical query describes history inside JOB table:

column change_history format a50
column participant format a12
set pagesize 66
accept p_job_id prompt 'enter job id, 0 for all: '

select distinct DEPTH,
CHANGE_HISTORY,
substr( change_history, instr( change_history, '/', -1,
1 ) + 1 ) PARTICIPANT,
decode( DEPTH - 1, 0, null, EVENT ) EVENT
from (
select level DEPTH,
sys_connect_by_path( job_id, '/' ) CHANGE_HISTORY,
'SPLIT' EVENT
from job
start with DECODE( '&p_job_id', '0', '1', job_id ) = decode(
'&p_job_id', '0', '1', '&p_job_id')
connect by prior split_from = job_id
union all
select level DEPTH,
sys_connect_by_path( job_id, '/' ) CHANGE_HISTORY,
'MERGE' EVENT
from job
start with DECODE( '&p_job_id', '0', '1', job_id ) = decode(
'&p_job_id', '0', '1', '&p_job_id' )
connect by prior job_id = merge_to
)
order by 2;

which gives this result:

DEPTH CHANGE_HISTORY PARTICIPANT EVENT
--------- ----------------------- ------------ -----
1 /AAAA AAAA
1 /BBBB BBBB
1 /CCCC CCCC
2 /CCCC/AAAA AAAA SPLIT
1 /DDDD DDDD
2 /DDDD/BBBB BBBB SPLIT
1 /EEEE EEEE
2 /EEEE/CCCC CCCC MERGE
2 /EEEE/DDDD DDDD MERGE
1 /FFFF FFFF
2 /FFFF/CCCC CCCC SPLIT
3 /FFFF/CCCC/AAAA AAAA SPLIT


So here is the problem: a query must be created which combines above
"history query" with table JOB_REPORT to produce following result:

job_id job_name YR1 ....
==========================================
AAAA name1 6
BBBB name2 5
CCCC name3 9 (*
DDDD name4 12 (**
EEEE name5 31 (***
FFFF name6 12 (****

if the query is for all JOB_IDs

or just

job_id job_name YR1 ....
==========================================
EEEE name5 31

if the query is for specific JOB_ID (EEEE in this example). Query must
show data for every YR (YR1, YR2, YR3, YR4, YR5)

remark (* : 9 is sum of 3 and 6 where 3 is the number of people who
worked on job CCCC, and where 6 is the number of people who worked on
job AAAA from which job CCCC was split. In this example I only showed
column YR1 but the same logic is applicable for columns YR2, YR3,YR4
and YR5 as well

remark (**: 12 is the sum of 7 and 5 where 7 is the number of people
who worked on job DDDD, and where 5 is the number of people who worked
on job BBBB from which job DDDD was split.

remark (***: 31 is the sum 10+9+12 where 10 is the number of people who
worked on job EEEE and 9 is explained in remark (* and 12 is explained
in remark (**. I.e. CCCC and DDDD were merged into EEEE

remark (****: 12 is the sum 3+9 where 3 is the number of people who
worked on job FFFF and 9 is explained in remark (*. I.e. FFFF was split
from job CCCC.

Basically, in this example data, job CCCC was created by splitting from
job AAAA, and job DDDD by splitting from job BBBB. CCCC and DDDD were
then merged into EEEE, while at the same time job FFFF was split from
CCCC. Query must process each row in JOB_REPORT table and aggregate the
number of employees when these numbers represent the "history" of
particular JOB_ID to correct existing value in JOB_REPORT table (to
"prorate" the number of employees in JOB_REPORT table based on the
history of given JOB_ID represented inside JOB table).

thanks,
mile
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [11 Posts] View previous topic :: View next topic
The time now is Thu Jan 08, 2009 5:44 am | All times are GMT
navigation Forum index » Databases » Oracle
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Bug#354304: ITP: libalgorithm-c3-perl -- A module for mer... Krzysztof Krzyzaniak (elo devel 0 Fri Feb 24, 2006 11:50 pm
No new posts Link aggregation with Fibre Channels? shiva.hs@gmail.com AIX 1 Thu Jan 26, 2006 5:47 pm
No new posts Rollup Aggregation Tuning Pawel Server 2 Mon Oct 17, 2005 2:37 am
No new posts Aggregation query in Oracle bhaskarjain@gmail.com Oracle 3 Tue Jul 19, 2005 1:26 pm
No new posts Inheritance or aggregation in the first variable? Peter Jakobi C++ 0 Tue Jun 28, 2005 10:37 am

Debt Consolidation | Car Loan | Debt Consolidation | Debt Consolidation | Remortgages
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1637s ][ Queries: 20 (0.0315s) ][ GZIP on - Debug on ]