With clause in oracle
how is it possible
suppose i have a query like select name ,count(),date
where <condition>
group by name,date
it return summary information like
a,2,11-nov-2007
b,4,11-nov-2007
that means it store summary info
i want to store detail info into other table
select name ,count(),date
where <same condition of above>
like a,xyz,11-nov-2007
a,abc,11-nov-2007
b,sts,11-nov-2007
b,pqs,11-nov-2007
b,stt,11-nov-2007
b,bts,11-nov-2007
so can i use WITH clause AS here.<basically i want to store summay info to a table and detail info to other table>
please help me .
After reading your post four times I still don't know what are you talking about.
select name
,count(),date
where <condition>
group by name,dateWhere is from clause?
select name ,count(),date
where <same condition of above>
like a,xyz,11-nov-2007
a,abc,11-nov-2007
b,sts,11-nov-2007
b,pqs,11-nov-2007
b,stt,11-nov-2007
b,bts,11-nov-2007Why count() returns something like abc, sts, and again - where is from clause?
Similar Messages
-
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 -
What is equivalent to 'with clause' in oracle
what is equivalent to 'with clause' in oracle
thanksMy reqirement is :
I have a table named egroup. I have to find the name of the egroup different from "other" that can be found in the most countries in Africa
create table EGROUP(
c_id char(2), -- country id
egroup varchar2(40), -- name of the ethnic group
percent number(4,2), -- percentage of population
constraint egroup_fk foreign key(c_id) references COUNTRY,
constraint egroup_pk primary key(c_id,egroup)
insert into egroup values('AF','Pashtun', 42 );
insert into egroup values('AF','Tajik',27 );
insert into egroup values('AF','Hazara',9 );
insert into egroup values('AF','Uzbek',9 );
insert into egroup values('AF','Aimak',4 );
insert into egroup values('AF','Turkmen',3 );
insert into egroup values('AF','Baloch',2 );
insert into egroup values('AF','other',4 );
insert into egroup values('AL','Albanian', 95 );
insert into egroup values('AL','Greek',3 );
insert into egroup values('AL','other',2 );
insert into egroup values('AG','Arab-Berber', 99 );
insert into egroup values('AG','European',1 );
insert into egroup values('AQ','Samoan (Polynesian)', 89 );
insert into egroup values('AQ','Caucasian',2 ); -
WITH Clause query doesn't work in Oracle Reports.
Hi Gurus,
I'm using a WITH clause query and need to build a report using the same query.
But when i'm trying to build a report, query is giving error as "WITH clause table or view doesn't exists".
But the same query perfectly works in sql prompt.
Oracle Reports doesn't supports WITH clause query?
Please suggest.
Thanks,
OnkarI ran into a similar problem before and worked around it by moving the query to a pipelined function in the database as described at WITH clause unexpectedly causes ORA-00942 in Reports Builder
Hope this helps. -
Bug in WITH clause (subquery factoring clause) in Oracle 11?
I'm using WITH to perform a set comparison in order to qualify a given query as correct or incorrect regarding an existing solution. However, the query does not give the expected result - an empty set - when comparing the solution to itself in Oracle 11 whereas it does in Oracle 10. A minimal example os posted below as script. There are also some observations about changes to the tables or the query that make Oracle 11 returning correct results but in my opinion these changes must not change the semantics of the queries.
Is this a bug or am I getting something wrong? The Oracle versions are mentioned in the script.
-- Bug in WITH clause (subquery factoring clause)
-- in Oracle Database 11g Enterprise Edition 11.2.0.1.0?
DROP TABLE B PURGE;
DROP TABLE K PURGE;
DROP TABLE S PURGE;
CREATE TABLE S (
m number NOT NULL,
x varchar2(30) NOT NULL
CREATE TABLE K (
k char(2) NOT NULL,
x varchar2(50) NOT NULL
CREATE TABLE B (
m number NOT NULL ,
k char(2) NOT NULL ,
n number
INSERT INTO S VALUES(1, 'h');
INSERT INTO S VALUES(2, 'l');
INSERT INTO S VALUES(3, 'm');
INSERT INTO K VALUES('k1', 'd');
INSERT INTO K VALUES('k2', 'i');
INSERT INTO K VALUES('k3', 'm');
INSERT INTO K VALUES('k4', 't');
INSERT INTO K VALUES('k5', 't');
INSERT INTO K VALUES('k6', 's');
INSERT INTO B VALUES(1, 'k1', 40);
INSERT INTO B VALUES(1, 'k2', 30);
INSERT INTO B VALUES(1, 'k4', 50);
INSERT INTO B VALUES(3, 'k1', 10);
INSERT INTO B VALUES(3, 'k2', 20);
INSERT INTO B VALUES(3, 'k1', 30);
INSERT INTO B VALUES(3, 'k6', 90);
COMMIT;
ALTER TABLE S ADD CONSTRAINT S_pk PRIMARY KEY (m);
ALTER TABLE K ADD CONSTRAINT K_pk PRIMARY KEY (k);
ALTER TABLE B ADD CONSTRAINT B_S_fk
FOREIGN KEY (m) REFERENCES S(m) ON DELETE CASCADE;
CREATE OR REPLACE VIEW v AS
SELECT S.m, B.n
FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
WHERE K.x='d'
ORDER BY B.n DESC;
-- Query 1: Result should be 0
WITH q AS
SELECT S.m, B.n
FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
WHERE K.x='d'
ORDER BY B.n DESC
SELECT COUNT(*)
FROM
SELECT * FROM q
MINUS
SELECT * FROM v
UNION ALL
SELECT * FROM v
MINUS
SELECT * FROM q
-- COUNT(*)
-- 6
-- 1 rows selected
-- Query 2: Result set should be empty (Query 1 without counting)
WITH q AS
SELECT S.m, B.n
FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
WHERE K.x='d'
ORDER BY B.n DESC
SELECT *
FROM
SELECT * FROM q
MINUS
SELECT * FROM v
UNION ALL
SELECT * FROM v
MINUS
SELECT * FROM q
-- M N
-- null 10
-- null 30
-- null 40
-- 1 40
-- 3 10
-- 3 30
-- 6 rows selected
-- Observations:
-- Incorrect results in Oracle Database 11g Enterprise Edition 11.2.0.1.0:
-- Query 1 returns 6, Query 2 returns six rows.
-- Correct in Oracle Database 10g Enterprise Edition 10.2.0.1.0.
-- Correct without the foreign key.
-- Correct if attribute x is renamed in S or K.
-- Correct if attribute x is left out in S.
-- Correct without the ORDER BY clause in the definition of q.
-- Only two results if the primary key on K is left out.
-- Correct without any change if not using WITH but subqueries (see below).
-- Fixed queries
-- Query 1b: Result should be 0
SELECT COUNT(*)
FROM
SELECT * FROM
SELECT S.m, B.n
FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
WHERE K.x='d'
ORDER BY B.n DESC
MINUS
SELECT * FROM v
UNION ALL
SELECT * FROM v
MINUS
SELECT * FROM
SELECT S.m, B.n
FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
WHERE K.x='d'
ORDER BY B.n DESC
-- COUNT(*)
-- 0
-- 1 rows selected
-- Query 2b: Result set shoud be empty (Query 1b without counting)
SELECT *
FROM
SELECT * FROM
SELECT S.m, B.n
FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
WHERE K.x='d'
ORDER BY B.n DESC
MINUS
SELECT * FROM v
UNION ALL
SELECT * FROM v
MINUS
SELECT * FROM
SELECT S.m, B.n
FROM S JOIN B ON S.m=B.m JOIN K ON B.k=K.k
WHERE K.x='d'
ORDER BY B.n DESC
-- M N
-- 0 rows selectedYou're all gonna love this one.....
The WITH clause works. But not easily.
Go ahead, build the query, (as noted in a recent thread, I, too, always use views), set the grants and make sure DISCOVERER and EULOWNER have SELECT privs.
1. Log into Disco Admin as EULOWNER. Trust me.
2. Add the view as a folder to the business area.
3. Log into Disco Desktop as EULOWNER. Don't laugh. It gets better.
4. Build the workbook and the worksheet (or just the worksheet if apropos)
5. Set the appropriate "sharing" roles and such
6. Save the workbook to the database.
7. Save the workbook to your computer.
8. Log out of Desktop.
9. Log back into Desktop as whatever, whoever you usually are to work.
10. elect "open existing workbook"
11. Select icon for "open from my computer". See? I told you it would get better!
12. Open the save .dis file from your computer.
13. Save it to the database.
14. Open a web browser and from there, you're on your own.
Fortran in VMS. Much easier and faster. I'm convinced the proliferation of the web is a detriment to the world at large...On the other hand, I'm also waiting for the Dodgers to return to Brooklyn. -
WITH clause equivalent in oracle 8i
Hi friends,
The "WITH" clause is very well supported in Oracle 9i. Is there any equivalent ways to use it in Oracle 8i?
Regards
J.BDear J. B.!
No, there isn't an equivalent for WITH in Oracle 8i. The only workaround for you is to build inline views in the FROM-Clause of your statements. Please have a look at the following link. It shows some example using a WITH-Clause and the equivalent SQL-Statements with an inline view.
[http://www.oracle-base.com/articles/misc/WithClause.php]
Yours sincerely
Florian W. -
Oracle SQL WITH clause support
It doesn't appear that JDeveloper 10.1.2 ADF BCs support using the WITH clause in an expert mode View. It says it's valid when tested, but the wizard loses all attribute references. Looking in the <view_object>.xml file, all attribute metdata has been removed from the file.
I can work around it easily by just repeating the sub-query. I was just wondering if it was not supported yet by the wizard, or is it a bug that I should file?
Thanks
ErikSince it isn't extremely important in this case, I'll just assume that ADF doesn't currently support SQL99 features in case anyone else ever asksthe same question.
Thanks
Erik -
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 -
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. -
Hi,
I am using Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 .
I am working in a procedure where I want to manipulate the data produced by a WITH AS clause. But it is giving "PL/SQL: ORA-00942: table or view does not exist" error. A small example to illustrate my problem is given below. Here in the example am just trying to get a replaced string by using the out put of a WITH AS clause.
SET SERVEROUTPUT ON;
DECLARE
CNT INTEGER;
LETTER CHAR(1);
REPLACED_STRING VARCHAR2(10);
BEGIN
REPLACED_STRING := 'ABC';
WITH T AS
(SELECT 'ABC' VAL FROM DUAL),
TMP (LEN, SUBVAL) AS
(SELECT LENGTH(VAL) LEN,
SUBSTR(VAL, 1, INSTR(VAL, 'B', 1, 1)) SUBVAL
FROM T
UNION ALL
SELECT LENGTH(VAL)-1 LEN,
SUBSTR(VAL, 2, INSTR(VAL, 'C', 1, 1)) SUBVAL
FROM T
SELECT COUNT(*) INTO CNT FROM TMP,T ;
FOR I IN 1..CNT LOOP
SELECT SUBSTR(SUBVAL,1,1) INTO LETTER FROM TMP,T;
SELECT REPLACE(REPLACED_STRING,LETTER,'X')INTO REPLACED_STRING FROM DUAL;
DBMS_OUTPUT.PUT_LINE(REPLACED_STRING);
END LOOP;
END;
I thought of declaring a cursor which will hold the data produced by WITH clause but it did not work. Can you please let me know what are the possible options to do this.
ThanksI think, I can not use the WITH inside loop as, I want to manipulate on data resulted by WITH reading row by row from it. I have a complex procedure and I wont be able to explain that scenario here and thus i came up with a similar problem through a very simple example. I will try to explain my problem thru my example here.
The WITH in my example give an out put as :
SUBVAL
AB
BC
WITH T AS
(SELECT 'ABC' VAL FROM DUAL),
TMP (LEN, SUBVAL) AS
(SELECT LENGTH(VAL) LEN,
SUBSTR(VAL, 1, INSTR(VAL, 'B', 1, 1)) SUBVAL
FROM T
UNION ALL
SELECT LENGTH(VAL)-1 LEN,
SUBSTR(VAL, 2, INSTR(VAL, 'C', 1, 1)) SUBVAL
FROM T
SELECT subval FROM TMP,T;
and then by using this in the PLSQL block mentioned above
FOR I IN 1..CNT LOOP
SELECT SUBSTR(SUBVAL,1,1) INTO LETTER FROM TMP,T;
SELECT REPLACE(REPLACED_STRING,LETTER,'X')INTO REPLACED_STRING FROM DUAL;
DBMS_OUTPUT.PUT_LINE(REPLACED_STRING);
END LOOP;
I want to have an output like:
XBC
XXC
Please note that nature of my original problem is that I want to manipulate the data given by WITH clause in the same procedure by passing it to other functions/procs to get the desired result. So please advice me following this approach only which would be helpful.
Thanks -
With clause within the cursor ???
Hello gurus,
Can we have a with clause within the cursor ???
something like this ..
cursor test_cur is
WITH unpivoted_data AS
SELECT DISTINCT
f.cust_id
f.office_cd
f.type_cd
FROM refoff ro,
cutomer p,
office f
WHERE f.cust_id = p.cust_id
AND f.office_cd = ro.office_cd
AND f.type_cd IN ('ACCT', 'pay')
SELECT cust_id
, MAX (CASE WHEN type_cd = 'ACCT' THEN office_id END) AS acct_office_id
, MAX (CASE WHEN type_cd = 'pay' THEN office_id END) AS pay_office_id
FROM unpivoted_data
GROUP BY cust_id
test_cur_rec test_cur% rowtype;
Begin
for test_cur_rec in test_cur
loop
insert into test1 (.....) values (.......);
end loop;
end;
One more quick question .... How do u guys past in the posting ??? Like somesought of a box .....I have seen lot senior gurus when they are replying, they post the code in some sought of a box ....that makes very easy to understand and take look at the code in the posting....???
Thank you so much!!!Hi,
user642297 wrote:
Hello gurus,
Can we have a with clause within the cursor ???Sure, but don't take my word for it. Try it and see!
If you get an error, post the error message.
If you get unexpected results, explain.
Whenever you have a problem, post your full Oracle version number.
One more quick question .... How do u guys past in the posting ??? Like somesought of a box .....I have seen lot senior gurus when they are replying, they post the code in some sought of a box ....that makes very easy to understand and take look at the code in the posting....??? Type these 6 characters:
(all small letters, inside curly brackets) before and after formatted sections. -
Using WITH clause in Pro*Cobol
Hi!
I am trying to improve the performance of a query by introducing WITH clause.
The query is in Pro*Cobol Release 9.2.0.6.0 - Production.
I got compilation error
WITH DPTCOST AS (
...............1
PCB-S-00400, Encountered the symbol "DPTCOST" when expecting one of the following:
END-EXEC
....continued
So I wonder if we could use that clause at all with Pro*Cobol
Here is the excerp of the code
EXEC SQL
DECLARE INPUT_ACTUAL CURSOR FOR
WITH DPTCOST AS (
SELECT /*+ rule */
A.CODE_COMBINATION_ID,
A.SEGMENT1, A.SEGMENT2, A.SEGMENT3,
A.SEGMENT6,
D.COSTING, D.PROCESS,
D.MTL_CODE, D.FACTOR
FROM
GL_CODE_COMBINATION A,
ALCGL_DEPARTMENT_COSTINGS D
WHERE
A.TEMPLATE_ID IS NULL
AND A.SUMMARY_FLAG <> 'Y'
AND A.SEGMENT1 = D.PLANT_NUMBER
AND A.SEGMENT3 <> '6999001'
AND A.SEGMENT3 <> '6999002'
AND SUBSTR(A.SEGMENT2,4,3) = D.DEPARTMENT
AND D.ACTUAL_FLAG = 'A'
) ... continuedMaterialized views are basically stored query results. They offer advanced functionality like query rewrite, refresh on commit, and more;
http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_6002.htm
Unlike a view, they actually store the results of the query - not just the query;
SQL> create table t (cid number primary key)
Table created.
SQL> insert into t select object_id from dba_objects where object_id is not null
12791 rows created.
SQL> create materialized view mv
as select * from t
Snapshot created.
SQL> select object_name, object_type from user_objects where object_name ='MV'
OBJECT_NAME OBJECT_TYPE
MV TABLE
MV MATERIALIZED VIEW
2 rows selected.
SQL> select segment_name, bytes from user_segments where segment_name in ('T', 'MV')
SEGMENT_NAME BYTES
T 196608
MV 196608
2 rows selected.Temporary tables are simply tables that are created then dropped. GLOBAL TEMPORARY TABLES have the advantage (or disadvantage) of only existing until commit or the end of the session. They results are visible to the user that inserted the data - but only temporarily;
http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_7002.htm#sthref7483 -
Hi,
I was going through some docs on using WITH clause in curosr...but coulcn't understand.
I was going through existing code for customization, that has 'WITH' clause.
I'm trying to understand below piece of code.
could you let me know what the below code is about..
CURSOR CUR_CRK
IS
WITH PTLPM_WITH AS
(SELECT
/*+ INDEX(P_TR_LOAN_PAST_MONTHLY PK_P_TR_LOAN_PAST_MONTHLY) */
FROM INF.P_TR_LOAN_PAST_MONTHLY
WHERE POST_DATE =P_POST_DATE
AND TREND_GROUP_ID = 'M'
AND APPL_ID IN ('FL','LN')
UIGL_INF_WITH AS
(SELECT * FROM INF.U_IM_GALL_LOAN
XHCC_INF_WITH as
(SELECT * FROM DWC.XREF_HIER_COST_CENTER_11SEP12 --INF.XREF_HIER_COST_CENTER
)Thanks.Perhaps a simple example will make it clear...
SQL> ed
Wrote file afiedt.buf
1 select e.empno, e.ename, d.dname
2 from (select * from emp) e
3 join (select * from dept) d
4* on (e.deptno = d.deptno)
SQL> /
EMPNO ENAME DNAME
7369 SMITH RESEARCH
7499 ALLEN SALES
7521 WARD SALES
7566 JONES RESEARCH
7654 MARTIN SALES
7698 BLAKE SALES
7782 CLARK ACCOUNTING
7788 SCOTT RESEARCH
7839 KING ACCOUNTING
7844 TURNER SALES
7876 ADAMS RESEARCH
7900 JAMES SALES
7902 FORD RESEARCH
7934 MILLER ACCOUNTING
14 rows selected.Ok, a bit of a nonsense query in itself, but it's demonstrating that we have two subqueries that we are getting our data from.
Now, writing the same query using a WITH clause...
SQL> ed
Wrote file afiedt.buf
1 with e as (select * from emp)
2 ,d as (select * from dept)
3 select e.empno, e.ename, d.dname
4* from e join d on (e.deptno = d.deptno)
SQL> /
EMPNO ENAME DNAME
7369 SMITH RESEARCH
7499 ALLEN SALES
7521 WARD SALES
7566 JONES RESEARCH
7654 MARTIN SALES
7698 BLAKE SALES
7782 CLARK ACCOUNTING
7788 SCOTT RESEARCH
7839 KING ACCOUNTING
7844 TURNER SALES
7876 ADAMS RESEARCH
7900 JAMES SALES
7902 FORD RESEARCH
7934 MILLER ACCOUNTING
14 rows selected.As you can see, the subqueries have been moved out of the main query and put into the WITH clause, and then the main query just references those subqueries aliases.
In the above example, there's not much point in doing this, but in more complex queries, you may have a subquery that is used several times, in which case having it in the WITH clause means that that subquery is processed just once and used many times, (you can also look at the MATERIALIZE hint to help improve performance with such subqueries if necessary)
The difficulty with finding this in the documentation is because you will no doubt be trying to search for "WITH", which isn't a very good search term to be using as it's used in the english language too much for an accurate hit... so... when you learn it's called "Subquery Factoring" (because you are factoring out the subqueries from the main query), it then becomes easier to find...
http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10002.htm#i2161315
... and you see it's included as part of the documentation for the SQL SELECT statement. -
Hi All,
I'm using oracle 10gr2.
I have a sql query which starts with a "WITH Clause".... Can is use this directly in a function?
I tried copy pasting in the new function after the BEGIN.....but it errors out saying INTO clause expected in select...when I try including INTO clause...it gives different error...
Appreciate your sugessions
Thanks in advance
H>
pls put an additional select ,as shown below
>
Why???
CREATE OR REPLACE FUNCTION with_test (p_empNo NUMBER)
RETURN VARCHAR2 IS
l_eName VARCHAR2(10);
BEGIN
WITH l_empTab AS
SELECT *
FROM emp
SELECT ename
INTO l_eName
FROM l_empTab
WHERE empno = p_empNo;
RETURN l_eName;
END;
SELECT with_test(7369) ename
FROM dual; Cheers
Ben
Maybe you are looking for
-
Filenames getting truncated in Save for Web AppleScript
Hey all, running CS4 on an Apple Macbook Pro (using OS X 10.5.6). I am creating an AppleScript that saves out a number of files with different filenames using save for web. The problem is that Photoshop CS4 is truncating the filename, and using a sho
-
Hide/Suppress Cost center field in MIGO and MB1A for movement types 551&552
Hi All, Can anybody tell me how to suppress or hide the cost center field in MIGO or MB1A for movement types 551 and 552? I did below settings and it worked fine for only MB1A and for MIGO still it is showing cost center field. IMG > Materials Manage
-
Google search box in Safari not displaying matches
Hello, I just did the latest Software Update on both my MacBook Pro and iMac (the update was a new iTunes--version 10.3). I'm not sure why this is happening, but when I use the Google search box (located in the upper-right corner of the Safari window
-
I made menu but it plays the first video clip and no autostart
Hi all, I created a dvd today with a menu, but instead of starting at the menu and letting you choose it automatically plays the first clip when you play the dvd. And when I put the dvd in a dvd player it doesn't automatically play the dvd menu or an
-
Can i use a environment variable inside a *.sql file?
Hello, I want to create a external table. So i am using the command create or replace directory abc as 'C:\folder'.... inside a sql file. Now i want the path "C:\folder" to be dynamic as i am using this path in many other places also inside the sql f