Hierarchical queries with Rollup sum (CONNECTED BY GROUP BY ROLLUP)
Hi allImagine the following scenario: I have an ACCOUNT table that contains the accounts and their hierarchy (currently 5 levels) and a BALANCE table that holds the record for the balance of the accounts. Only CHILD accounts (level 5) have records in the table for BALANCE. 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 each node, I calculate the sum of all its children (in TERMINAL nodes that are child accounts, this amount is the value of the balances itself). End result would be:TOT -> 10000
ANA1 -> 3000
4801001 -> 2000
4801001 -> 1000
ANA2 -> 7000
4802001 -> 3000
4802002 -> 4000
I tried many ways and found a solution that works for a fixed amount of levels, basically he built the hierarchy and calculates the SYS_CONNECT_BY_PATH, then divides it as a regular expression and using GROUP BY ROLLUP to calculate the highest levels. Then I assemble again, now with the calculated values. Here's 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_acct
All is 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, regardless of the number of levels, amounts of parent are all wound like that?Thank you very much in advance! Best regards!
Thiago
Published by: Thiago Sep 6, 2011 11:31
Published by: Thiago Sep 6, 2011 13:01
select account_code,
(
select sum(balance_amount)
from accounts a2,
balances b
where b.account_code(+) = a2.account_code
start with a2.account_code = a1.account_code
connect by a2.parent_account = prior a2.account_code
) balance_amount
from accounts a1
/
ACCOUNT_CODE BALANCE_AMOUNT
--------------- --------------
TOT 10000
ANA1 3000
4801001 3000
4801002
ANA2 7000
4802001 7000
4802002
7 rows selected.
SQL>
SY.
Tags: Database
Similar Questions
-
Hierarchical queries - problem with condition "begins by".
Hi people
I play with the connection by and start with clause and am faced with a particular problem which I can not solve...
My data set is:
Create table dates_q (start_date date, end_date date) /
REM INSERTING into dates_q Insert into dates_q ("START_DATE","END_DATE") values (to_date('01-JAN-14','DD-MON-RR'),to_date('10-JAN-14','DD-MON-RR')); Insert into dates_q ("START_DATE","END_DATE") values (to_date('11-JAN-14','DD-MON-RR'),to_date('20-JAN-14','DD-MON-RR')); Insert into dates_q ("START_DATE","END_DATE") values (to_date('10-MAR-14','DD-MON-RR'),to_date('20-MAR-14','DD-MON-RR')); Insert into dates_q ("START_DATE","END_DATE") values (to_date('21-MAR-14','DD-MON-RR'),to_date('31-MAR-14','DD-MON-RR')); Insert into dates_q ("START_DATE","END_DATE") values (to_date('01-APR-14','DD-MON-RR'),to_date('10-APR-14','DD-MON-RR'));
Now I basically just want to get your hands on hierarchical queries and working with the syntax of various...
What I now want is, start with the date of April 1 as my start date and work backward to build my 'tree '. The condition of my tree is between two rows; my start and end dates differ from 1 day. If they do not; I don't want these records in my tree.
And using sys_connect_by_path, I want to get all the way from the root.
Thus, for example,.
SELECT a.*, sys_connect_by_path(start_date, '|'), LEVEL lvl FROM dates_q a CONNECT BY PRIOR end_date = (start_date - 1)
I get the following output
START_DATE END_DATE SYS_CONNECT_BY_PATH(START_DATE,'|') LVL 01.01.2014 10.01.2014 | 1 JANUARY 14 1 11.01.2014 20.01.2014 | 1 JANUARY 14 | JANUARY 11, 14 2 11.01.2014 20.01.2014 | JANUARY 11, 14 1 10.03.2014 20.03.2014 | MARCH 10, 14 1 21.03.2014 31.03.2014 | MARCH 10, 14. MARCH 21, 14 2 01.04.2014 10.04.2014 |10-MAR-14|21-MAR-14|01-APR-14 3 21.03.2014 31.03.2014 | MARCH 21, 14 1 01.04.2014 10.04.2014 | MARCH 21, 14. 1 APRIL 14 2 01.04.2014 10.04.2014 | 1 APRIL 14 1 But for the moment I did not have any starting point... Now comes the FUN part...
When I give the State of departure; I get a single row :-(
Example of
SELECT a.*, sys_connect_by_path(start_date, '|'), LEVEL lvl FROM dates_q a CONNECT BY PRIOR end_date = (start_date - 1) START WITH start_date = To_Date('01-apr-2014','dd-mon-yyyy');
The result is
START_DATE END_DATE SYS_CONNECT_BY_PATH(START_DATE,'|') LVL 01.04.2014 10.04.2014 | 1 APRIL 14 1 Just a line...!
I'm unable to understand this and work more and need help.
The formation of the tree works only in a 'sense' and I'm going the other way around? Don't know what it means but just something that comes to mind. :/
Thank you
K
P.S. - database is 10g R2.
Hello
Thanks for the display of the data of the sample; It is very useful.
What do you expect the result will be and why?
LEVEL = 1 contains all rows that meet the condition to START WITH. The rows that meet the condition
start_date = To_Date('01-apr-2014','dd-mon-yyyy')
in this case? Only the line you actually obtained.
LEVEL = N (where N > 1) contains all rows that meet the conditions regarding some FRONT CONNECT BY rank level = N - 1. Since the only line level = 1 to end_date = To_Date('10-apr-2014','dd-mon-yyyy'), lines satisfy the condition
End_date PRIOR = (start_date - 1).
? None. End_date PREREQUISITE is April 10, while rows with start_date April 11 would fulfill this condition, there is no line on LEVEL = 2 and the query stops there.
You would have expected this from your previous results. The line with the start_date April 1 had no children in the previous application, so there no children in any application that has the same State of CONNECT BY.
Maybe you meant the CONNECT BY condtion to be
End_date = BEFORE (start_date - 1).
-
How to order a tree balanced with SQL hierarchical queries
by searching the forum I found this
is there a way I can order this tree in alphabetical order so that the result looks like
LEVEL INDENTED_ENAME
---------- --------------------
1 KING BED
2 BLAKE
3 ALLEN
3 JAMES
MARTIN 3
3 TURNER
WARD 3
2 CLARK
3 MILLER
2 JONES
3 FORD
4 SMITH
3 SCOTT
4 ADAMS
-the original query-
SELECT THE LEVEL
, LPAD (' ', 3 * LEVEL) | Ename AS indented_ename
FROM scott.emp
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr
;
LEVEL INDENTED_ENAME
---------- --------------------
1 KING BED
2 JONES
3 SCOTT
4 ADAMS
3 FORD
4 SMITH
2 BLAKE
3 ALLEN
WARD 3
MARTIN 3
3 TURNER
3 JAMES
2 CLARK
3 MILLERHello
Bodin wrote:
Hi Frank, I can order it selectively depending on the level, which means that only siblings stopped at third level, but rest of the brothers and sisters remain Nations United orderedIt's actually quite difficult. You can "ORDER of brothers and SŒURS BY CASE... ', like this:
SELECT LEVEL , LPAD (' ', 3 * LEVEL) || ename AS indented_ename FROM scott.emp START WITH mgr IS NULL CONNECT BY mgr = PRIOR empno ORDER SIBLINGS BY CASE WHEN job = 'MANAGER' THEN ename ELSE NULL END ;
In this case to get desired results in table scott.emp, as the lines are LEVEL = 2 if and only if use = "MANAGER".
But if you reference LEVEL in the CASE expression (for example, if you replace ' job = 'MANAGER' ' with "2 LEVEL =" above "), then you will get the error" ORA-00976: LEVEL, PRIOR or ROWNUM not allowed here. "
The best way I can think to do exactly what you asked is to do 2 CONNECT BY queries; one just to get the LEVEL and the other for the brothers and SŒURS ORDER BY:
{code}
WITH got_lvl AS
(
SELECT LEVEL AS lvl
Bishop
empno
ename
FROM scott.emp
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr
)
SELECT lvl
, LPAD (' ', 3 * LEVEL) | Ename AS indented_ename
OF got_lvl
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr
BROTHERS AND SŒURS OF ORDER OF CASES
ONCE lvl = 2 THEN ename
ANOTHER NULL
END
;
{code}
Why you can't simply "Brothers and SŒURS of ORDER BY ename" at all levels? If all you care is the order of the items of LEVEL = 2, then this is probably the most effective and simplest way. It really hurt anything if nodes on levels 3, 4, 5,... are in order, too?Here's something you can do if you want to order by different unique things to different levels:
{code}
WITH got_sort_key AS
(
SELECT LEVEL AS lvl
, LPAD (' ', 3 * LEVEL) | Ename AS indented_ename
empno
SYS_CONNECT_BY_PATH (LPAD (CASE
WHEN LEVEL = 2
THEN ename
Of OTHER TO_CHAR (empno)
END
10
)
, ','
) AS sort_key
FROM scott.emp
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr
)
SELECT lvl
indented_ename
empno
OF got_sort_key
ORDER BY sort_key
;
{code}
However, all possible values of the CASE expression must uniquely identify the node; otherwise, the output won't necessarily hierarchical order. You can assign arbitrary unique IDS to the lines (using the ROW_NUMBER analytic function, for example), but that requires another subquery and is also complex and perhaps as ineffective as the solution above with 2 garages CONNECT. -
Building the tree balanced with SQL hierarchical queries
Hi all
I have the following hierarchical data with different levels of subtree:
A0
-A001
-A00101
A1
-A101
A2
-A201
-A20101
-A201010001
A0 subtree has 3 levels, A1 subtree has 2 levels and subtree of the A3 is level 4. I want to generate a tree balanced on the data with all levels of the subtree equal to the maximum number of levels available in the whole tree which, in this particular case, is 4.
I don't know that it is possible with SQL. Script to generate the above mentioned are as below:
CREATE TABLE codes_tree
(node_id VARCHAR2 (10))
parent_node_id VARCHAR2 (10)
);
INSERT INTO codes_tree VALUES ('A0', NULL);
INSERT INTO codes_tree VALUES ('A001', 'A0');
INSERT INTO codes_tree VALUES ('A00101', 'A001');
---
INSERT INTO codes_tree VALUES ('A1', NULL);
INSERT INTO codes_tree VALUES ('A101', 'A1');
---
INSERT INTO codes_tree VALUES ('A2', NULL);
INSERT INTO codes_tree VALUES ('A201', 'A2');
INSERT INTO codes_tree VALUES ('A20101', 'A201');
INSERT INTO codes_tree VALUES ('A201010001', 'A20101');
Any help will be much appreciated.
Thank you... Best regards
Published by: naive2Oracle on May 12, 2011 19:40
Published by: naive2Oracle on May 12, 2011 19:41Hello
Of course, you can do it in SQL.
One way is to take the normal output of hierarchical and manipulate the result set so that the leaves are repeated as often as necessary to make all branches of the same length. I have Oracle 10.2 available right now, so here's a solution that will work in Oracle 10 (and more):WITH original_hierarchy AS ( SELECT node_id , LEVEL AS lvl , CONNECT_BY_ISLEAF AS isleaf , ROWNUM AS rnum FROM codes_tree START WITH parent_node_id IS NULL CONNECT BY parent_node_id = PRIOR node_id ) , got_max_lvl AS ( SELECT o.* , MAX (lvl) OVER () AS max_lvl FROM original_hierarchy o ) SELECT LPAD ( ' ' , 3 * ( ( d.lvl + NVL (c.rnum, 1) - 1 ) - 1 ) ) || CASE WHEN c.rnum > 1 THEN '*' || d.node_id || '*' ELSE d.node_id END AS display_id FROM got_max_lvl d LEFT OUTER JOIN got_max_lvl c ON d.isleaf = 1 AND c.rnum <= 1 + d.max_lvl - d.lvl ORDER BY d.rnum , c.rnum ;
With the help of Oracle 11.2, it would be preferable to generate original_hierarchy as above, but to manipulate using a WITH recursive clause.
Analytical functions often interfere with CONNECT BY, so I used a separate subquery to get max_lvl, do CONNECT BY in a sub-querry and analytic function in a separate subquery. I don't know what is needed on all versions.Output of your sample data:
DISPLAY_ID ------------------------------- A0 A001 A00101 *A00101* A1 A101 *A101* *A101* A2 A201 A20101 A201010001
Below is a generic version of the same query, which I used to test this on scott.emp:
DEFINE table_name = scott.emp DEFINE id_col = empno DEFINE parent_id_col = mgr DEFINE display_col = ename WITH original_hierarchy AS ( SELECT &display_col AS display_txt , LEVEL AS lvl , CONNECT_BY_ISLEAF AS isleaf , ROWNUM AS rnum FROM &table_name START WITH &parent_id_col IS NULL CONNECT BY &parent_id_col = PRIOR &id_col ) , got_max_lvl AS ( SELECT o.* , MAX (lvl) OVER () AS max_lvl FROM original_hierarchy o ) SELECT LPAD ( ' ' , 3 * ( ( d.lvl + NVL (c.rnum, 1) - 1 ) - 1 ) ) || CASE WHEN c.rnum > 1 THEN '*' || d.display_txt || '*' ELSE d.display_txt END AS display_id FROM got_max_lvl d LEFT OUTER JOIN got_max_lvl c ON d.isleaf = 1 AND c.rnum <= 1 + d.max_lvl - d.lvl ORDER BY d.rnum , c.rnum ;
Output:
DISPLAY_ID ----------------------------- KING JONES SCOTT ADAMS FORD SMITH BLAKE ALLEN *ALLEN* WARD *WARD* MARTIN *MARTIN* TURNER *TURNER* JAMES *JAMES* CLARK MILLER *MILLER*
Published by: Frank Kulash, May 13, 2011 06:38
Adding the generic version -
Help to combine the two queries with dependencies
Combining two queries with dependencies:First ask: Returns USER_ID that has a type of access to a table with the OWNER (I also shoot the TLA of the USER_ID standard: ' of TLA #)Select distinct SUBSTR (DBA_USERS. Username, 2, 3) AS TLA
DBA_USERS. Username USED_ID
DBA_TAB_PRIVS. Owner OWNER
from DBA_USERS
DBA_ROLE_PRIVS
DBA_TAB_PRIVS
where DBA_USERS. Username = DBA_ROLE_PRIVS. Dealer
and DBA_USERS.default_tablespace not in ('SYSTEM', "SYSAUX")
and DBA_USERS. Username like would be %'
and DBA_ROLE_PRIVS. Granted_role = DBA_TAB_PRIVS. Dealer
and SUBSTR (DBA_USERS. Username, 2, 3) <>DBA_TAB_PRIVS. Owner
;
Example of results (red font used in the last citation):TLA USED_ID OWNER
--- ------------------------------ ------------------------------
DGTX999 GTX GRR
DGTX999 GTX ABG
DGTX999 GTX HTC
FWS DFWS999 GRR
OCN DOCN999 GRR
RHR DRHR999 DAS
ETM DETM999 DAS
FWS DFWS995 DAS
CD DCDS999 DAS
ABE DABE999 DAS
Second request: matches the USER name (same standards as above) and MACHINES (filter PC connections) and the number of connections made ofSelect DBA_USERS. Username
DBA_HIST_ACTIVE_SESS_HISTORY. Machine
COUNT (*) AS CONN_COUNT
of DBA_HIST_ACTIVE_SESS_HISTORY
DBA_USERS
where DBA_HIST_ACTIVE_SESS_HISTORY. User_id = DBA_USERS. User_id
and DBA_USERS. Username like would be %'
and DBA_HIST_ACTIVE_SESS_HISTORY. Machine not as "WINAD%\%-%".
Group of DBA_USERS. Username
DBA_HIST_ACTIVE_SESS_HISTORY. Machine
order by 1
3 desc;
Example of results (red font used in the last citation):MACHINE CONN_COUNT USERNAME
--------------- ------------------------- ----------
DFWS999 home13e0 7557
DGAM800 vu1246 37
DGAM999 vu1246 2558
DABE999 back18cb 4639
DINL888 home162f 84
DFWS999 WINDAD\OMHQ58BF 178
DCDS999 back18cb 788
DGTX999 home18c8 7
DGTX999 home13d3 62
DOCN999 vu1261 44
DFWS999 back1976 3
DCDS999 home18c8 173
DGTX999 home19c9 13
What I'd like to have (just made two first USER_IDs from the first query) (COL duplicated for each separate owner 1-4). So 3 machines and 3 owners would result in 9 lines (I could reorganize the collar and use a 'pause'):
TLA USED_ID MACHINE CONN_COUNT OWNER
--- ------------------------------ ------------------------- ---------- ------------------------------
GTX DGTX999 home13d3 62 GRR
Home18c8 GTX DGTX999 7 GRR
DGTX999 GTX home19c9 GRR 6
GTX DGTX999 home13d3 62 ABG
DGTX999 GTX home18c8 ABG 7
DGTX999 GTX home19c9 6 GBS
GTX DGTX999 home13d3 62 CTH
DGTX999 GTX HTC 7 home18c8
DGTX999 GTX home19c9 6 HTC
FWS DFWS999 home13e0 7557 GRR
FWS DFWS999 WINDAD\OMHQ58BF 178 GRR
FWS DFWS999 back1976 GRR 3
So it would be logically read: TLA as USER_ID of MACHINE, CONN_COUNT consultation times: OWNER information.Thought as only those with the necessary values. Just try this and let me know in the case of all conflicts in the output.
WITH T1 AS)
Select distinct SUBSTR (DBA_USERS. Username, 2, 3) AS TLA
DBA_USERS. Username USED_ID
DBA_TAB_PRIVS. Owner OWNER
ROWNUM RN
from DBA_USERS
DBA_ROLE_PRIVS
DBA_TAB_PRIVS
where DBA_USERS. Username = DBA_ROLE_PRIVS. Dealer
and DBA_USERS.default_tablespace not in ('SYSTEM', "SYSAUX")
and DBA_USERS. Username like would be %'
and DBA_ROLE_PRIVS. Granted_role = DBA_TAB_PRIVS. Dealer
and SUBSTR (DBA_USERS. Username, 2, 3) <> DBA_TAB_PRIVS. Owner),
() AS T2
Select DBA_USERS. Username
DBA_HIST_ACTIVE_SESS_HISTORY. Machine
COUNT (*) AS CONN_COUNT
of DBA_HIST_ACTIVE_SESS_HISTORY
DBA_USERS
where DBA_HIST_ACTIVE_SESS_HISTORY. User_id = DBA_USERS. User_id
and DBA_USERS. Username like would be %'
and DBA_HIST_ACTIVE_SESS_HISTORY. Machine not as "WINAD%\%-%".
Group of DBA_USERS. Username
DBA_HIST_ACTIVE_SESS_HISTORY. Machine
order by 1
3 desc)
SELECT T1. TLA
, T1. USED_ID
, T2. MACHINE
, T2. CONN_COUNT
, T1. OWNER
FROM T1
T2
WHERE T1. USED_ID = T2. USER NAME;
-
Reg: Prior in hierarchical queries:
Hi Experts,
A little doubt - I've been working on the requests, but it still haunts me... Hierarchical queries
One thing I am more confused about is the prior clause.
I'll try to explain my concern below.
Here, the tree yesterday gets reversed by changing the clause PRIOR to 'Manager Id' to 'Employee Id' and vice versa.
Exactly how to understand where to put FORWARD... which column?
I can get my results just double check... but I just want to know the concept. So stupid right... but do not want to take this doubt more far. ;)
Please help guys.ranit@XE11GR2>> select * from 2 emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 17-DEC-08 800 20 7566 NORGAARD MANAGER 7839 01-APR-07 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-07 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-09 2850 30 7782 FOOTE MANAGER 7839 09-JUN-08 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 ELLISON PRESIDENT 17-NOV-06 5000 10 7844 TURNER SALESMAN 7698 08-SEP-08 1500 0 30 7876 ADAMS CLERK 7788 18-JUN-07 1100 20 7900 JAMES CLERK 7698 03-DEC-07 950 30 7902 LOFSTROM ANALYST 7566 04-DEC-08 3000 20 7934 MILLER CLERK 7782 23-JAN-08 1300 10 12 rows selected. Elapsed: 00:00:00.04 ranit@XE11GR2>> select * from 2 emp 3 connect by empno = prior mgr 4 start with empno = 7698; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7698 BLAKE MANAGER 7839 01-MAY-09 2850 30 7839 ELLISON PRESIDENT 17-NOV-06 5000 10 Elapsed: 00:00:00.01 ranit@XE11GR2>> select * from 2 emp 3 connect by prior empno = mgr 4 start with empno = 7698; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7698 BLAKE MANAGER 7839 01-MAY-09 2850 30 7654 MARTIN SALESMAN 7698 28-SEP-07 1250 1400 30 7844 TURNER SALESMAN 7698 08-SEP-08 1500 0 30 7900 JAMES CLERK 7698 03-DEC-07 950 30 Elapsed: 00:00:00.07
Thank youOracle Database 11g Express Edition Release 11.2.0.2.0 - Production PL/SQL Release 11.2.0.2.0 - Production "CORE 11.2.0.2.0 Production" TNS for 32-bit Windows: Version 11.2.0.2.0 - Production NLSRTL Version 11.2.0.2.0 - Production
Vanessa B.I know the feeling!
START WITH gives you the first line of the result set.
CONNECT BY gets you the next row. At this point, you already have is the "before" line and the line that you get is the current line.
If the current line is supposed to be the Manager of the previous line, this means that Bishop PREREQUISITE = empno.
If the previous line is supposed to be the Manager of the current line, which means PRIOR empno = mgr.
Think AHEAD as referring to the line that you just received and "without any preconditions" as the line you want.
Another way of thinking that is to ask "what new information I receive?
If you say "Bishop PREREQUISITE = empno", you already know the Director of the previous row. New information will be the Director of the current line. As you continue, you will learn step by step who are managers.
If you say "PRIOR empno = mgr", you already know that the employee of the previous row. New information will be the new empno. So step by step, you will find the empnos.
You always ' move to before current. "Of mgr to empno" descends from the string, "empno to Bishop" goes to the top of the chain.
Published by: stew Ashton on February 16, 2013 11:57
-
Gites join vs hierarchical queries
Hello
Please tel me who must use a 1?
I have to get job list of the simple Manger, should I use self-join or hierarchical queries (CONNECT BY and earlier versions)?
Yours sincerelyHello
944768 wrote:
HelloPlease tel me who must use a 1?
I have to get job list of the simple Manger, should I use self-join or hierarchical queries (CONNECT BY and earlier versions)?It depends on your data and your needs.
Whenever you have a question, please post a small example of data (CREATE TABLE and INSERT statements) for all the tables involved, so people who want to help you can recreate the problem and test their ideas. Also post the results you want from this data, as well as an explanation of how you get these results from these data.
Explain, using specific examples, how you get these results from these data.
If you show what the problem using commonly available tables (suc as scott.emp, who has a hierarchy of level 4), then you do not have ot post sample data, just the results and explanations.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum {message identifier: = 9360002}If your hierarchy consists only of 2 levels, then a self-join will be probably more effective, simpler code and easier to maintain.
If you don't know how many levels in the hierarchy, then self-join is not an option. Use CONNECT BY or, if you have Oracle 11.2, a WITH recursive clause.
If you have a fixed number of levels (or an upper limit) greater than 2, then CONNECT BY (or a WITH recursive clause) will probably be the best.
-
Hello
I wrote an application that allows the user to filter the data displayed based on several parameters, for example one of the parameters (b) is hierarchical, each element (with the exception of the root element) has a father and several sons, the hierarchy is managed by a specific table (LINKS), I need when the user selects b the result will include also all his descendants.
This is the query:
SELECT *.
R
JOIN IN-HOUSE RT ON R.A = RT. A
WHERE (R.B IN (select sub_id
the beginning of LINKS with father_id =: id
Connect prior sub_id = father_id)
or R.B =: id or: id = 0)
AND (R.A =: GOLD: a = 0)
ORDER OF R.B, R.A
The problem is that the query is so slow, it allows the application of stuck, when I've omitted the line: ' or R.B =: id or: id = 0 ' he not stuck but this line is necessary because the user may also fill the filter field or try to filter b himself and not his descendants.
Is it possible to improve the performance of this query or write it better?You can move the logic on r.b in the subquery.
There is a small chance that it could accelerate the entire query.
not testedSELECT * FROM R JOIN RT ON R.A = RT.A WHERE R.B IN (select sub_id from LINKS start with father_id = :id connect by prior sub_id= father_id UNION ALL select :id sub_id from dual UNION ALL select sub_id from LINKS where :id = 0 ) AND (R.A=:a or :a=0) ORDER BY R.B, R.A
Also see the ecexution use it and explain how to call the select statement.
A goal of optimization often used is wo writing several queries and call the right according to the parameters.
Different queries may be optimized independently of each other and hence faster query where everything is condensed into one big.pls/sql example
if :a=0 and :b=0 then SELECT * into... FROM R JOIN RT ON R.A = RT.A ORDER BY R.B, R.A; elsif :b=0 then SELECT * into... FROM R JOIN RT ON R.A = RT.A where R.A=:a ORDER BY R.B, R.A; elsif :a=0 then SELECT * into... FROM R JOIN RT ON R.A = RT.A WHERE R.B IN (select sub_id from LINKS start with father_id = :id connect by prior sub_id= father_id UNION ALL select :id sub_id from dual) ORDER BY R.B, R.A; else /* both parameters are selected */ SELECT * into... FROM R JOIN RT ON R.A = RT.A WHERE R.B IN (select sub_id from LINKS start with father_id = :id connect by prior sub_id= father_id UNION ALL select :id sub_id from dual) and R.A=:a ORDER BY R.B, R.A; end;
-
Problem with "vpn sysopt connection permit.
Hi all
I would like to ask you for advice with "vpn sysopt connection permit". I have a problem with by-pass-access list (acl) in the INSIDE interface. As I understand it and I'm going to use this command, there is no need to especialy allow traffic in the access list for the INSIDE and I can control the filter-vpn traffic. But in my case it's quite the opposite, I want particularly to this INTERIOR acl traffi. When I allow this traffic inside acl L2L tunnel rises, hollow traffic flow vpn-fltr ane acl that everything is OK. But when I do not allow that this traffic is inside of the rule with Deny statement in acl INSIDE block traffic and tunnel goes ever upward. Part of the configuraciton which you can view below.
Please let me know if I'm wrong, or what I did wrong?
Thank you
Karel
PHA-FW01 # view worm | Worm Inc
Cisco Adaptive Security Appliance Software Version 4,0000 1
PHA-FW01 # display ru all sys
No timewait sysopt connection
Sysopt connection tcpmss 1380
Sysopt connection tcpmss minimum 0
Sysopt connection permit VPN
Sysopt connection VPN-reclassify
No sysopt preserve-vpn-stream connection
no RADIUS secret ignore sysopt
No inside sysopt noproxyarp
No EXT-VLAN20 sysopt noproxyarp
No EXT-WIFI-VLAN30 sysopt noproxyarp
No OUTSIDE sysopt noproxyarp
PHA-FW01 # display the id of the object-group ALGOTECH
object-group network ALGOTECH
object-network 10.10.22.0 255.255.255.0
host of the object-Network 172.16.15.11
PHA-FW01 # show running-config id of the object VLAN20
network of the VLAN20 object
subnet 10.1.2.0 255.255.255.0
L2L_to_ALGOTECH list extended access permitted ip object object-group VLAN20 ALGOTECH
extended access list ACL-ALGOTECH allow ip object-group object VLAN20 ALGOTECH
Note EXT-VLAN20 of access list =.
access list EXT-VLAN20 allowed extended ip object VLAN20 ALGOTECH #why object-group must be the rule here?
access list EXT-VLAN20 extended permitted udp object VLAN20 object-group OUT-DNS-SERVERS eq field
EXT-VLAN20 allowed extended VLAN20 object VPN-USERS ip access list
EXT-VLAN20 extended access list permit ip object VLAN20 OPENVPN-SASPO object-group
EXT-VLAN20 allowed extended object VLAN10 VLAN20 ip access list
deny access list extended VLAN20 EXT ip no matter what LOCAL NETS of object-group paper
EXT-VLAN20 allowed extended icmp access list no echo
access list EXT-VLAN20 allowed extended object-group SERVICE VLAN20 object VLAN20 everything
EXT-VLAN20 extended access list deny ip any any newspaper
extended access list ACL-ALGOTECH allow ip object-group object VLAN20 ALGOTECH
GROUP_POLICY-91 group policy. X 41. X.12 internal
GROUP_POLICY-91 group policy. X 41. X.12 attributes
value of VPN-filter ACL-ALGOTECH
Ikev1 VPN-tunnel-Protocol
tunnel-group 91.X41. X.12 type ipsec-l2l
tunnel-group 91.X41. X.12 General attributes
Group Policy - by default-GROUP_POLICY-91. X 41. X.12
tunnel-group 91.X41. X.12 ipsec-attributes
IKEv1 pre-shared-key *.
PHA-FW01 # show running-config nat
NAT (EXT-VLAN20, outdoors) static source VLAN20 VLAN20 static destination ALGOTECH ALGOTECH non-proxy-arp-search to itinerary
network of the VLAN20 object
dynamic NAT interface (EXT-VLAN20, outdoors)
group-access to the INTERIOR in the interface inside
Access-group interface VLAN20 EXT EXT-VLAN20
Hello
The command "sysopt connection permit-vpn" is the default setting and it applies only to bypass ACL interface to the interface that ends the VPN. It would be connected to the external network interface. This custom has no effect on the other interfaces ACL interface.
So if you initiate or need to open connections from your local network to remote network through the VPN L2L connection then you will need to allow this traffic on your LAN interface ACL networks.
If the situation was that only the remote end has launched connections to your network then 'sysopt permit vpn connection' would allow their connections around the external interfaces ACL. If If you have a VPN configured ACL filter, I think that the traffic will always accompany against this ACL.
Here are the ASA reference section to order custom "sysopt"
http://www.Cisco.com/en/us/docs/security/ASA/command-reference/S21.html#wp1567918
-Jouni
-
Add vNIC PortGroup scope with the REST API security group
I created a security group within the reach of a PortGroup via the REST API. Now, I'm trying to add a vNIC, also via the REST API.
To help illustrate, I have a JMTest1 VM with 1 vNIC connected to the PortGroup which is the scope of the security group. When I change this group the first item in the list that could be added to the security group is the first (and only) JMTest1 vNIC.
To add it via the REST API, I need to provide the following:
https://192.168.x.x/API/2.0/services/SecurityGroup/SecurityGroup-XX/members/ < member-moref >
for example I need to understand what the < member-moref > for the vNIC to JMTest1 and this is what I have a problem with.
If I add the vNIC through the user interface and then interrogate the security group it gives me below the answer:
< securitygroup >
< objectIdobjectId > securitygroup-xx < / objectId >
< type >
< typeName > SecurityGroup < / typeName >
< / type >
< name > JMTest7 < / name >
< description / >
< revision > 9 < / revision >
< objectTypeName > SecurityGroup < / objectTypeName >
< scope >
< id > dvportgroup-xxxxx / < ID >
< objectTypeName > DistributedVirtualPortgroup < / objectTypeName >
< name > dv-xxxxx < / name >
< / scope >
< extendedAttributes / >
< inheritanceAllowed > false < / inheritanceAllowed >
< member >
500758f6-b97b - 7A 79 - 0c < objectId > 04 - 996f53edf3f0.000 < / objectId >
< type >
Vnic < typeName > < / typeName >
< / type >
< name > JMTest1 - NIC 1 < / name >
< revision > 6 < / revision >
< objectTypeName > Vnic < / objectTypeName >
< scope >
< id > vm-xxxxx / < ID >
< objectTypeName > VirtualMachine < / objectTypeName >
< name > JMTest1 < / name >
< / scope >
< extendedAttributes / >
< / member >
< / securitygroup >
It seems that the < member-moref > for the JMTest1 vNIC is < objectId > 500758f6-b97b - a 7, 79 - 0c 04 - 996f53edf3f0.000 < / objectId >
If I run now:
then the vNIC is successfully added to the security group. (yay!) So I am now left with the task of how to get
500758f6-b97b - 7A 79 - 0c < objectId > 04 - 996f53edf3f0.000 < / objectId >
of a vNIC?
I have looked at the object in the Mob vCenter and via PowerCLI, but cannot see how to derive from it.
Anyone know the answer to that?
The uuid vnic is created by concatenating the vm instanceUuid + '. ' + the last three digits of the vnic device key. (The vnic is located in the area of the config.hardware.device of the virtual machine and the key will be to shape 4xxx, where xxx represents the 3 numbers you need).
-
Need help with query SQL Inline views + Group
Hello gurus,
I would really appreciate your time and effort on this application. I have the following data set.
Reference_No---Check_Number---Check_Date---description---Invoice_Number---Invoice_Type---Paid_Amount---Vendor_Number
1234567 11223 - 05/07/2008 -paid for cleaning- 44345563-I-* 20.00 *---19
1234567 11223 - 05/07/2008 - 44345563 -a--10,00---19 ofbad quality adjustment
7654321 11223 - 05/07/2008 - setting the last billing cycle - 23543556 - A - 50.00 - 19
4653456 11223 - 05/07/2008 - paid for cleaning - 35654765 - I - 30, 00-19
Please ignore '-' added for clarity
I'm writing a paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, aggregate query Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Type, Invoice_Number, Vendor_Number. When there are no more records I want to display the respective Description.
The query should return the following data set
Reference_No---Check_Number---Check_Date---description---Invoice_Number---Invoice_Type---Paid_Amount---Vendor_Number
1234567 11223 - 05/07/2008 -paid for cleaning- 44345563-I-* 10.00 *---19
7654321 11223 - 05/07/2008 - setting the last billing cycle - 23543556 - A - 50.00 - 19
4653456 11223 - 05/07/2008 - paid for cleaning - 35654765 - I - 30, 00-19
Here's my query. I'm a little lost.
Select b., A.sequence_id, A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
de)
Select sequence_id, check_number, check_date, invoice_number, sum (paid_amount) sum, vendor_number
of the INVOICE
Sequence_id group check_date, check_number, invoice_number, vendor_number
) A, B OF INVOICE
where A.sequence_id = B.sequence_id
Thank you
NickIt seems that this is a duplicate thread - correct me if I am wrong in this case->
Need help with query SQL Inline views + Group
Kind regards.
LOULOU.
-
Started MacBook and not charging with the charger connected. Charger works. Keyboard does not not except the power button / stop. Restarted ex drive no available help hoping that it was a matter of SW. SW and the operating system works very well. Read some info online. Tried to reset the SMC. Now, the MacBook does not start. Can U help me please. Thanks in advance.
MacBook, 2010, Mac OS X 10.6.8
Hi Veronica Steward,.
I understand that you have turned into a series of questions about your MacBook and want help getting the computer to turn on. Here is an article that can help you through the steps to fix this problem:
If your Mac will not turn on - Apple Support
Let us know how it goes and thanks for coming to Apple Support communities.
See you soon.
-
After a problem with a mobile connection, I tried with mozilla to connect to nooz.gr , but I've redirected the page services of vodafone, everytime I try again to open this site specific mozilla still redirects me vodafone support page, how can I regain access to nooz.gr?
Could clear you cookies and cache and check it out.
The problem happened when you connect your mobile connection he tries to display the service vodafone page, and it is not updated because of cookie issue.
Try in safe mode
-
Seeing orange blinking after that stop with the charger connected.
Hello
I'm new here and I have the following problem.
Four months after buying the PC (Satellite-Z930-103) I had a battery problem. Suddenly the orange led blinking and the laptop could not be launched. Toshiba service replaced the battery and everything worked fine for about two months. Then the same thing happened again. Because I was working in China, I opened the laptop and disconnected the battery and can be used with the charger connected.
The PC was sent to a point of service when I arrived at home, where they still indicated a battery problem and offered to change the battery. Because the warranty was expired, I had to pay for it. I refused, because the price they were charging was 300 euros.
To the surprise... when I received it from the point of service, they have reconnected the battery and the service report said "computer does work well, no fault found '...
Since I started my fight with Toshiba support and tried to find me a solution. However, the problem was not resolved until today and I have to disconnect the battery and plug it in again after waiting a day every time I forgot to unplug the charger when the shutdown or when he goes into sleepmode) I became an expert at opening and closing of the laptop...
In any case, maybe someone has the same problem and found a solution, I suppose that it may be a software problem, but Toshiba has not solved until today.
To describe the problem even once:
Orange (power) led starts flashing as soon as the computer is powered down (or "standby mode") with the charger connected.
If this happens, I opened the laptop and disconnect the battery (so I can work with feeder pugged in)
Then the next day I open the laptop again and re-connect the battery. It works fine until I still forget to unpug the charger, turn off or restart the laptop.I'm guessing it's a problem of software and a few simple arrangements or a re-installation of something, it can be solved.
Because I travel a lot, I love the little thing but I got frustrated at the moment.
It would be great if I'm right, and someone already had this thing resolved!
THX advance for any help!Peter
Satellite Z930 is very nice machine designed for mobile use and I am really sorry to hear that you have such problems. I imagine that you can't find some identical threads on this forum so I hope this isn't a "general problem" with this machine.
I am not a technician but I can't imagine this problem described has something to do with the settings of the software. In the BIOS, you cannot change a large part and in windows, there is a standard Windows power settings and several Toshiba avancΘs.
Electronic power placed in the motherboard has a few 'standard' functions and switch automatically between battery and power running.I can't say for sure, but I guess there must be some problem with electronic power supply. It is not easy to say what the problem is here, and I really wonder that Toshiba service provider was not able to identify the reason why all this is happening.
Let's see what the other members of the forum think problem described.
-
Loading slow web page and with Airport Extreme connection speeds
About 5 days ago, I noticed a significant slow down the loading of web pages. In some cases, I see a blank white screen for up to a minute before the page is loaded. In others, the page never loads at all. And sometimes they load very quickly as expected. This happens with my iMac that is connected to a router via ethernet, as well as my Macbook Air connected via Wifi to Airport Extreme.
I have a high-speed internet connection by cable Comcast/Xfinity: Blast 250Mbps.
I tried to connect my iMac directly on the model of cable (Arris Surfboard SB6183), and there is no problem. Download speed was 30Mbps 300Mbps and upload.
Then, I plugged my Airport Extreme router again and connected the iMac to the router via Ethernet. Download speed dropped to 100 Mbps!
This suggests that the problem may be with the Airport Express. It is not a wireless problem, because as you can see the download speed went from 300Mbp/s connected directly to the model 100 Mbps when connected to the Airport Extreme.
How do I troubleshoot/fix this?
One last thing: I did a speed test with my Macbook Air that is connected via Wifi and the Airport Extreme router. Download speed dropped to 35 MB/s.
Direct connection to the modem is so very fast, suggesting the internet connection Comcast isn't the issue. Speed falls even with ethernet to the Airport Extreme router connection, then decreases even more with the Wifi connection.
Maybe you are looking for
-
How to determine the outgoing (SMTP) server settings?
ErrorMessage:The message send failed.The message could not be sent to the help of Outgoing server (SMTP) smtp.uniq.edu.ht for an unknown reason. Check that your outgoing (SMTP) server settings are correct and try again... How to do this? The paramete
-
Safari will not fill my mail.
Safari will not fill my mail. I have OS X 10.11.3.
-
Cannot use bluetooth &; wifi in my hp 14-d002tx
I use Windows 7 Ultimate edition. But I have no drivers bluetooth & wifi for this operating system. Please suggest me the driver names and where I could find these drivers. Help, please.
-
Hello, I have a HP Pavilion g6-2170-ec, bought the week ago. First of all, nothing was wrong but I installed Steam and Call of Duty: MW2. During the game, view began to switch between the integrated display and double projector (in period of 10 to 20
-
Sometimes, when I load stuff the sansa screen is empty and when I try to delete a folder, it won't let me. When do not delete files, the pc says that the player has been disconnected or has stopped responding. Why these events happen?