Multiple with clause
Hello guys.
Can i use multiple time WITH clause in one query..
I can't find the way.
I am trying to do somtehing like :
with a as
select 1 from x
with b as
select 2 from y
select a.1,b.2
from a , b
HI all,
I have a requirement to write insert script using with clauses, some thing like this...
with a as
select 1 as col from x
b as
select 1 as col1, 2 as col2 from y
where 1 = a.col
insert into y
(id,1, 2)
select seq.nextval,
a.col,
b.col2
from a, b
Please advice.
Similar Messages
-
Avoiding WITH clause when querying Multiple Facts
OBIEE 10.1.3.4
We have a report which requires that we query multiple facts, which share the same Item dimension.
When we run the report, the BI Server generates a query using the WITH clause -
WITH SAWITH0 ( SELECT.............)
SAWITH1( SELECT .............)
SELECT .........
FROM SAWITH0 FULL OUTER JOIN SAWITH1
The above query errors out with ORA-918 Column Ambiguously Defined. I cannot find any issue in the query that has been generated.
The query does fail from SQL Plus with the same ORA error. But each of the select statements executed properly and the complete statement executes when I replace the FULL OUTER JOIN with INNER JOIN.
I can only assume that this is a bug in Oracle Database.
Now at times I have seen OBIEE generate separate queries when querying multiple facts and then combine the data together in memory.
Is there a way that I can force OBIEE to avoid using WITH clause and run separate SELECT statements ? How does the BI Server choose between using a WITH clause or running separate queries ?
Any help would be appreciated.I do not have a multi-dimensional data source.
My connection pool seems fine to me, I am not sure I need to do anything with the connection pool.
Hi Turribeach,
I understand that BI Server uses WITH clause for multiple facts. But I have also seen it issue separate queries against the database.
So what I am looking to understand under what situation does it issue separate queries i.e. No WITH clause ?
Thanks -
Multiple Start with clause-Hiercharical Query
Hi,
Can we use more than 1 "start with" clause .In one of our requirement there can be a parent id can be null or not null
START WITH (gpo.orig_po_confirmation_no IS NULL OR gpo.orig_po_confirmation_no IS NOT NULL)
CONNECT BY PRIOR gpo.po_confirmation_no =gpo.orig_po_confirmation_no;When I executed I got below error
ORA-01436: CONNECT BY loop in user data
01436. 00000 - "CONNECT BY loop in user data"
*Cause:
*Action:Any suggestion to resolve the error
Regards,
AchyutAchyut K wrote:
Hi,
Can we use more than 1 "start with" clause .In one of our requirement there can be a parent id can be null or not nullNo you cant use more than one START WITH clause. But you can specify multiple CONDITION in the START WITH clause.
When I executed I got below error
ORA-01436: CONNECT BY loop in user data
01436. 00000 - "CONNECT BY loop in user data"
*Cause:
*Action:
To diagnose this we need to see the data. One quick try would be to using NOCYCLE in your CONNECT BY clause. -
Report with multiplying columns and WITH clause
Hello
I saw sth strange in my report. I assume that it could happens very often.
I have report with few columns which two of them ar most complicated (many joins subqueries aggreagations on joined values etc.) These two columns (i.e C3,C4) should be multiplied (C3*C4).
When i do pure report without multiplying only columns C1,C2,C3,C4 everything is ok - duration about 15 sec. but... when I put next column on report which multiply these columns (in Answers C5=C3*C4)
I wait 3-4 minutes and my database hungs :(. After investigation I saw that in first case to databese goes pure "SELECT" statement it means:
"Select ... as C1, ... as C2, max(...) as C3, sum(xxx)... C4 from yyy,sss,ttt WHERE aaa"
but in second case BI uses WITH clause it means:
WITH SAWITH0 AS
( Select ... as C1, ... as C2, max(...) as C3, sum(xxx)... C4 from yyy,sss,ttt WHERE aaa )
SELECT SAWITH0.C1 as C1,
SAWITH0.C2 as C2,
SAWITH0.C3 as C3,
SAWITH0.C4 as C4,
SAWITH0.C3*SAWITH0.C4 as C5 FROM SSS
and this statement is long runninq query and kills my database :(.
I checked that SQL like this:
Select ... as C1, ... as C2, max(...) as C3, sum(xxx)... C4, max(...)*sum(xxx)... As C5 from yyy,sss,ttt WHERE aaa" -
runs few times faster than that above
I know that I can do this multiply in business model layer but sometimes users can multiply(or other operations) on columns in reports without my knowledge and it kills my db :(. Where is bug? Why SQLs with WITH clause takes so much db time?
Thank you for each kind of helpWITH clause or Subquery Factoring allows the set of data to be reused multiple times within the SQL. Oracle will usually materialize the data into a temporary table (you will see it if you take an explain plan of the SQL).
I would be surprised if it was the actual WITH clause that was causing the performance issue, however you can test this by turning the WITH clause feature off. Go to the Physical model, right mouse click on your Database > Properties > Features Tab, scroll down to WITH_CLAUSE_SUPPORTED and switch it off.
I'd be interested to know if you do see actual improvement.
Good Luck. -
WITH clause query:- Returns No Rows
Hi,
I am using the following query with, WITH clause, but some how it give "0 rows" returned,
The underlying query does returns row when run in stand alone mode,
With v_score_his_old
As
SELECT schs_run_date,schs_loc_shortcut_source,schs_lot_number,schs_facility_new,schs_facility_old, schs_operation_new,
schs_operation_old,schs_route_new,schs_route_old,schs_product_new,schs_product_old,schs_owner_old,schs_intransit,
schs_date,schs_transaction,schs_quantity_1_new,schs_quantity_1_old,/*0,*/schs_if_count_new,schs_if_count_old,
schs_preprocessed,schs_consumed_quantity,/*'Y',*/schs_if_id,schs_if_id_old,fif_fac_loc_id
FROM scm_score_his,full_item_facilities,location_params
WHERE schs_state = 0
AND fif_if_id = schs_if_id_old
AND lopo_param_value = to_char(fif_site_id_fm)
AND lopo_param_name IN ('SITE_FAB', 'SITE_SORT')
AND (schs_loc_shortcut_source = 'MP' OR schs_loc_shortcut_source = 'RE' OR schs_loc_shortcut_source = 'VI')
UNION ALL
SELECT schb_run_date,schb_loc_shortcut_source,schb_lot_number,schb_facility_new,schb_facility_old,schb_operation_new,
schb_operation_old,schb_route_new,schb_route_old,schb_product_new,schb_product_old,schb_owner_old,schb_intransit,
schb_date,schb_transaction,schb_quantity_1_new,schb_quantity_1_old,/*0,*/schb_if_count_new,schb_if_count_old,schb_preprocessed,
schb_consumed_quantity,/*'Y',*/schb_if_id,schb_if_id_old,fif_fac_loc_id
FROM scm_score_his_backups,full_item_facilities,location_params
WHERE schb_state = 0
AND fif_if_id = schb_if_id_old
AND lopo_param_value = to_char(fif_site_id_fm)
AND lopo_param_name IN ('SITE_FAB', 'SITE_SORT')
AND schb_date >= trunc(sysdate - 84)
AND (schb_loc_shortcut_source = 'MP' OR schb_loc_shortcut_source = 'RE' OR schb_loc_shortcut_source = 'VI')
Select schs_run_date,schs_loc_shortcut_source,schs_lot_number
From v_score_his_old
Where schs_loc_shortcut_source = 'MP';
Oracle version is Oracle9i Enterprise Edition Release 9.2.0.6.0
I am not able to get where I can going wrong.
Please help me to understand this issue,
Regards
Umesh..With v_score_his_old
As
SELECT
schs_run_date,schs_loc_shortcut_source,schs_lot_numbe
,schs_facility_new,schs_facility_old,
schs_operation_new,
chs_operation_old,schs_route_new,schs_route_old,schs_p
roduct_new,schs_product_old,schs_owner_old,schs_intran
sit,
chs_date,schs_transaction,schs_quantity_1_new,schs_qua
ntity_1_old,/*0,*/schs_if_count_new,schs_if_count_old,
chs_preprocessed,schs_consumed_quantity,/*'Y',*/schs_i
f_id,schs_if_id_old,fif_fac_loc_id
FROM
cm_score_his,full_item_facilities,location_params
WHERE schs_state = 0
AND fif_if_id = schs_if_id_old
AND lopo_param_value =
to_char(fif_site_id_fm)
AND lopo_param_name IN ('SITE_FAB',
'SITE_SORT')
AND (schs_loc_shortcut_source = 'MP' OR
schs_loc_shortcut_source = 'RE' OR
schs_loc_shortcut_source = 'VI')
UNION ALL
SELECT
schb_run_date,schb_loc_shortcut_source,schb_lot_number
,schb_facility_new,schb_facility_old,schb_operation_ne
w,
chb_operation_old,schb_route_new,schb_route_old,schb_p
roduct_new,schb_product_old,schb_owner_old,schb_intran
sit,
chb_date,schb_transaction,schb_quantity_1_new,schb_qua
ntity_1_old,/*0,*/schb_if_count_new,schb_if_count_old,
schb_preprocessed,
chb_consumed_quantity,/*'Y',*/schb_if_id,schb_if_id_ol
d,fif_fac_loc_id
FROM
cm_score_his_backups,full_item_facilities,location_par
ams
WHERE schb_state = 0
AND fif_if_id = schb_if_id_old
AND lopo_param_value =
to_char(fif_site_id_fm)
AND lopo_param_name IN ('SITE_FAB',
'SITE_SORT')
AND schb_date >= trunc(sysdate - 84)
AND (schb_loc_shortcut_source = 'MP' OR
schb_loc_shortcut_source = 'RE' OR
schb_loc_shortcut_source = 'VI')
ect
schs_run_date,schs_loc_shortcut_source,schs_lot_number
From v_score_his_old
Where schs_loc_shortcut_source = 'MP';
Hi!
I've a doubt. If u finally picking all the values which matched with field schs_loc_shortcut_source and which is 'MP' - then why r u using multiple condition in the WITH part? So, in that case, u don't have to write that additional filtering of the following lines in the outer query -
Where schs_loc_shortcut_source = 'MP';Regards.
Satyaki De. -
ORA-32036 unsupported case for inlining of query name in WITH clause
I have a query with a WITH clause. In the WITH clause I am using the TABLE command to pull multiple records from a PL/SQL function in the FROM clause:
WITH select_a AS
(select x.col1,
x.col2
from A,
TABLE(schema1.function1) x
where ...)
select ...
from ...;
The query is returning 32036 oracle error. Any thoughts on why I get this error? Is it valid to use the TABLE command with a function in the WITH clause?Is it valid to use the TABLE command with a function in the WITH clause?Something else must be going on: Even on my old 9i I can use WITH together with TABLE:
SQL> select * from v$version where rownum = 1
BANNER
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
1 row selected.
SQL> create or replace function f1 (deptno int) return sys.dbms_debug_vc2coll
as
begin
return sys.dbms_debug_vc2coll(deptno);
end;
Function created.
SQL> with t as (
select dept.* from dept, table (f1(deptno)) where column_value = deptno
select * from t
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
4 rows selected. -
db and dev 10g rel2 , hi all,
i am trying to learn the "WITH CLAUSE" , and i do not see any difference between using it and using sub-queries .
when i searched for this , i found that the with clause is used when You need to reference the subquery block multiple places in the query by specifying the query name , but i can not imagine an example for doing so .
if you could provide me with an example please ? and telling me about another situations in which i could need using the "with clause" if any ?
thanks>
db and dev 10g rel2 , hi all,
i am trying to learn the "WITH CLAUSE" , and i do not see any difference between using it and using sub-queries .
when i searched for this , i found that the with clause is used when You need to reference the subquery block multiple places in the query by specifying the query name , but i can not imagine an example for doing so .
if you could provide me with an example please ? and telling me about another situations in which i could need using the "with clause" if any ?
>
It isn't just about referencing a subquery multiple times. There are other advantages to using 'common table expressions'/'subquery factoring' also.
Take a look at the example below. It first defines 'dept_costs' as a query block, then defines 'avg_cost' as a new query block and it references the first query block.
Then the actual query references both query blocks just as if they are tables. And, in fact, in some circumstances Oracle will actually materialize them AS temporary tables.
Look at how easy it is to understand the entire statement. You can focus first on the 'dept_costs' query block WITHOUT having to look at anything else. That is because the query block is self-contained; you are defining a result set. There is no 'join' or connection to any other part of the statement.
It is easy for a developer, and for Oracle, to understand what is needed for that one piece.
Next you can focus entirely on the 'avg_cost' query block. Since it uses the first query block just as if it were a table you can treat it as a table. That means you do NOT even need to look at the first query block to understand what the second query block is doing.
Same with the actual query. You can analyze it by treating the two query blocks just as if they were other tables.
Even better you can test the first query block by itself in sql*plus or other tool to confirm that it works and you can create an execution plan for it to make sure it will use an appropriate index. You can also then test the first and second query blocks together to make sure THEY have a proper execution plan.
Then when you test then entire statement you already know that the query blocks work correctly.
Try to do THAT with a query that uses nested sub-queries.
Sure - you could write a set of nested sub-queries to accomplish the same thing (Oracle will sometimes rewrite your query that way itself) but it becomes one big query and the individual pieces are not nearly as easy to see, analyze or understand.
It can be difficult if not impossible to extract a nested query in order to test it even to just try to get the syntax working. And when you do extract it you will often be testing something that isn't quite exactly the same as when i t was nested.
So: easier to understand, easier to write and test (especially for new developers) as well as easier to use multiple times without having to duplicate it.
>
subquery_factoring_clause
The WITH query_name clause lets you assign a name to a subquery block. You can then reference the subquery block multiple places in the query by specifying the query name. Oracle Database optimizes the query by treating the query name as either an inline view or as a temporary table.
>
The SQL Language doc has an example.
http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_10002.htm#i2129904
>
Subquery Factoring: Example The following statement creates the query names dept_costs and avg_cost for the initial query block containing a join, and then uses the query names in the body of the main query.
WITH
dept_costs AS (
SELECT department_name, SUM(salary) dept_total
FROM employees e, departments d
WHERE e.department_id = d.department_id
GROUP BY department_name),
avg_cost AS (
SELECT SUM(dept_total)/COUNT(*) avg
FROM dept_costs)
SELECT * FROM dept_costs
WHERE dept_total >
(SELECT avg FROM avg_cost)
ORDER BY department_name;
DEPARTMENT_NAME DEPT_TOTAL
Sales 313800
Shipping 156400 -
Join and Multiple where clauses: which is better??
There was an argument between a colleague and I. He prefers building his views joining many tables with many where clause while I am cute with my join which is simpler and more understandable for me. So, it started an argument on which is a superior way of joining tables.
I will actually like to know we is faster and better: using JOINs or using multiple where clauses to retrieve values from multiple tables.?Dave:
"Are we talking about the difference between Oracle Joins and ANSI Joins?
e.g.
Select d.dept_name, e.emp_name
from dept d, emp e
where d.dept_id = e.dept_id"
That is not really an "Oracle Join". That syntax works on just about every SQL database I have ever worked with. The (+) syntax for an outer join is Oracle specific, but some other databases use a similar style for outer joins (something like a.c += b.c or a.c *= b.c).
OP:
There are things you can do with the ANSI style syntax that either cannot be done, or cannot be done as easily, with Oracle's syntax. Most notably, outer joining one table to more than one other tables, and FULL OUTER joins.
Having said that, in my experience, both are rather rare occurrences, so I tend to stick to doing the jopins in the WHERE clause because that is what I am used to.
John -
Difference between global temporary table and with clause
what is the difference between global temporary table and with claue .(with clause is used as table in select query)
what is the difference between global temporary table and with claue .(with clause is used as table in select query)Its big difference between the two.
Global temporary table exists for a session or a transaction while, with clause lives only for a query.
GTT is a named permanent storage table whose data flushes away on session exit or at end of a transaction while WITH clause just provides names to a reference data within a query (which is as good as having union subquery in FROM clause)
eg
SQL> with c as
2 (
3 select 1 id1, 2 id2 from dual union all
4 select 2,4 from dual union all
5 select 3,5 from dual)
6 select * from c
7 /
ID1 ID2
1 2
2 4
3 5
SQL> ed
Wrote file afiedt.buf
1 select *
2 from
3 (
4 select 1 id1, 2 id2 from dual union all
5 select 2,4 from dual union all
6* select 3,5 from dual)
7 /
ID1 ID2
1 2
2 4
3 5But GTT serves its purpose very well in case of session specific transaction and the scnearion with multiple users working on same data. -
Difference between WITH CLAUSE and INLINNE VIEW.
Hi experts.
Can anyone explain me the diff. between WITH CLAUSE and INLINNE VIEW with some example.
Thanks in advance.user10314274 wrote:
Hi experts.
Can anyone explain me the diff. between WITH CLAUSE and INLINNE VIEW with some example.Generally, they are the same thing with a few minor differences.
The WITH clause gives you a little more control on how the sub-queries are to be set up and used and allows the same subquery to be used multiple times without re-reading the data. Both simulate the functionality of views.
I feel the WITH clause provides better strucure and maintainablility.
The WITH clause aslo offesr two hints, INLINE and MATERIALIZE that can affect performance - both are used with the query in the IN clause. INLINE causes the subquery to be used as a subquery (inline view) while MATERIALIZE requests that the data be copied to a temporary table first. These hints are undocumented but are recognized by the user community.
Edited by: riedelme on Jan 20, 2010 6:32 AM -
My DB version is 10.2.0
One of my query takes long time for execution and here is the Old Query,
SELECT HD.DATUM DATUM, HA2.GELDEINGAENGE_INSG-NVL(HA3.VERWERTUNGSERLOESE,0)
GELDEINGANG, HA3.VERWERTUNGSERLOESE
FROM
( SELECT DISTINCT(TO_CHAR(ERFASSDATUM,'YYYY/MM')) DATUM
FROM TRANS_HIST H1 ,
(SELECT BUCHUNGSGRUPPE, LFDNR, SICHERHEITBEZUG
FROM BUCHUNGSSCHL
WHERE STORNOMM=0) B1
WHERE H1.ERFASSDATUM >=TO_DATE('01.10'||TO_CHAR(ADD_MONTHS(SYSDATE,-9),'YYYY')||' 00.00.00','DD.MM.YYYY HH24:MI:SS')
AND H1.BUCHUNGSGRUPPE = B1.BUCHUNGSGRUPPE AND H1.LFDNR = B1.LFDNR
AND (H1.BUCHUNGSGRUPPE = '8888' OR ( H1.BUCHUNGSGRUPPE > 5999 AND H1.BUCHUNGSGRUPPE < 7100 AND B1.SICHERHEITBEZUG = 1)) ) HD,
(SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM, SUM(BETRAG) GELDEINGAENGE_INSG
FROM TRANS_HIST H2,
(SELECT F.GLAEUBIGERNR,F.FORDNR,F.FORDERGNR,A.MAHN_NUM
FROM FRD_ACCT F,
(SELECT * FROM ANS_PRCH WHERE RANGMM=1) A
WHERE F.GLAEUBIGERNR=A.GLAEUBIGERNR (+) AND F.FORDNR=A.FORDNR(+)
AND F.FORDERGNR=A.FORDERGNR(+) AND NVL(F.INDIVIDUALFLAG,0)=0 ) F1
WHERE ( (F1.GLAEUBIGERNR= H2.GLAEUBIGERNR AND F1.FORDNR=H2.FORDNR AND F1.FORDERGNR=H2.FORDERGNR) OR
F1.MAHN_NUM = H2.MAHN_NUM) AND H2.BUCHUNGSGRUPPE = '8888' AND
H2.ERFASSDATUM >= TO_DATE('01.10'||TO_CHAR(ADD_MONTHS(SYSDATE,-9),'YYYY')||' 00.00.00','DD.MM.YYYY HH24:MI:SS')
GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM') ) HA2,
(SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM, SUM(BETRAG) VERWERTUNGSERLOESE
FROM TRANS_HIST H3, (SELECT BUCHUNGSGRUPPE, LFDNR,
SICHERHEITBEZUG, NACHMIETVERTRAGE
FROM BUCHUNGSSCHL
WHERE STORNOMM=0) B3,
(SELECT F.GLAEUBIGERNR,F.FORDNR,F.FORDERGNR,A.MAHN_NUM
FROM FRD_ACCT F,
(SELECT * FROM ANS_PRCH WHERE RANGMM=1) A
WHERE F.GLAEUBIGERNR= A.GLAEUBIGERNR (+) AND F.FORDNR=A.FORDNR(+)
AND F.FORDERGNR=A.FORDERGNR(+) AND NVL(F.INDIVIDUALFLAG,0)=0 ) F2
WHERE ( (F2.GLAEUBIGERNR=H3.GLAEUBIGERNR AND F2.FORDNR=H3.FORDNR AND F2.FORDERGNR=H3.FORDERGNR)
OR F2.MAHN_NUM = H3.MAHN_NUM)
AND H3.ERFASSDATUM >=TO_DATE('01.10'||TO_CHAR(ADD_MONTHS(SYSDATE,-9),'YYYY')||' 00.00.00','DD.MM.YYYY HH24:MI:SS')
AND H3.BUCHUNGSGRUPPE = B3.BUCHUNGSGRUPPE AND
H3.LFDNR = B3.LFDNR AND H3.BUCHUNGSGRUPPE > 5999
AND H3.BUCHUNGSGRUPPE < 7100 AND (B3.SICHERHEITBEZUG = 1 OR B3.NACHMIETVERTRAGE =1)
GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM') ) HA3
WHERE HD.DATUM=HA2.DATUM (+)
AND HD.DATUM=HA3.DATUM (+)
ORDER BY DATUM ASC
call count cpu elapsed disk query current rows
Parse 1 0.22 0.22 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 1 469.61 1448.30 2874498 3017355 1 9
total 3 469.83 1448.53 2874498 3017355 1 9
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 62 (recursive depth: 1)
Rows Row Source Operation
9 SORT ORDER BY (cr=3017355 pr=2874498 pw=0 time=1448309418 us)
9 HASH JOIN RIGHT OUTER (cr=3017355 pr=2874498 pw=0 time=1448309191 us)
9 VIEW (cr=1228085 pr=1175604 pw=0 time=906871801 us)
9 HASH GROUP BY (cr=1228085 pr=1175604 pw=0 time=906871785 us)
1559 CONCATENATION (cr=1228085 pr=1175604 pw=0 time=564453620 us)
233 HASH JOIN (cr=614043 pr=589377 pw=0 time=562088377 us)
94 TABLE ACCESS FULL BUCHUNGSSCHL (cr=29 pr=0 pw=0 time=505 us)
254136 HASH JOIN (cr=614014 pr=589377 pw=0 time=509476999 us)
497464 TABLE ACCESS FULL TRANS_HIST (cr=586339 pr=562603 pw=0 time=65783878 us)
737515 HASH JOIN RIGHT OUTER (cr=27675 pr=26774 pw=0 time=18577731 us)
372656 TABLE ACCESS FULL ANS_PRCH (cr=6346 pr=5910 pw=0 time=408778 us)
737515 TABLE ACCESS FULL FRD_ACCT (cr=21329 pr=20864 pw=0 time=2254657 us)
1326 HASH JOIN (cr=614042 pr=586227 pw=0 time=520726941 us)
94 TABLE ACCESS FULL BUCHUNGSSCHL (cr=29 pr=0 pw=0 time=641 us)
221360 FILTER (cr=614013 pr=586227 pw=0 time=372791872 us)
221360 HASH JOIN OUTER (cr=614013 pr=586227 pw=0 time=372570499 us)
221360 HASH JOIN (cr=607668 pr=580286 pw=0 time=368077766 us)
243053 TABLE ACCESS FULL TRANS_HIST (cr=586339 pr=563978 pw=0 time=1425434011 us)
737515 TABLE ACCESS FULL FRD_ACCT (cr=21329 pr=16308 pw=0 time=8859226 us)
372656 TABLE ACCESS FULL ANS_PRCH (cr=6345 pr=5941 pw=0 time=1872464 us)
9 HASH JOIN OUTER (cr=1789270 pr=1698894 pw=0 time=541436637 us)
9 VIEW (cr=586667 pr=562439 pw=0 time=213337882 us)
9 HASH UNIQUE (cr=586667 pr=562439 pw=0 time=213337873 us)
748717 HASH JOIN (cr=586667 pr=562439 pw=0 time=104432018 us)
830 TABLE ACCESS FULL BUCHUNGSSCHL (cr=29 pr=0 pw=0 time=1042 us)
1241936 TABLE ACCESS FULL TRANS_HIST (cr=586638 pr=562439 pw=0 time=339666777 us)
9 VIEW (cr=1202603 pr=1136455 pw=0 time=328097826 us)
9 HASH GROUP BY (cr=1202603 pr=1136455 pw=0 time=328097809 us)
695373 CONCATENATION (cr=1202603 pr=1136455 pw=0 time=324453471 us)
0 HASH JOIN (cr=587003 pr=557994 pw=0 time=167168982 us)
744472 TABLE ACCESS FULL TRANS_HIST (cr=587003 pr=557994 pw=0 time=30622271 us)
0 HASH JOIN RIGHT OUTER (cr=0 pr=0 pw=0 time=0 us)
0 TABLE ACCESS FULL ANS_PRCH (cr=0 pr=0 pw=0 time=0 us)
0 TABLE ACCESS FULL FRD_ACCT (cr=0 pr=0 pw=0 time=0 us)
695373 FILTER (cr=615600 pr=578461 pw=0 time=157284464 us)
695373 HASH JOIN OUTER (cr=615600 pr=578461 pw=0 time=156589072 us)
695373 HASH JOIN (cr=609255 pr=572518 pw=0 time=150571393 us)
744472 TABLE ACCESS FULL TRANS_HIST (cr=587926 pr=556115 pw=0 time=31820718 us)
737515 TABLE ACCESS FULL FRD_ACCT (cr=21329 pr=16403 pw=0 time=3696334 us)
372656 TABLE ACCESS FULL ANS_PRCH (cr=6345 pr=5943 pw=0 time=391928 us)I tried fine tuning the query using "With Clause" but landed up with oracle error, which prevents me from using with clause inside a paranthesis.
SELECT HD.DATUM DATUM, HA2.GELDEINGAENGE_INSG-NVL(HA3.VERWERTUNGSERLOESE,0)
GELDEINGANG, HA3.VERWERTUNGSERLOESE
FROM
( WITH HIST_VIEW AS (SELECT GLAEUBIGERNR,FORDNR,FORDERGNR,MAHN_NUM,BUCHUNGSGRUPPE,LFDNR,STORNOMM,ERFASSDATUM,BETRAG
FROM TRANS_HIST H
WHERE ERFASSDATUM >=TO_DATE('01.10'||TO_CHAR(ADD_MONTHS(SYSDATE,-9),'YYYY')||' 00.00.00','DD.MM.YYYY HH24:MI:SS')
AND (H.BUCHUNGSGRUPPE = '8888' OR ( H.BUCHUNGSGRUPPE > 5999 AND H.BUCHUNGSGRUPPE < 7100))),
FORD_VIEW AS (SELECT F.GLAEUBIGERNR,F.FORDNR,F.FORDERGNR,A.MAHN_NUM
FROM FRD_ACCT F,ANS_PRCH A
WHERE F.GLAEUBIGERNR=A.GLAEUBIGERNR(+) AND F.FORDNR=A.FORDNR(+)
AND F.FORDERGNR=A.FORDERGNR(+) AND NVL(F.INDIVIDUALFLAG,0)=0 AND A.RANGMM=1)
(SELECT DISTINCT(TO_CHAR(ERFASSDATUM,'YYYY/MM')) DATUM
FROM HIST_VIEW H1 ,BUCHUNGSSCHL B1
WHERE B1.STORNOMM=0 AND H1.BUCHUNGSGRUPPE = B1.BUCHUNGSGRUPPE AND H1.LFDNR = B1.LFDNR
AND (H1.BUCHUNGSGRUPPE = '8888' OR (H1.BUCHUNGSGRUPPE > 5999 AND H1.BUCHUNGSGRUPPE < 7100 AND B1.SICHERHEITBEZUG = 1))) HD,
(SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM, SUM(BETRAG) GELDEINGAENGE_INSG
FROM HIST_VIEW H2,FORD_VIEW F1
WHERE ((F1.GLAEUBIGERNR= H2.GLAEUBIGERNR AND F1.FORDNR=H2.FORDNR AND F1.FORDERGNR=H2.FORDERGNR) OR
F1.MAHN_NUM = H2.MAHN_NUM) AND H2.BUCHUNGSGRUPPE = '8888'
GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM') ) HA2,
(SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM, SUM(BETRAG) VERWERTUNGSERLOESE
FROM HIST_VIEW H3, BUCHUNGSSCHL B3,FORD_VIEW F2
WHERE ((F2.GLAEUBIGERNR=H3.GLAEUBIGERNR AND F2.FORDNR=H3.FORDNR
AND F2.FORDERGNR=H3.FORDERGNR) OR F2.MAHN_NUM = H3.MAHN_NUM) AND B3.STORNOMM=0
AND H3.BUCHUNGSGRUPPE = B3.BUCHUNGSGRUPPE AND
H3.LFDNR = B3.LFDNR AND H3.BUCHUNGSGRUPPE > 5999
AND H3.BUCHUNGSGRUPPE < 7100 AND (B3.SICHERHEITBEZUG = 1 OR B3.NACHMIETVERTRAGE =1)
GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM')) HA3
WHERE HD.DATUM=HA2.DATUM (+)
AND HD.DATUM=HA3.DATUM (+)
ORDER BY DATUM ASC )
ORA-00907: missing right parenthesisIf you format your code it makes it far easier to spot mistakes...
WITH HIST_VIEW AS (SELECT GLAEUBIGERNR,FORDNR,FORDERGNR,MAHN_NUM,BUCHUNGSGRUPPE,LFDNR,STORNOMM,ERFASSDATUM,BETRAG
FROM TRANS_HIST H
WHERE ERFASSDATUM >=TO_DATE('01.10'||TO_CHAR(ADD_MONTHS(SYSDATE,-9),'YYYY')||' 00.00.00','DD.MM.YYYY HH24:MI:SS')
AND ( H.BUCHUNGSGRUPPE = '8888'
OR (H.BUCHUNGSGRUPPE > 5999 AND H.BUCHUNGSGRUPPE < 7100)
FORD_VIEW AS (SELECT F.GLAEUBIGERNR,F.FORDNR,F.FORDERGNR,A.MAHN_NUM
FROM FRD_ACCT F,ANS_PRCH A
WHERE F.GLAEUBIGERNR=A.GLAEUBIGERNR(+)
AND F.FORDNR=A.FORDNR(+)
AND F.FORDERGNR=A.FORDERGNR(+)
AND NVL(F.INDIVIDUALFLAG,0)=0
AND A.RANGMM=1
SELECT HD.DATUM DATUM
,HA2.GELDEINGAENGE_INSG-NVL(HA3.VERWERTUNGSERLOESE,0) GELDEINGANG
,HA3.VERWERTUNGSERLOESE
FROM (SELECT DISTINCT(TO_CHAR(ERFASSDATUM,'YYYY/MM')) DATUM
FROM HIST_VIEW H1
,BUCHUNGSSCHL B1
WHERE B1.STORNOMM=0
AND H1.BUCHUNGSGRUPPE = B1.BUCHUNGSGRUPPE
AND H1.LFDNR = B1.LFDNR
AND ( H1.BUCHUNGSGRUPPE = '8888'
OR (H1.BUCHUNGSGRUPPE > 5999 AND H1.BUCHUNGSGRUPPE < 7100 AND B1.SICHERHEITBEZUG = 1)
) HD,
(SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM
,SUM(BETRAG) GELDEINGAENGE_INSG
FROM HIST_VIEW H2
,FORD_VIEW F1
WHERE (
( F1.GLAEUBIGERNR= H2.GLAEUBIGERNR
AND F1.FORDNR=H2.FORDNR
AND F1.FORDERGNR=H2.FORDERGNR
OR
F1.MAHN_NUM = H2.MAHN_NUM
AND H2.BUCHUNGSGRUPPE = '8888'
GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM')
) HA2,
(SELECT TO_CHAR(ERFASSDATUM,'YYYY/MM') DATUM
,SUM(BETRAG) VERWERTUNGSERLOESE
FROM HIST_VIEW H3
,BUCHUNGSSCHL B3
,FORD_VIEW F2
WHERE (
( F2.GLAEUBIGERNR=H3.GLAEUBIGERNR
AND F2.FORDNR=H3.FORDNR
AND F2.FORDERGNR=H3.FORDERGNR
OR
F2.MAHN_NUM = H3.MAHN_NUM
AND B3.STORNOMM=0
AND H3.BUCHUNGSGRUPPE = B3.BUCHUNGSGRUPPE
AND H3.LFDNR = B3.LFDNR
AND H3.BUCHUNGSGRUPPE > 5999
AND H3.BUCHUNGSGRUPPE < 7100
AND (B3.SICHERHEITBEZUG = 1 OR B3.NACHMIETVERTRAGE =1)
GROUP BY TO_CHAR(ERFASSDATUM,'YYYY/MM')
) HA3
WHERE HD.DATUM=HA2.DATUM (+)
AND HD.DATUM=HA3.DATUM (+)
ORDER BY DATUM ASC
) <--- What's this doing on the end?Remove that last bracket and you should be ok.
I took the liberty of putting the subquery factoring at the beginning, so see if that works. -
OPEN CURSOR using a WITH clause in the select query
Hi,
I am using Oracle 9i. I have a requirement where I have a REFCURSOR as an OUT parameter for my procedure. I have declared the TYPE and created the procedure.
In the procedure, I am using OPEN <cursor_name> FOR <query>;
Ideally this works in most of the cases that I have tried earlier. However, in the current case I am using a WITH clause in my query to get the results.
I need help in understanding if the above mentioned syntax would not allow me to use the WITH clause in the query.What error do you get , seems to work ok for me on 10g
SQL> begin
2 open :cv for 'with x as (select * from emp) select * from x';
3 end;
4 /
PL/SQL procedure successfully completed.
SQL> print :cv
EMPNO
ENAME
JOB MGR HIREDATE SAL COMM DEPTNO
7521
WARD
SALESMAN 7698 22-FEB-81 1250 500 30
7566
JONES
MANAGER 7839 02-APR-81 2975 20
EMPNO -
Need help with INSERT and WITH clause
I wrote sql statement which correctly work, but how i use this statment with INSERT query? NEED HELP. when i wrote insert i see error "ORA 32034: unsupported use of with clause"
with t1 as(
select a.budat,a.monat as period,b.vtweg,
c.gjahr,c.buzei,c.shkzg,c.hkont, c.prctr,
c.wrbtr,
c.matnr,
c.menge,
a.monat,
c.zuonr
from ldw_v1.BKPF a,ldw_v1.vbrk b, ldw_v1.bseg c
where a.AWTYP='VBRK' and a.BLART='RV' and a.BUKRS='8431' and a.awkey=b.vbeln
and a.bukrs=c.bukrs and a.belnr=c.belnr and a.gjahr=c.gjahr and c.koart='D'
and c.ktosl is null and c.gsber='4466' and a.gjahr>='2011' and b.vtweg='01'
,t2 as(
select a.BUKRS,a.BELNR, a.GJAHR,t1.vtweg,t1.budat,t1.monat from t1, ldw_v1.bkpf a
where t1.zuonr=a.xblnr and a.blart='WL' and bukrs='8431'
,tcogs as (
select t2.budat,t2.monat,t2.vtweg, bseg.gjahr,bseg.hkont,bseg.prctr,
sum(bseg.wrbtr) as COGS,bseg.matnr,bseg.kunnr,sum(bseg.menge) as QUANTITY
from t2, ldw_v1.bseg
where t2.bukrs=bseg.bukrs and t2.belnr=bseg.BELNR and t2.gjahr=bseg.gjahr and BSEG.KOART='S'
group by t2.budat,t2.monat,t2.vtweg, bseg.gjahr,bseg.hkont,bseg.prctr,
bseg.matnr,bseg.kunnr
,t3 as
select a.budat,a.monat,b.vtweg,
c.gjahr,c.buzei,c.shkzg,c.hkont, c.prctr,
case when c.shkzg='S' then c.wrbtr*(-1)
else c.wrbtr end as NTS,
c.matnr,c.kunnr,
c.menge*(-1) as Quantity
from ldw_v1.BKPF a,ldw_v1.vbrk b, ldw_v1.bseg c
where a.AWTYP='VBRK' and a.BLART='RV' and a.BUKRS='8431' and a.awkey=b.vbeln
and a.bukrs=c.bukrs and a.belnr=c.belnr and a.gjahr=c.gjahr and c.koart='S'
and c.ktosl is null and c.gsber='4466' and a.gjahr>='2011' and b.vtweg='01'
,trevenue as (
select t3.budat,t3.monat,t3.vtweg, t3.gjahr,t3.hkont,t3.prctr,
sum(t3.NTS) as NTS,t3.matnr,t3.kunnr,sum(t3.QUANTITY) as QUANTITY
from t3
group by t3.budat,t3.monat,t3.vtweg, t3.gjahr,t3.hkont,t3.prctr,t3.matnr,t3.kunnr
select NVL(tr.budat,tc.budat) as budat,
NVL(tr.monat,tc.monat) as monat,
NVL(tr.vtweg,tc.vtweg) as vtweg,
NVL(tr.gjahr, tc.gjahr) as gjahr,
tr.hkont as NTS_hkont,
tc.hkont as COGS_hkont,
NVL(tr.prctr,tc.prctr) as prctr,
NVL(tr.MATNR, tc.MATNR) as matnr,
NVL(tr.kunnr, tc.kunnr) as kunnr,
NVL(tr.Quantity, tc.Quantity) as Quantity,
tr.NTS as NTS,
tc.COGS as COGS
from trevenue TR full outer join tcogs TC
on TR.BUDAT=TC.BUDAT and TR.MONAT=TC.MONAT and TR.GJAHR=TC.GJAHR
and TR.MATNR=TC.MATNR and TR.KUNNR=TC.KUNNR and TR.QUANTITY=TC.QUANTITY
and TR.VTWEG=TC.VTWEG and TR.PRCTR=TC.PRCTR
Edited by: user13566113 on 25.03.2011 5:26Without seeing what you tried it is hard to say what you did wrong, but this is how it would work
SQL> create table t ( n number );
Table created.
SQL> insert into t
2 with test_data as
3 (select 1 x from dual union all
4 select 2 x from dual union all
5 select 3 x from dual union all
6 select 4 x from dual)
7 select x from test_data;
4 rows created.
SQL> -
i am writing these SQL Lines in my stored procedure i am using with clause i am getting error when i perform
inner join
With WardItemsDtl as ( SELECT WardItemDtl_ID,
WardTransID,
wdDtl.ItemID,
SUM(ItemQty) ItemQty,
ReceivedWardID as WardID
FROM LL_WardItems_Details wdDtl
WHERE isposted = 0
GROUP BY wdDtl.ItemID,WardItemDtl_ID,ReceivedWardID,WardTransID
ORDER BY WardItemDtl_ID )
, PartyItemsDtl AS ( SELECT WardItemDtl_ID,
ItemID,
SUM(IssueItemQty) IssueItemQty
FROM LL_PartyItems_Details
GROUP BY WardItemDtl_ID,ItemID
ORDER BY WardItemDtl_ID )
SELECT wdDt.WardTransID,
wdDt.ItemID,
itmt.ItemCode,
itmt.ItemDescription,
wdDt.ItemQty as TotalItemQty,
NVL(pDtl.IssueItemQty, 0) as IssueQtyToParty,
wdDt.WardID,
wdmt.Ward_Name
from WardItemsDtl wdDt,PartyItemsDtl pDtl
inner join LLItemMst itmt
on itmt.ItemID=wdDt.ItemID -----error in this line
inner join ward_mst wdmt -- error in this line
on wdmt.Ward_ID=wdDt.WardID
error
Error report:
SQL Error: ORA-00904: "WDDT"."ITEMID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:WITH WARDITEMSDTL AS
(SELECT WARDITEMDTL_ID,
WARDTRANSID,
WDDTL.ITEMID,
SUM(ITEMQTY) ITEMQTY,
RECEIVEDWARDID AS WARDID
FROM LL_WARDITEMS_DETAILS WDDTL
WHERE ISPOSTED = 0
GROUP BY WDDTL.ITEMID,
WARDITEMDTL_ID,
RECEIVEDWARDID,
WARDTRANSID
ORDER BY WARDITEMDTL_ID
PARTYITEMSDTL AS
(SELECT WARDITEMDTL_ID,
ITEMID,
SUM(ISSUEITEMQTY) ISSUEITEMQTY
FROM LL_PARTYITEMS_DETAILS
GROUP BY WARDITEMDTL_ID,
ITEMID
ORDER BY WARDITEMDTL_ID
SELECT WDDT.WARDTRANSID,
WDDT.ITEMID,
ITMT.ITEMCODE,
ITMT.ITEMDESCRIPTION,
WDDT.ITEMQTY AS TOTALITEMQTY,
NVL(PDTL.ISSUEITEMQTY, 0) AS ISSUEQTYTOPARTY,
WDDT.WARDID,
WDMT.WARD_NAME
FROM WARDITEMSDTL WDDT,
PARTYITEMSDTL PDTL,
LLITEMMST ITMT,
WARD_MST WDMT
WHERE ITMT.ITEMID=WDDT.ITEMID
AND WDMT.WARD_ID=WDDT.WARDID ;AND WHERE YOUR JOIN PARTYITEMSDTL PDTL -
Unable to use the with clause in oracle 9.0.2
Hi,
I need to use oracle SQL with clause in oracle 9.0.2 database. Its a 9i feature but i am unable to use it.
It is giving internal error, when i try to execute it.
Even for simple query:
WITH acct_summary as ( select TOT_COLL_AMT from tdc_acct)
select TOT_COLL_AMT from acct_summary WHERE TOT_COLL_AMT>100;
Error message while using 8.0.5 sql plus client:
SP2-0642: SQL*Plus internal error state 2091, context 0:0:0
Unsafe to proceed
Please help to find out why i am not able to use the sql with clause in oracle 9.0.2 database.
Thanks and regards,
RaajkathirHi Jens Petersen,
Yes, You are correct. Thank you very much.
Regards,
Raja
Maybe you are looking for
-
Not able to set Weblogic Runtime on eclipse while adding new weblogic srvr
Hi Everyone, I downloaded Oracle Integration pack for eclipse by clicking at "Download additional server adapters". I am not able to set Weblogic Runtime on eclipse while adding new weblogic server. I am trying to give following values: Name :Oracle
-
IPod Touch connection with Vista
My iPod touch is not recognized by Windows Vista. On the same computer the iPod video works well. I have the newest version of iTunes 4.7, which should have the driver included, but Vista keeps looking for the driver. Any suggestions?
-
I have a MacBook Pro model 7.1 running 10.6.8; need to upgrade to Mavericks...question concerns 3 software apps: Reunion 10, Quicken 2007, Appleworks 6.2.9 (word processing docs). Any recommendations?
-
Plays only 20 seconds, then freezes
I have the U2 edition iPod and on Friday I added new songs (roughly 150?). After I did that my brother borrowed it and used his own headphones. Since yesterday, when I play songs, it only plays 20 seconds and then stops, moves to the next song and it
-
GE70 2PE Buzzlike noise from Fan/CPU area
Greetings fellow dragon army. My newly bought(been 3 weeks or so) has started to create excessive buzzlike noise from the fan as if theres a little string hitting the fan while its working especially when turbo boost is on making it very very annoyin