Alternative for Hierarchical Queries
Hi all,
Is there any other way to implement the Hierachical Query in Oracle. Let us assume the following example of the Scott.emp Table. The output of the table must be in a Hierarchical manner as follows :
ORG_CHART EMPNO MGR JOB
KING 7839 PRESIDENT
JONES 7566 7839 MANAGER
SCOTT 7788 7566 ANALYST
ADAMS 7876 7788 CLERK
FORD 7902 7566 ANALYST
SMITH 7369 7902 CLERK
BLAKE 7698 7839 MANAGER
ALLEN 7499 7698 SALESMAN
WARD 7521 7698 SALESMAN
MARTIN 7654 7698 SALESMAN
TURNER 7844 7698 SALESMAN
JAMES 7900 7698 CLERK
CLARK 7782 7839 MANAGER
MILLER 7934 7782 CLERK
The above structure can be achieved using the following implementation by using the clauses namely CONNECT BY PRIOR, LEVEL and START WITH :
SELECT LPAD(' ',2*(LEVEL-1)) || ename org_chart,
empno, mgr, job
FROM emp
START WITH job = 'PRESIDENT'
CONNECT BY PRIOR empno = mgr;
The above query works fine without any issues.
But is there any other way to implement the above logic without using the above hierarchical query clauses.
Please help me on the above.
Thanks in advance.
Regards
Raj
Thanks.
Why I require this implementation is we have an software which runs both on Oracle and SQL Server, we accomplish the same very easily in Oracle by using the Hierarchical Queries already available in Oracle. But there is no such predefined keywords to implement in SQL server. That is the purpose of the above.
When we can achieve the same in a alternative way in Oracle, the same I feel can be implemented in SQL server.
Raj
Not without dropping into PLSQL, but this is not easy, and why bother, if your query works OK?
Similar Messages
-
Alternative For Select Queries
H All,
Is there a way to extract sales doc. number of corresponding billing doc. number with out using select query in a report?Ravi,
Is there a way to extract sales doc. number of corresponding billing doc. number with out using select query in a report?
I want to take shower without getting Wet?
Try with BAPI_SALESORDER_GETDETAILBOS,and see the details from BAPISDFLOW structure.
Amit. -
Help needed in understanding the concept of hierarchical queries
I really need help in this matter. I have a flafile containing about 4000 rows. It is from my supplier, it's structure is as follows:
create table Flatfile
(Pgroup varchar2(30),
Pclass varchar2(30),
Manufacturer varchar2(30),
Article varchar2(30),
Price Number(6,2));
Insert into Flatfile Values
('Application Software','Database Software','Oracle','Oracle 10G',115);
Insert into Flatfile Values
('Application Software','Database Software','Microsoft','MS SQL Server 2000',200);
Insert into Flatfile Values
('Application Software','Spreadsheet Software','Microsoft','Excel',100);
Insert into Flatfile Values
('Monitor','15"','Acer','Acer 15"" TFT superscreen',199);
Insert into Flatfile Values
('Monitor','15"','Sony','Sony R1500 flat',225);
Insert into Flatfile Values
('Monitor','17"','Philips','Philips Flatscreen',250);
Insert into Flatfile Values
('Monitor','19"','Viewsonic','Viewsonic PLasma Monitor',275);
Insert into Flatfile Values
('Processor','AMD','AMD','FX-55',600);
Insert into Flatfile Values
('Processor','Intel','Intel','P4 3 GHZ',399);
My goal is to make a hierarchical query with the start with and connect by clauses. From what I have read is that I need to normalize the data of the flatfile.
How do I achieve a table which I can query so that the query will represent the hierarchy that exists. Namely
Pgroup
++Pclasse
Application Software
++Database Software
++Spreadsheet Software
So a 2-level hierarchy. I'd like to understand this simple concept first. I built on the knowledge that I gain. So the questions are:
1.What do I need to do to make the table so that I can use a hierarchical query on it?
2. How should the query syntax be?
3. Is it also possible to get the data in the hierarchical query sorted asec?
I would only like to use the simple structures of the start with and connect by clauses first. I've read there are some new additions to 10G. The problem with the examples used by the tutorials is that the tables are already made so that they are suitable for hierarchical queries. I hope to understand it by this example. And take it a step further.
Sincerely,
PetePrimarily hierarchy query serves to process tree-like structures which RDBMS simulates using through parent-child relation, often in a single table (see famoust
EMP table where employee can have the manager who is an employee at the same time).
In your case it could look like:
SQL> select pgroup, pclass from flatfile;
PGROUP PCLASS
Application Software Database Software
Application Software Database Software
Application Software Spreadsheet Software
Monitor 15"
Monitor 15"
Monitor 17"
Monitor 19"
Processor AMD
Processor Intel
Application Software
Monitor
Processor
12 rows selected.
SQL> select decode(level,1,pclass,' ' || pclass), Manufacturer from flatfile
2 start with pgroup is null
3 connect by prior pclass = pgroup
4 /
DECODE(LEVEL,1,PCLASS,''||PCLASS MANUFACTURER
Application Software
Database Software Oracle
Database Software Microsoft
Spreadsheet Software Microsoft
Monitor
15" Acer
15" Sony
17" Philips
19" Viewsonic
Processor
AMD AMD
Intel Intel
12 rows selected.The hierarchy syntax is described completely in the documentation including
LEVEL and PRIOR keywords.
As for the ordering question you can use siblings ordering:
SQL> select decode(level,1,pclass,' ' || pclass), Manufacturer from flatfile
2 start with pgroup is null
3 connect by prior pclass = pgroup
4 order siblings by 1 desc
5 /
DECODE(LEVEL,1,PCLASS,''||PCLASS MANUFACTURER
Processor
Intel Intel
AMD AMD
Monitor
19" Viewsonic
17" Philips
15" Acer
15" Sony
Application Software
Spreadsheet Software Microsoft
Database Software Oracle
Database Software Microsoft
12 rows selected.Rgds. -
I've a table with these values.
ID Name Parent_ID
0 Organization1 null
1 Organization2 0
2 Organization3 0
3 Organization4 0
4 Organization5 1
5 Organization6 1
6 Organization7 2
7 Organization8 2
8 Organization9 4
9 Organization10 4
10 Organization11 6
11 Organization12 8.
Need to write a sql query in Oracle 9i to get the Hierarchy of Organization12:
The output should be:
Organization1
Organization9
Organization5
Organization2
Organization1.
The query should be dynamic. If I give Organization11, I should get the hierarchy of Organization11.
Organization1
Organization7
Organization3
Organization1.
Could anyone please help me in getting this output.See the followings links for hierarchical queries:
http://philip.greenspun.com/sql/trees.html
http://www.psoug.org/reference/connectby.html
--Manohar -
Hi
I have to simple sets of query Q1 and Q2, both of them have the SUM function used, they work just fine separately, but we want the result in 1 single query.
The problem is that Q1 has a master/detail table relation in the join so whenever we join both queries together the result set is being summed incorrectly according to the number of detail records found in the detail table.
Of course the solution is to use UNION operator.
What I'm looking for is an alternative for the UNION
Are there any analytical functions where I can have the result???
Query examples:
TONY@DEV> SELECT X.AUXL_RPRSNT_TITLE GRP_CODE,
2 M.AUXL_CODE,
3 D.ITEM_CODE,
4 SUM(DECODE(M.TMVS_OPERATION,6,D.MVTS_QUANTITY_R,11,-D.MVTS_QUANTITY_R)) QTY
5 FROM ISTD_STOCK_MOUVEMENTS_M M ,
6 ISTD_STOCK_MOUVEMENTS_D D,
7 IACD_AUXILIARIES X
8 WHERE M.TMVS_CODE = D.TMVS_CODE
9 AND M.CPNY_CODE = D.CPNY_CODE
10 AND M.BRCH_CODE = D.BRCH_CODE
11 AND M.MVTS_YEAR = D.MVTS_YEAR
12 AND M.MVTS_DOC_NUM = D.MVTS_DOC_NUM
13 AND X.AUXL_TYPE = M.AUXL_TYPE
14 AND X.AUXL_CODE = M.AUXL_CODE
15 AND X.AUXL_RPRSNT_TITLE = 1
16 AND M.AUXL_TYPE = 1
17 GROUP BY X.AUXL_RPRSNT_TITLE, M.AUXL_CODE, D.ITEM_CODE;
GRP_CODE AUXL_CODE ITEM_CODE QTY
1 33405 4030010 318
1 33405 4030020 22
1 33405 4030030 22
1 33408 4030010 14
1 33408 4030020 2
1 33408 4030030 2
1 33410 4030010 992
1 33410 4030020 42
1 33410 4030030 42
1 33413 4030010 789
1 33413 4030020 38
GRP_CODE AUXL_CODE ITEM_CODE QTY
1 33413 4030030 38
1 33413 5010008 1
1 33413 5010009 1
1 33413 5010012 1
15 rows selected.
Elapsed: 00:00:00.00
TONY@DEV> SELECT X.AUXL_RPRSNT_TITLE GRP_CODE,
2 X.AUXL_CODE,
3 SUM(NVL(A.AGAC_REV_AMT, 0)) AMT,
4 SUM(NVL(A.AGAC_AMT, 0)) PAID,
5 SUM(NVL(A.AGAC_REV_AMT, 0) - NVL(A.AGAC_AMT, 0)) DUE
6 FROM IACD_PAYM_AGING A,
7 IACD_AUXILIARIES X
8 WHERE X.AUXL_TYPE = A.AUXL_TYPE
9 AND X.AUXL_CODE = A.AUXL_CODE
10 AND X.AUXL_RPRSNT_TITLE = 1
11 GROUP BY X.AUXL_RPRSNT_TITLE, X.AUXL_CODE;
GRP_CODE AUXL_CODE AMT PAID DUE
1 33405 845.248 93.34 751.908
1 33408 37.334 13.34 23.994
1 33410 2655.328 394.68 2260.648
1 33413 2103.855 293.302 1810.553
Elapsed: 00:00:00.00
TONY@DEV> SELECT X.AUXL_RPRSNT_TITLE GRP_CODE,
2 M.AUXL_CODE,
3 D.ITEM_CODE,
4 SUM(DECODE(M.TMVS_OPERATION,6,D.MVTS_QUANTITY_R,11,-D.MVTS_QUANTITY_R)) QUANTITY,
5 NULL AMT,
6 NULL PAID,
7 NULL DUE
8 FROM ISTD_STOCK_MOUVEMENTS_M M ,
9 ISTD_STOCK_MOUVEMENTS_D D,
10 IACD_AUXILIARIES X
11 WHERE M.TMVS_CODE = D.TMVS_CODE
12 AND M.CPNY_CODE = D.CPNY_CODE
13 AND M.BRCH_CODE = D.BRCH_CODE
14 AND M.MVTS_YEAR = D.MVTS_YEAR
15 AND M.MVTS_DOC_NUM = D.MVTS_DOC_NUM
16 AND X.AUXL_TYPE = M.AUXL_TYPE
17 AND X.AUXL_CODE = M.AUXL_CODE
18 AND X.AUXL_RPRSNT_TITLE = 1
19 AND M.AUXL_TYPE = 1
20 GROUP BY X.AUXL_RPRSNT_TITLE, M.AUXL_CODE, D.ITEM_CODE
21 UNION
22 SELECT X.AUXL_RPRSNT_TITLE GRP_CODE,
23 X.AUXL_CODE,
24 NULL ITEM_CODE,
25 NULL QUANTITY,
26 SUM(NVL(A.AGAC_REV_AMT, 0)) AMT,
27 SUM(NVL(A.AGAC_AMT, 0)) PAID,
28 SUM(NVL(A.AGAC_REV_AMT, 0) - NVL(A.AGAC_AMT, 0)) DUE
29 FROM IACD_PAYM_AGING A,
30 IACD_AUXILIARIES X
31 WHERE X.AUXL_TYPE = A.AUXL_TYPE
32 AND X.AUXL_CODE = A.AUXL_CODE
33 AND X.AUXL_RPRSNT_TITLE = 1
34 GROUP BY X.AUXL_RPRSNT_TITLE, X.AUXL_CODE;
GRP_CODE AUXL_CODE ITEM_CODE QUANTITY AMT PAID DUE
1 33405 4030010 318
1 33405 4030020 22
1 33405 4030030 22
1 33405 845.248 93.34 751.908
1 33408 4030010 14
1 33408 4030020 2
1 33408 4030030 2
1 33408 37.334 13.34 23.994
1 33410 4030010 992
1 33410 4030020 42
1 33410 4030030 42
GRP_CODE AUXL_CODE ITEM_CODE QUANTITY AMT PAID DUE
1 33410 2655.328 394.68 2260.648
1 33413 4030010 789
1 33413 4030020 38
1 33413 4030030 38
1 33413 5010008 1
1 33413 5010009 1
1 33413 5010012 1
1 33413 2103.855 293.302 1810.553
19 rows selected.
Elapsed: 00:00:05.03
TONY@DEV>Here are samples from the joined query
TONY@DEV> ed
Wrote file afiedt.buf
1 SELECT X.AUXL_RPRSNT_TITLE GRP_CODE,
2 M.AUXL_CODE,
3 D.ITEM_CODE,
4 SUM(DECODE(M.TMVS_OPERATION,6,D.MVTS_QUANTITY_R,11,-D.MVTS_QUANTITY_R)) QUANTITY,
5 SUM(NVL(A.AGAC_REV_AMT, 0)) AMT,
6 SUM(NVL(A.AGAC_AMT, 0)) PAID,
7 SUM(NVL(A.AGAC_REV_AMT, 0) - NVL(A.AGAC_AMT, 0)) DUE
8 FROM ISTD_STOCK_MOUVEMENTS_M M ,
9 ISTD_STOCK_MOUVEMENTS_D D,
10 IACD_AUXILIARIES X,
11 IACD_PAYM_AGING A
12 WHERE M.TMVS_CODE = D.TMVS_CODE
13 AND M.CPNY_CODE = D.CPNY_CODE
14 AND M.BRCH_CODE = D.BRCH_CODE
15 AND M.MVTS_YEAR = D.MVTS_YEAR
16 AND M.MVTS_DOC_NUM = D.MVTS_DOC_NUM
17 AND X.AUXL_TYPE = M.AUXL_TYPE
18 AND X.AUXL_CODE = M.AUXL_CODE
19 AND X.AUXL_TYPE = A.AUXL_TYPE
20 AND X.AUXL_CODE = A.AUXL_CODE
21 AND X.AUXL_RPRSNT_TITLE = 1
22 AND M.AUXL_TYPE = 1
23* GROUP BY X.AUXL_RPRSNT_TITLE, M.AUXL_CODE, D.ITEM_CODE
TONY@DEV> /
GRP_CODE AUXL_CODE ITEM_CODE QUANTITY AMT PAID DUE
1 33405 4030010 10494 30428.928 3360.24 27068.688
1 33405 4030020 726 845.248 93.34 751.908
1 33405 4030030 726 845.248 93.34 751.908
1 33408 4030010 140 373.34 133.4 239.94
1 33408 4030020 20 37.334 13.34 23.994
1 33408 4030030 20 37.334 13.34 23.994
1 33410 4030010 56544 164630.336 24470.16 140160.176
1 33410 4030020 2394 10621.312 1578.72 9042.592
1 33410 4030030 2394 13276.64 1973.4 11303.24
1 33413 4030010 56808 151477.56 21117.744 130359.816
1 33413 4030020 2736 10519.275 1466.51 9052.765
GRP_CODE AUXL_CODE ITEM_CODE QUANTITY AMT PAID DUE
1 33413 4030030 2736 10519.275 1466.51 9052.765
1 33413 5010008 72 2103.855 293.302 1810.553
1 33413 5010009 72 2103.855 293.302 1810.553
1 33413 5010012 72 2103.855 293.302 1810.553
15 rows selected.
Elapsed: 00:00:03.09
TONY@DEV> ED
Wrote file afiedt.buf
1 SELECT X.AUXL_RPRSNT_TITLE GRP_CODE,
2 M.AUXL_CODE,
3 D.ITEM_CODE,
4 SUM(DECODE(M.TMVS_OPERATION,6,D.MVTS_QUANTITY_R,11,-D.MVTS_QUANTITY_R)) QUANTITY,
5 SUM(NVL(A.AGAC_REV_AMT, 0)) AMT,
6 SUM(NVL(A.AGAC_AMT, 0)) PAID,
7 SUM(NVL(A.AGAC_REV_AMT, 0) - NVL(A.AGAC_AMT, 0)) DUE
8 FROM ISTD_STOCK_MOUVEMENTS_M M ,
9 ISTD_STOCK_MOUVEMENTS_D D,
10 IACD_AUXILIARIES X,
11 IACD_PAYM_AGING A
12 WHERE M.TMVS_CODE = D.TMVS_CODE
13 AND M.CPNY_CODE = D.CPNY_CODE
14 AND M.BRCH_CODE = D.BRCH_CODE
15 AND M.MVTS_YEAR = D.MVTS_YEAR
16 AND M.MVTS_DOC_NUM = D.MVTS_DOC_NUM
17 AND X.AUXL_TYPE = M.AUXL_TYPE
18 AND X.AUXL_CODE = M.AUXL_CODE
19 AND X.AUXL_TYPE = A.AUXL_TYPE
20 AND X.AUXL_CODE = A.AUXL_CODE
21 AND A.AUXL_CODE = M.AUXL_CODE
22 AND A.AUXL_TYPE = M.AUXL_TYPE
23 AND X.AUXL_RPRSNT_TITLE = 1
24 AND M.AUXL_TYPE = 1
25* GROUP BY X.AUXL_RPRSNT_TITLE, M.AUXL_CODE, D.ITEM_CODE
TONY@DEV> /
GRP_CODE AUXL_CODE ITEM_CODE QUANTITY AMT PAID DUE
1 33405 4030010 10494 30428.928 3360.24 27068.688
1 33405 4030020 726 845.248 93.34 751.908
1 33405 4030030 726 845.248 93.34 751.908
1 33408 4030010 140 373.34 133.4 239.94
1 33408 4030020 20 37.334 13.34 23.994
1 33408 4030030 20 37.334 13.34 23.994
1 33410 4030010 56544 164630.336 24470.16 140160.176
1 33410 4030020 2394 10621.312 1578.72 9042.592
1 33410 4030030 2394 13276.64 1973.4 11303.24
1 33413 4030010 56808 151477.56 21117.744 130359.816
1 33413 4030020 2736 10519.275 1466.51 9052.765
GRP_CODE AUXL_CODE ITEM_CODE QUANTITY AMT PAID DUE
1 33413 4030030 2736 10519.275 1466.51 9052.765
1 33413 5010008 72 2103.855 293.302 1810.553
1 33413 5010009 72 2103.855 293.302 1810.553
1 33413 5010012 72 2103.855 293.302 1810.553
15 rows selected.
Elapsed: 00:00:00.02
TONY@DEV>
Note: If I join the A table with D table the query's returning no rows
Any suggestions are apreciated
Tony S. GarabedianHi Laurent,
I've got the same results as the joined tables without UNION.
The AUXL_CODE column is detail in the D table and master in M table the query is counting the detail records over and over when joined with table A where the function SUM is executed against columns in this table even with grouping sets
Am I correct??
I'm sure there is (must be) another way other than UNION and all the lengthy query but I just can't figure it out.
samples
1 SELECT X.AUXL_RPRSNT_TITLE,
2 X.AUXL_CODE,
3 D.ITEM_CODE,
4 DECODE(GROUPING(X.AUXL_CODE), 1, SUM(DECODE(M.TMVS_OPERATION,6,D.MVTS_QUANTITY_R,11,-D.MVTS_QUANTITY_R))) QTY,
5 SUM(NVL(A.AGAC_REV_AMT, 0)) AMT, SUM(NVL(A.AGAC_AMT, 0)) PAID,
6 SUM(NVL(A.AGAC_REV_AMT, 0) - NVL(A.AGAC_AMT, 0)) DUE
7 FROM ISTD_STOCK_MOUVEMENTS_M M ,
8 ISTD_STOCK_MOUVEMENTS_D D,
9 IACD_AUXILIARIES X,
10 IACD_PAYM_AGING A
11 WHERE M.TMVS_CODE = D.TMVS_CODE
12 AND M.CPNY_CODE = D.CPNY_CODE
13 AND M.BRCH_CODE = D.BRCH_CODE
14 AND M.MVTS_YEAR = D.MVTS_YEAR
15 AND M.MVTS_DOC_NUM = D.MVTS_DOC_NUM
16 AND X.AUXL_TYPE = M.AUXL_TYPE
17 AND X.AUXL_CODE = M.AUXL_CODE
18 AND X.AUXL_TYPE = A.AUXL_TYPE
19 AND X.AUXL_CODE = A.AUXL_CODE
20 AND A.AUXL_CODE = M.AUXL_CODE
21 AND A.AUXL_TYPE = M.AUXL_TYPE
22 AND X.AUXL_RPRSNT_TITLE = 1
23 AND M.AUXL_TYPE = 1
24* GROUP BY GROUPING SETS((X.AUXL_RPRSNT_TITLE), (X.AUXL_RPRSNT_TITLE, X.AUXL_CODE), (X.AUXL_RPRSNT_TITLE, X.AUXL_CODE, D.ITEM_CODE))
TONY@RIM> /
AUXL_RPRSNT_TITLE AUXL_CODE ITEM_CODE QTY AMT PAID DUE
1 33405 4030010 32162.102 3453.58 28708.522
1 33405 4030020 869.246 93.34 775.906
1 33405 4030030 869.246 93.34 775.906
1 33405 33900.594 3640.26 30260.334
1 33408 4030010 373.34 133.4 239.94
1 33408 4030020 37.334 13.34 23.994
1 33408 4030030 37.334 13.34 23.994
1 33408 448.008 160.08 287.928
1 33410 4030010 171351.747 24864.84 146486.907
1 33410 4030020 10879.476 1578.72 9300.756
1 33410 4030030 13599.345 1973.4 11625.945
AUXL_RPRSNT_TITLE AUXL_CODE ITEM_CODE QTY AMT PAID DUE
1 33410 195830.568 28416.96 167413.608
1 33413 4030010 157668.977 21411.046 136257.931
1 33413 4030020 10799.245 1466.51 9332.735
1 33413 4030030 10799.245 1466.51 9332.735
1 33413 5010008 2159.849 293.302 1866.547
1 33413 5010009 2159.849 293.302 1866.547
1 33413 5010012 2159.849 293.302 1866.547
1 33413 185747.014 25223.972 160523.042
1 141375 415926.184 57441.272 358484.912
20 rows selected.
TONY@RIM> SELECT X.AUXL_RPRSNT_TITLE GRP_CODE,
2 M.AUXL_CODE,
3 D.ITEM_CODE,
4 SUM(DECODE(M.TMVS_OPERATION,6,D.MVTS_QUANTITY_R,11,-D.MVTS_QUANTITY_R)) QUANTITY,
5 SUM(NVL(A.AGAC_REV_AMT, 0)) AMT,
6 SUM(NVL(A.AGAC_AMT, 0)) PAID,
7 SUM(NVL(A.AGAC_REV_AMT, 0) - NVL(A.AGAC_AMT, 0)) DUE
8 FROM ISTD_STOCK_MOUVEMENTS_M M ,
9 ISTD_STOCK_MOUVEMENTS_D D,
10 IACD_AUXILIARIES X,
11 IACD_PAYM_AGING A
12 WHERE M.TMVS_CODE = D.TMVS_CODE
13 AND M.CPNY_CODE = D.CPNY_CODE
14 AND M.BRCH_CODE = D.BRCH_CODE
15 AND M.MVTS_YEAR = D.MVTS_YEAR
16 AND M.MVTS_DOC_NUM = D.MVTS_DOC_NUM
17 AND X.AUXL_TYPE = M.AUXL_TYPE
18 AND X.AUXL_CODE = M.AUXL_CODE
19 AND X.AUXL_TYPE = A.AUXL_TYPE
20 AND X.AUXL_CODE = A.AUXL_CODE
21 AND A.AUXL_CODE = M.AUXL_CODE
22 AND A.AUXL_TYPE = M.AUXL_TYPE
23 AND X.AUXL_RPRSNT_TITLE = 1
24 AND M.AUXL_TYPE = 1
25 GROUP BY X.AUXL_RPRSNT_TITLE, M.AUXL_CODE, D.ITEM_CODE;
GRP_CODE AUXL_CODE ITEM_CODE QUANTITY AMT PAID DUE
1 33405 4030010 11118 32162.102 3453.58 28708.522
1 33405 4030020 748 869.246 93.34 775.906
1 33405 4030030 748 869.246 93.34 775.906
1 33408 4030010 140 373.34 133.4 239.94
1 33408 4030020 20 37.334 13.34 23.994
1 33408 4030030 20 37.334 13.34 23.994
1 33410 4030010 58812 171351.747 24864.84 146486.907
1 33410 4030020 2436 10879.476 1578.72 9300.756
1 33410 4030030 2436 13599.345 1973.4 11625.945
1 33413 4030010 59130 157668.977 21411.046 136257.931
1 33413 4030020 2774 10799.245 1466.51 9332.735
GRP_CODE AUXL_CODE ITEM_CODE QUANTITY AMT PAID DUE
1 33413 4030030 2774 10799.245 1466.51 9332.735
1 33413 5010008 73 2159.849 293.302 1866.547
1 33413 5010009 73 2159.849 293.302 1866.547
1 33413 5010012 73 2159.849 293.302 1866.547
15 rows selected.
TONY@RIM>This is with UNION and these are the correct results
TONY@RIM> ed
Wrote file afiedt.buf
1 SELECT X.AUXL_RPRSNT_TITLE GRP_CODE,
2 M.AUXL_CODE,
3 D.ITEM_CODE,
4 SUM(DECODE(M.TMVS_OPERATION,6,D.MVTS_QUANTITY_R,11,-D.MVTS_QUANTITY_R)) QUANTITY,
5 NULL AMT,
6 NULL PAID,
7 NULL DUE
8 FROM ISTD_STOCK_MOUVEMENTS_M M ,
9 ISTD_STOCK_MOUVEMENTS_D D,
10 IACD_AUXILIARIES X
11 WHERE M.TMVS_CODE = D.TMVS_CODE
12 AND M.CPNY_CODE = D.CPNY_CODE
13 AND M.BRCH_CODE = D.BRCH_CODE
14 AND M.MVTS_YEAR = D.MVTS_YEAR
15 AND M.MVTS_DOC_NUM = D.MVTS_DOC_NUM
16 AND X.AUXL_TYPE = M.AUXL_TYPE
17 AND X.AUXL_CODE = M.AUXL_CODE
18 AND X.AUXL_RPRSNT_TITLE = 1
19 AND M.AUXL_TYPE = 1
20 GROUP BY X.AUXL_RPRSNT_TITLE, M.AUXL_CODE, D.ITEM_CODE
21 UNION
22 SELECT X.AUXL_RPRSNT_TITLE GRP_CODE,
23 X.AUXL_CODE,
24 NULL ITEM_CODE,
25 NULL QUANTITY,
26 ROUND(SUM(NVL(A.AGAC_REV_AMT, 0)), 2) AMT,
27 ROUND(SUM(NVL(A.AGAC_AMT, 0)), 2) PAID,
28 ROUND(SUM(NVL(A.AGAC_REV_AMT, 0) - NVL(A.AGAC_AMT, 0)), 2) DUE
29 FROM IACD_PAYM_AGING A,
30 IACD_AUXILIARIES X
31 WHERE X.AUXL_TYPE = A.AUXL_TYPE
32 AND X.AUXL_CODE = A.AUXL_CODE
33 AND X.AUXL_RPRSNT_TITLE = 1
34* GROUP BY X.AUXL_RPRSNT_TITLE, X.AUXL_CODE
TONY@RIM> /
GRP_CODE AUXL_CODE ITEM_CODE QUANTITY AMT PAID DUE
1 33405 4030010 327
1 33405 4030020 22
1 33405 4030030 22
1 33405 869.25 93.34 775.91
1 33408 4030010 14
1 33408 4030020 2
1 33408 4030030 2
1 33408 37.33 13.34 23.99
1 33410 4030010 1014
1 33410 4030020 42
1 33410 4030030 42
GRP_CODE AUXL_CODE ITEM_CODE QUANTITY AMT PAID DUE
1 33410 2719.87 394.68 2325.19
1 33413 4030010 810
1 33413 4030020 38
1 33413 4030030 38
1 33413 5010008 1
1 33413 5010009 1
1 33413 5010012 1
1 33413 2159.85 293.3 1866.55
19 rows selected.
TONY@RIM>Thanks again,
Tony
Message was edited by:
Tony Garabedian -
Map the hierarchical queries pseudo column level
Is it possible to map the hierarchical queries pseudo column level in the Workbench?
I have an object for which I use connect by queries. I would like to map the pseudo column level. is this possible? if yes, how
If the mapping is not possible how do I get the level using a report query?
Is there any other way to retrive level?
Thanks
Edited by: amehta5 on May 4, 2010 11:52 AM
Edited by: amehta5 on May 4, 2010 11:55 AMThanks James, appreciate your feedback.
I tried report.addItem("level", builder.getFunction("LEVEL"));
but the query TopLink generates has LEVEL() and it errors out.
Code -
Expression startExpr = null;
Expression connectBy = builder.get("manager");
Vector<Expression> order = new Vector<Expression>();
order.addElement(builder.get("name"));
report.setHierarchicalQueryClause(startExpr, connectBy, order);
report.addAttribute("name");
report.addItem("level", builder.getFunction("LEVEL"));
Query generated by TOpLink - SELECT NAME, ID, LEVEL() FROM EMPLOYEE WHERE (EMPLOYEE_TYPE = ?) CONNECT BY PRIOR EMPLOYEE.MANAGER_ID = EMPLOYEE.ID ORDER SIBLINGS BY NAME
bind => [M]
Edited by: amehta5 on May 6, 2010 6:11 AM -
Hi,
i use in my query different hierarchie nodes in columns
For example
COL A: Hierarchie Countries Node: World
COL B: Hierarchie Countries Node: Europe
in an other Query
COL A: Hierarchie Countries Node: World
COL B: Hierarchie Countries Node: Asia
So i'd like to use a variable for these nodes to enter Europe or Asia.
I've a variable for hierarchies, but when i use it i can only enter the name of the hierarchie. Here countries.
How can i enter nodes in a variable and store them in one workbook with different tables. Both tables use the same query but one with Europe and one with Asia
Thanks for help .... MarkusMarkus,
Do you mean to say :
You have different hiearchies in two different queries and would like to enter only the node ? did you try using Hierarchy node variables - this should allow you to select a hierarchy node from the hierarchy attached.
or did you mean that depending on your node input the query should select the appropriate hierarchy - you can try this using a user exit not sure if it can be done in any other way.
Arun -
Hierarchical queries (mother, father,...)
Hi guys,
a have a problem that I think it can be solved whit hierarchical queries.
the question is like this. I have a person ...and each person has a mother and a father. (the mother/father may be or not be in my table)
so my table configuration will be like:
create table person
(id number,
name varchar2 (100),
mother_id number,
father_id number
the questions I need to answer are like:
1. return all persons that have all grandparents (or grand grand parents) in this table.(including parents). so entire genealogic tree is intact
e.g. if the grandfather is not in the table I will not display that person we cannot display that person
I have something like:
insert into person values (1,'alin', 4, 5); ----id 1 has parents in my table: 4 the mother, 5 the father
insert into person values (4,'mother_1', 8,null); ----id 4 has only her mother in table
insert into person values (5, 'father_1',9,10);--id 5(who is father of 1) with 9 as mother and 10 as father
insert into person values (8, 'grant',null ,null );
insert into person values (9, 'aaa',11,12);
insert into person values (10,'bbb',13,14);
insert into person values (11,'ccc',null ,null);
insert into person values (12, 'ddd',null ,null);
insert into person values (13,'eee',15,16);
insert into person values (14,'fff',17,18);
insert into person values (15,'ggg',null ,null);
insert into person values (16,'hhh',null ,null);
insert into person values (17,'iii',null ,null);
insert into person values (18,'jjjj',null,null);
in this configuration my select should return id 5 with his relatives and id 10 with this relatives because only them have all relatives in table:
id | name | mother | father | mather_mother (mm) | father_mother (fm) | mf | ff
5 | 'father' | 9 | 10 | 11 | 12 | 13 | 14
10 | bbb | 13 | 14 | 15 | 16 | 17 | 18
and second select ..if I input an id..lets say 1 to get the same result..like:
id | name | mother | father | mather_mother (mm) | father_mother (fm) | mf | ff
1 | 'alin' | 4 | 5 | 8 | null | 9 | 10and maybe at some point I want to show level 3(with grand grand parents)
please se the attached picture:
link: [http://picasaweb.google.com/alinbor/Tree#5428860555382426514]
ThanksHi,
Here's a revised version of the String Aggregation approach.
For :level_cnt = 3, it produces this output:
ROOT_ID ROOT_NAME ANCESTORS
M F MM FM MF FF
5 father_1 9 10 11 13 12 14
10 bbb 13 14 15 17 16 18The last part, starting with UNION, gets the "header " row.
The first part, before UNION, is exactly what I posted yesterday.
WITH got_tree AS
SELECT id
, name
, CONNECT_BY_ROOT id AS root_id
, CONNECT_BY_ROOT name AS root_name
, LEVEL AS level_num
, CASE
WHEN id = PRIOR mother_id
THEN 'M'
ELSE 'F'
END AS mf_flag
, SYS_CONNECT_BY_PATH ( CASE
WHEN id = PRIOR mother_id
THEN 'M'
ELSE 'F'
END
) AS gender_path
FROM person p
CONNECT BY ( id = PRIOR mother_id
OR id = PRIOR father_id
AND LEVEL <= :level_cnt
, got_sa_num AS
SELECT t.*
, ROW_NUMBER () OVER ( PARTITION BY root_id
ORDER BY level_num
, mf_flag DESC
, gender_path DESC
) AS sa_num
FROM got_tree t
SELECT root_id
, root_name
, REPLACE ( SYS_CONNECT_BY_PATH ( TO_CHAR (id, '9999')
) AS ancestors
FROM got_sa_num
WHERE sa_num = POWER (2, :level_cnt) - 1
START WITH sa_num = 2
CONNECT BY sa_num = PRIOR sa_num + 1
AND root_id = PRIOR root_id
UNION
SELECT NULL AS root_id
, NULL AS root_name
, REPLACE ( SYS_CONNECT_BY_PATH ( LPAD ( SUBSTR ( REPLACE ( gender_path
, 2
, 5 -- 1 + number of '9's in TO_CHAR, 13 lines up
) AS ancestors
FROM got_sa_num
WHERE sa_num = POWER (2, :level_cnt) - 1
START WITH sa_num = 2
CONNECT BY sa_num = PRIOR sa_num + 1
AND root_id = PRIOR root_id
ORDER BY root_id NULLS FIRST
; -
Hierarchical queries no longer working with 11gr2
Hi, next hierarchical query:
SELECT REF(f) FROM STORE1.FOLDER_TABLE f
WHERE LEVEL > 1 START WITH REF(f) = ? CONNECT BY PRIOR f.key$ = f.header$.parentKey
used to work fine with 11gr1, while with 11gr2 it raises ORA-00904: "F": invalid identifier.
This seems to occur for all hierarchical queries.Here's a simple script for SQL developer:
CREATE OR REPLACE TYPE Test_type AS OBJECT (
key RAW(16),
parentKey RAW(16))
CREATE TABLE Test_table OF Test_type (
key DEFAULT SYS_GUID() PRIMARY KEY)
OBJECT IDENTIFIER IS PRIMARY KEY;
SELECT REF(f) FROM Test_table f
START WITH f.parentKey IS NULL
CONNECT BY PRIOR f.key = f.parentKey
Returning ref(f) raises ORA-00904: "F": invalid identifier.
Returning f.key succeeds. -
JLayeredPane alternative for SWT
Hi there,
I have recently started working in SWT, have been long working in Swing.
I post this question here since I didn't find any specific forum for SWT queries, which btw I think is the need of the hour. SWT definitely calls for a separate dedicated forum like we have for Swing.
Basically what my goal is to develop a Chess game for online-game play on popular chess servers like FICS, ICC, there is another open source client (pretty good) developed in java swing available by the name JIN, perhaps you know of it too.
I do like JIN, but I personally like the interface of BabasChess better and more comfortable, but its not available as such for linux environment and so I decided that I should go ahead and build one for myself. If it is good enough, I will even release it as an open source applications for others' use.
Since SWT is much better than Swing in terms of both accessibility in general and visually too, I am certain I want to do it in SWT rather than in Swing.
Coming to my question, I am wondering if there is an alternative for JLayeredPane of Swing in SWT so that I can create a Chess board and easily allow Drag and Drop of chess pieces for moves. I will really appreciate any help or input that I can get from here since I am very serious about developing a game.
Apart from that I also want to know how logical or appropriate it is to use Swing components in SWT?
Thanks & Regards,
Dhruva SagarWell then what alternatives do I have to be able to make a drag and drop enabled chess game in SWT and not in Swing.
I know this is a Swing forum but there is no SWT forum, where should I post it otherwise?
I guess I will try at JavaRanch and java-forums.org. Thanks for your help. -
Hierarchical Queries / Rowsets via JDBC?
I have an application that requires data from an SQL database to be displayed in a hierarchical directed graph (i.e. a JTree). Is it possible to construct Hierarchical Queries that return Hierarchical Rowsets in servlets via JDBC? This would be akin to using a Microsoft ADODB Hierarchical Recordset object, but I need a Java solution that's portable and isn't dependent on proprietary extensions in a server (i.e. iPlanet, etc.).
Any help would be appreciated.
Thanks.Is it possible to construct Hierarchical Queries that
return Hierarchical Rowsetsin servlets via JDBC?No. JDBC only speaks in flat rows and columns.
What type of hierarchical rowset are you referring to? Is it multidimensional data? For example MSSqlServer OLAP with row, column and measure dimensions? Or, is it simply grouped data for which you are both summarizing and displaying detail? -
Single Selection Prompt for all queries
Hi All,
I have 5 queries in my workbook.
Each query uses some variable which is not used in the other queries.
So, i get 5 prompts when i run the query.
This looks very awkward and also my system almost hangs while showing for each prompt.
Is there any way of showing all the variables in a single prompt?
Thanks,
Arun KKHi,
I found this answer on a message board:
http://sap.ittoolbox.com/groups/technical-functional/sap-bw/assigning-same-variable-values-for-queries-in-workbook-711943
There are two answers to your question, depending on whether or not all of the dates are the same info object (i.e., have the same technical name).
Case 1: All info objects have the same technical name.
Use the "refresh all" option:
Run "SAPBEX.XLA!SAPBEXrefresh", True 'true = refresh all
BW will use the input from the first set of refresh screen variables for all others. IF each query have exactly the same refresh screen, you will not see another refresh variable screen. If not, then BW will stop between queries to ask for more input, only if required.
So, the trick would be to define the first query that refreshes in such a way that its variable screen covers everything needed.
Case 2: Not all info objects have the same technical name.
Use the "Save and reuse variable values" option. You will find this in the query Properties dialog box on the second tab ("Interaction"). Put a check there for all queries except the first one.
Set up the workbook so that the first query refreshes by itself
Run "SAPBEX.XLA!SAPBEXrefresh", False 'False = refresh one only
Then, after the first query has refreshed, locate the date(s) the user selected and copy those to the ranges for all other date values. If your installation is BW 3.x, the code below should do the trick.
It is slightly ineffecient because it will refresh the first query twice. But, the second refresh will be fast because the results will still be in cache. And, the code to keep it from refreshing the first query twice is not something you really want to mess with.
Alternatively, instead of using the SAPBEXrefresh, True at the end, you could put in 13 different SAPBEXrefresh, False statements, identifying the other 13 query worksheets individually.
Run "SAPBEX.xla!SAPBEXrefresh", False, _
Sheets("firstQueryToRefresh").Range("a cell in the query")
'locate dates user selected for "0CALMONTH"
For i = numFilter + 3 To 4 Step -1
If Range("GS" & i) = "0CALMONTH" Then
myType = Range("GC" & i)
myInterval = Range("GD" & i)
intfirstMonth ("GE" & i)
extfirstMonth = Range("GF" & i)
intlastMonth = Range("GG" & i)
extlastMonth = Range("GH" & i)
Exit For
End If
Next i
For i = 4 To numFilters + 3
If Range("GS" & i) = "YCODOCMNT" Or _
Range("GS" & i) = "YCOSHPMNT" Or _
Range("GS" & i) = "0CALMONTH" Or _
Range("GS" & i) = "YCOACCMT" Then
Range("GC" & i) = myType
Range("GD" & i) = myInterval
Range("GE" & i) = intfirstMonth
Range("GF" & i) = extfirstMonth
Range("GG" & i) = intlastMonth
Range("GH" & i) = extlastMonth
End If
Next i
Sheets("SAPBEXqueries").Visible = xlSheetVeryHidden
Sheets("firstQueryToRefresh").Select
Application.ScreenUpdating = True
Run "SAPBEX.xla!SAPBEXrefresh", False
End Sub
maybe this will help a bit.
regards,
Raymond Baggen
Uphantis bv -
Self join vs Hierarchical Queries
Hi,
please tel me which one 1 should use ?
i have to get simple manger's employ list, should i use self join or hierarchical queries(CONNECT BY and prior)?
yours sincerelyHi,
944768 wrote:
Hi,
please tel me which one 1 should use ?
i have to get simple manger's employ list, should i use self join or hierarchical queries(CONNECT BY and prior)?It depends on your data and your requirements.
Whenever you have a question, please post a little sample data (CREATE TABLE and INSERT statements) for all the tables involved, so the people who want to help you can re-create the problem and test their ideas. Also post the results you want from that data, and an explanation of how you get those results from that data.
Explain, using specific examples, how you get those results from that data.
If you can show what the problem is using commonly available tables (suc as scott.emp, which has a 4-level hierarchy) then you don't have ot post any sample data, just the results and the explanation.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ {message:id=9360002}
If your hierarchy consists only of 2 levels, then a self-join will probably be more efficient, simpler to code, and easier to maintain.
If you don't know how many levels are in the hierarchy, then self-join isn't an option. Use CONNECT BY or, if you have Oracle 11.2, a recursive WITH clause.
If you have a fixed number of levels (or an upper bound) greater than 2, then CONNECT BY (or a recursive WITH clause) will probably be best. -
Hierarchal queries and USER_DEPENDENCIES
I'm new to Hierarchal queries; I have a Hierarchal query on USER_DEPENDENCIES that does not work and gives an error
'TSPKG_ES' is a package the works; I'm trying to do query to see what is calls
SELECT LEVEL, D.*
FROM DBA_DEPENDENCIES D
START WITH D.name = 'TSPKG_ES'
CONNECT BY PRIOR D.name = D.referenced_name
Result = ORA-00600: internal error code, arguments: [sorsikbeg_1], [5], [0] ,[],[],[],[],[]
SELECT LEVEL, D.*
FROM DBA_DEPENDENCIES D
START WITH D.name = 'DATATESTSPKG_ES'
CONNECT BY PRIOR D.referenced_name = D.name
Result = ORA=0143: CONNECT BY loop in user dataOS = Microsoft Windows XP V2002 Service Pack 3
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
"CORE 10.2.0.4.0 Production"
TNS for 64-bit Windows: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production -
Values not getting displayed in the selection screen for Hierarchies?
Hi All,
Our system was upgraded from BW 3.1 to 2004s. The quality system displayes values when query result is filtered for a hierarchy infoobject but the production system does not display any values and sometimes throws an error 'The operation failed because of a server error. See system logs.' There is no changes done for hierarchies in both the systems.
Please let me know if anyone has had similar problems.
Thanks,
KrishnaHi All,
Our system was upgraded from BW 3.1 to 2004s. The quality system displayes values when query result is filtered for a hierarchy infoobject but the production system does not display any values and sometimes throws an error 'The operation failed because of a server error. See system logs.' There is no changes done for hierarchies in both the systems.
Please let me know if anyone has had similar problems.
Thanks,
Krishna
Maybe you are looking for
-
ATA vs VG202 vs VIC3-2FXS-E/DID & Wireless
What is best solution moving forward? Fax server probably two years out. Background: I have 34 locations 30 locations have 2 analog devices 4 Operations have 26, 14, 15, 10 analog devices Currently we use ATA 188 and 186 and one VIC2-2FXS We Have int
-
PSE8 doesn´t read DNG from mFT cameras - why?
Hi all, I just installled PSE8 trial and at once tried to open my (from RAW to DNG converted files from my Olympus E-P1 and Lumix G1, but Bridge couldn´t display them. If I opened them i Camera Raw everything worked OK. This is strange since there is
-
Would like to know how to use the spool at the backend to create the pdf output in webdynpro. Thanks
-
Table for mitigation control frequency
Hi, We are trying to build a program to be able to send notifications to mitigation monitors. For this I am trying to find the tables where the relevant details of the mitigation controls are located. I need the following information from the tables:
-
Multiple open db connections in a weblogic cluster
we have created a cluster in weblogic with oracle database. Whenever we are starting the cluster (managed servers), there are a lot of connections are opening in database (around 150 in database). Although we have set max capacity of cgDataSource, cg