With clause and union
any alternative solution for these kind of query? i have union in two with clause query but resulting error.
below is a sample example.
1 with x as (select distinct no from dbo.tt)
2 select * from x
3 union all
4 with yy as (select distinct no from dbo.tt)
5* select * from yy
SQL> /
select * from yy
ERROR at line 5:
ORA-32034: unsupported use of WITH clause
with x as (select distinct no from dbo.tt),
yy as (select distinct no from dbo.tt)
select * from x
union all
select * from yySY.
Similar Messages
-
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 -
WITH clause and Custom Folders in Discoverer 10.1.2....
Hi ,
Is there any way to write a sql query using the WITH clause in Discoverer Administrator 10.1.2. custom folder....???
I mean something like this......
WITH sample_data as (select .........)
as
(select ..... from sample_data) which of course is valid in SQL*PLUS....
Thanks , a lot
SimonHi ,
sorry but i didn't make it working.....
I have written this WITH clause...
SQL> with sample_data as (select '13' as id , 'Y' as in_active from dual
2 union all
3 select '194' as id , 'Y' as in_active from dual
4 union all
5 select '792' as id , 'N' as in_active from dual)
6 select id , in_active from sample_data
7 /
ID IN_ACTIVE
13 Y
194 Y
792 NIn custom folder of Discoverer , i have written....
select id , in_active from (with select '13' as id , 'Y' as in_active from dual
union all
select '194' as id , 'Y' as in_active from dual
union all
select '792' as id , 'N' as in_active from dual)But an error occured....ORA-00903 invalid table name
Can somebody help me in this...?????
Thanks , a lot
Simon -
WITH clause, Oracle, UNION problem
I have a table with two fields, SYSTEM and DATEOPENED. It's basically a log of events.
For each row, I need to calculate how much time has elapsed between one row and the next for the same system.
Basically, when I execute this directly against the database (replacing parameters with actual TO_DATE('...','...')), this works flawlessly.
The problem when I run this in Crystal Reports is that the WHERE clause of the CUMULATIVE portion of the UNION is completely ignored. I get ALL the data from the database for the CUMULATIVE.
I inverted both portions of the UNION (DISCRETE first and CUMULATIVE second), which had the result of completely removing the CUMULATIVE portion of the result set.
I'm using Crystal Reports 11.5.0.313 (XI R2)
The query looks like this:
WITH ACCT_DATA AS (
SELECT SYSTEM, Round(DateOpened,'IW') WeekDate, Sum(IsUDRWithinThreshold) CNT_WITHINTHRESHOLD, Count(1) CNT
FROM (
SELECT SYSTEM, DateOpened, (
CASE
WHEN SYSTEM='system1' AND ((Lead (DateOpened,1) over (PARTITION BY SYSTEM ORDER BY SYSTEM, DateOpened))-DateOpened)*24 < 5 THEN 1
WHEN SYSTEM='system2' AND ((Lead (DateOpened,1) over (PARTITION BY SYSTEM ORDER BY SYSTEM, DateOpened))-DateOpened)*24 < 5 THEN 1
WHEN SYSTEM='system3' AND ((Lead (DateOpened,1) over (PARTITION BY SYSTEM ORDER BY SYSTEM, DateOpened))-DateOpened)*24 < 9 THEN 1
ELSE 0
END
) IsUDRWithinThreshold
FROM ACCT_FILES
WHERE SYSTEM IN ('system1','system2','system3')
AND Round(DateOpened,'YYYY')=Round({?ToDate},'YYYY')
GROUP BY SYSTEM, Round(DateOpened,'IW')
ORDER BY SYSTEM, Round(DateOpened,'IW')
SELECT 'CUMULATIVE' DATA_TYPE, d1.SYSTEM, d1.WeekDate, Sum(d2.CNT_WITHINTHRESHOLD) CNT_WITHINTHRESHOLD, Sum(d2.CNT) CNT
FROM ACCT_DATA d1 INNER JOIN ACCT_DATA d2 ON d1.SYSTEM=d2.SYSTEM AND d2.WeekDate<=d1.WeekDate
WHERE d1.WeekDate BETWEEN {?FromDate} AND {?ToDate}
GROUP BY d1.SYSTEM, d1.WeekDate
UNION
SELECT 'DISCRETE' DATA_TYPE, SYSTEM, WeekDate, CNT_WITHINTHRESHOLD, CNT
FROM ACCT_DATA
WHERE WeekDate BETWEEN {?FromDate} AND {?ToDate}
ORDER BY DATA_TYPE, SYSTEM, WeekDateHello,
First, you can't use that SQL directly in Crystal Reports unless you are using a Command object. In which case CR does not modify the SQL at all. It may be due to multiple results sets in which case CR will have problems logically with this type of SQL.
what happens if you use SQLPlus to test the SQL and results?
Also, before doing any of that you are using the original release of Crystal Reports XI R2. Go to the Businessobjects tab above and download SP4, full build. Easier than applying all of the incremental patches but it does require you to un-install CR first.
Test again, if you still get the same problem then do more debugging.
Also note in you reply what DB driver you are using, ODBC, OLE DB etc., and who's driver, Oracle's DataDirect, Microsoft etc.?
Thank you
Don -
Issue with Alias and Union of CTEs
I have a query that works in SQL Server but I'm having issues with it in PL/SQL. I keep running into the error saying "invalid identifier" when I try to reference PlantNumber or Plant_No or any other piece of the CTE or subquery. Is this not possible with Oracle?
Here's a shortened version of my query:
WITH RemoveData AS
SELECT a.PLANT_NO,a.ALLOC_WHDV_VOL,a.KW_CTR_REDELIVERED_HV, a.MTR_NO, a.MTR_SFX, a.TRNX_ID, a.REC_STATUS_CD,
MAX(a.ACCT_DT) ACCT_DT
FROM GasStmt a
WHERE a.REC_STATUS_CD = 'RR'
GROUP BY a.PLANT_NO,a.ALLOC_WHDV_VOL,a.KW_CTR_REDELIVERED_HV, a.MTR_NO, a.MTR_SFX, a.TRNX_ID, a.REC_STATUS_CD
HAVING COUNT(a.REC_STATUS_CD) > 2
RemoveData2 AS
SELECT plant_no "PlantNumber"
,SUM(-a.ALLOC_WHDV_VOL) "PlantStandardGrossWellheadMcf"
,SUM(KW_CTR_REDELIVERED_HV) "KeepWholeResidueMMBtu"
FROM RemoveData a
GROUP BY plant_no
OriginalData AS
SELECT a.PLANT_NO "PlantNumber"
,SUM(a.ALLOC_WHDV_VOL) "PlantStandardGrossWellheadMcf"
,SUM(CASE WHEN a.REC_STATUS_CD = 'RR' THEN -a.KW_CTR_REDELIVERED_HV ELSE a.KW_CTR_REDELIVERED_HV END) "KeepWholeResidueMMBtu"
FROM GasStmt a
LEFT OUTER JOIN (SELECT MTR_NO, MTR_SFX, TRNX_ID, REC_STATUS_CD, MAX(ACCT_DT) ACCT_DT
FROM GasStmt
WHERE REC_STATUS_CD = 'RR'
GROUP BY MTR_NO, MTR_SFX, TRNX_ID, REC_STATUS_CD
HAVING COUNT(TRNX_ID) > 1) b
ON a.MTR_NO = b.MTR_NO
AND a.TRNX_ID = b.TRNX_ID
AND a.Rec_Status_Cd = b.REC_STATUS_CD
AND a.Acct_Dt = b.ACCT_DT
WHERE a.ACCT_DT > '1/1/2010'
AND b.MTR_NO IS NULL
GROUP BY a.PLANT_NO
UnionCTE AS (
SELECT *
FROM RemoveData2
UNION
SELECT *
FROM OriginalData
SELECT PlantNumber, SUM(PlantStandardGrossWellheadMcf) AS PlantStandardGrossWellheadMcf,SUM(KeepWholeResidueMMBtu) AS KeepWholeResidueMMBtu
FROM UnionCTE
GROUP BY PlantNumber
It's the bottom select from UnionCTE that's causing the issue. Any tips would be appreciated!I can't check it at the moment, but here's some code I forgot to post. Thanks for your response, I'll let you know if it works for me.
CREATE TABLE STG.GasStmt
(PLANT_NO varchar(100),
ALLOC_WHDV_VOL numeric(29, 5),
KW_CTR_REDELIVERED_HV numeric(29, 5),
MTR_NO varchar(100),
MTR_SFX varchar(100),
TRNX_ID bigint,
REC_STATUS_CD varchar(100),
ACCT_DT DateTime)
insert into STG.GasStmt
select '043','0','50','36563','','83062200','OR','12/1/2011' union all
select '002','0','100','36563','','83062222','OR','12/1/2011' union all
select '002','0','-.99','36563','','-83062299','RR','12/1/2011' union all
select '002','0','-.99','36563','','-83062299','RR','2/1/2013' union all
select '002','0','-.99','36563','','-83062299','RR','4/1/2013' union all
select '002','0','-.99','36563','','83062299','OR','2/1/2011' union all
select '002','0','-.99','36563','','-86768195','RR','12/1/2011' union all
select '002','0','-.99','36563','','-86768195','RR','2/1/2013' union all
select '002','0','-.99','36563','','-86768195','RR','4/1/2013' union all
select '002','0','-.99','36563','','86768195','OR','3/1/2011' union all
select '002','0','-.99','36563','','-90467786','RR','1/1/2012' union all
select '002','0','-.99','36563','','-90467786','RR','2/1/2013' union all
select '002','0','-.99','36563','','-90467786','RR','4/1/2013' union all
select '002','0','-.99','36563','','90467786','OR','4/1/2011' union all
select '002','0','-.99','36563','','-77671301','RR','2/1/2013' union all
select '002','0','-.99','36563','','-77671301','RR','4/1/2013' union all
select '002','0','-.99','36563','','77671301','OR','1/1/2011' union all
select '002','0','-.99','36563','','-68420423','RR','2/1/2013' union all
select '002','0','-.99','36563','','68420423','OR','4/1/2013' union all
select '002','0','-.99','36563','','-188808446','RR','3/1/2013' union all
select '002','0','-.99','36563','','188808446','OR','1/1/2013' union all
select '002','1205.15','0','36563','A','138365544','OR','2/1/2012' -
Need help with WITH Clause and Outer-Joins
Hi,
I have been struggling to understand these queries.
C:\>sqlplus /nolog
SQL*Plus: Release 10.2.0.1.0 - Production on Thu Oct 20 15:28:33 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
SQL> conn hr@xe
Enter password:
Connected.
SQL> with week as ( select * from (select 1 item, 10 qty, 1 week from dual ))
2 select main.item
3 , week1.qty qty1
4 , week2.qty qty2
5 from week week1
6 , week week2
7 , ( select 1 item from dual ) main
8 where main.item = week1.item (+)
9 and week1.week (+) = 1
10 and main.item = week2.item (+)
11 and week2.week (+) = 2
12 ;
ITEM QTY1 QTY2
1
SQL> with week as ( select 1 item, 10 qty, 1 week from dual )
2 select main.item
3 , week1.qty qty1
4 , week2.qty qty2
5 from week week1
6 , week week2
7 , ( select 1 item from dual ) main
8 where main.item = week1.item (+)
9 and week1.week (+) = 1
10 and main.item = week2.item (+)
11 and week2.week (+) = 2
12 ;
ITEM QTY1 QTY2
1 10
SQL>I don't understand why the first query won't show the result for QTY1. The only difference they have is that the first uses an additional inline view.
Can someone please explain the reason behind.
Thanks,
Allen
Edited by: Allen Sandiego on Oct 20, 2011 3:31 PM
Enclosed in CODE tags.Also in 10.2.0.4.0. The predicate part contains a clue about the transformation
mhouri > select * from v$version;
BANNER
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 Solaris: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
mhouri > with week as ( select * from (select 1 item, 10 qty, 1 week from dual ))
2 select
3 main.item
4 , week1.qty qty1
5 , week2.qty qty2
6 from week week1
7 , week week2
8 , ( select 1 item from dual ) main
9 where main.item = week1.item (+)
10 and week1.week (+) = 1
11 and main.item = week2.item (+)
12 and week2.week (+) = 2;
ITEM QTY1 QTY2
1
mhouri > select * from table(dbms_xplan.display_cursor);
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | | | 8 (100)| |
| 1 | TEMP TABLE TRANSFORMATION | | | | | |
| 2 | LOAD AS SELECT | | | | | |
| 3 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
| 4 | MERGE JOIN OUTER | | 1 | 18 | 6 (0)| 00:00:01 |
| 5 | NESTED LOOPS OUTER | | 1 | 9 | 4 (0)| 00:00:01 |
| 6 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
|* 7 | VIEW | | 1 | 9 | 2 (0)| 00:00:01 |
| 8 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6631_72948EFD | 1 | 39 | 2 (0)| 00:00:01 |
| 9 | BUFFER SORT | | 1 | 9 | 4 (0)| 00:00:01 |
|* 10 | VIEW | | 1 | 9 | 2 (0)| 00:00:01 |
| 11 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6631_72948EFD | 1 | 39 | 2 (0)| 00:00:01 |
Predicate Information (identified by operation id):
7 - filter(("WEEK1"."ITEM"=1 AND "WEEK1"."WEEK"=1))
10 - filter(("WEEK2"."ITEM"=1 AND "WEEK2"."WEEK"=2))
33 rows selected.
mhouri > with week as ( select * from (select 1 item, 10 qty, 1 week from dual ))
2 select /*+ no_query_transformation */
3 main.item
4 , week1.qty qty1
5 , week2.qty qty2
6 from week week1
7 , week week2
8 , ( select 1 item from dual ) main
9 where main.item = week1.item (+)
10 and week1.week (+) = 1
11 and main.item = week2.item (+)
12 and week2.week (+) = 2;
ITEM QTY1 QTY2
1 10
mhouri > select * from table(dbms_xplan.display_cursor);
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | | | 9 (100)| |
| 1 | TEMP TABLE TRANSFORMATION | | | | | |
| 2 | LOAD AS SELECT | | | | | |
| 3 | VIEW | | 1 | 9 | 2 (0)| 00:00:01 |
| 4 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
|* 5 | HASH JOIN OUTER | | 1 | 21 | 7 (15)| 00:00:01 |
|* 6 | HASH JOIN OUTER | | 1 | 12 | 5 (20)| 00:00:01 |
| 7 | VIEW | | 1 | 3 | 2 (0)| 00:00:01 |
| 8 | FAST DUAL | | 1 | | 2 (0)| 00:00:01 |
|* 9 | VIEW | | 1 | 9 | 2 (0)| 00:00:01 |
| 10 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6630_72948EFD | 1 | 9 | 2 (0)| 00:00:01 |
|* 11 | VIEW | | 1 | 9 | 2 (0)| 00:00:01 |
| 12 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6630_72948EFD | 1 | 9 | 2 (0)| 00:00:01 |
Predicate Information (identified by operation id):
5 - access("MAIN"."ITEM"="WEEK2"."ITEM")
6 - access("MAIN"."ITEM"="WEEK1"."ITEM")
9 - filter("WEEK1"."WEEK"=1)
11 - filter("WEEK2"."WEEK"=2)
36 rows selected.Best regards
Mohamed Houri -
ORA-32034: unsupported use of WITH clause-issue
hello all,
i am facing some issue when i use with clause and union all operator.
i have created a dummy code to solve this problem..
my code is ----------
with dept_1 as
(select deptno d1 from detp9 where deptno = 20)
select empno from emp9 e,dept_d1 where e.empno = dept_1.d1
UNION ALL
with dept_1 as
(select deptno d2 from detp9 where deptno = 30)
select empno from emp9 e,dept_d2 where e.empno = dept_2.d2.
when i ran this i gort a message-
ORA-32034: unsupported use of WITH clause.
please help me to solve this iisue..
when i ran it separatly without using union/union all it ran sucessfully..
thanks in advance..923315 wrote:
hello all,
i am facing some issue when i use with clause and union all operator.
i have created a dummy code to solve this problem..
my code is ----------
with dept_1 as
(select deptno d1 from detp9 where deptno = 20)
select empno from emp9 e,dept_d1 where e.empno = dept_1.d1
UNION ALL
with dept_1 as
(select deptno d2 from detp9 where deptno = 30)
select empno from emp9 e,dept_d2 where e.empno = dept_2.d2.
when i ran this i gort a message-
ORA-32034: unsupported use of WITH clause.
please help me to solve this iisue..
when i ran it separatly without using union/union all it ran sucessfully..
thanks in advance..Well, i don't see anything about these queries that makes sense.
You are essentially joining emp and dept on EMPNO to DEPTNO ... that doesn't usually make any sense.
How about you step back from the query which is almost certainly incorrect, and explain your tables, their data and what you need as output?
Cheers, -
Very slow performance with UNION and UNION ALL
I am returning three datasets as one with a UNION ALL between each of three SQL statements. The first one returns either 3 or 4 rows, the second one returns about 10 rows and the third one returns one row. The SQL statements are a little complex but they're all working on very small datasets ( < 1000 rows ) so when I run each of the three SQL statements by themselves, they run to completion and return data in less than 1 second.
However.
When I join the three statements together with a UNION ALL statement between them, the statement runs until I manually abort it. I've run it for up to two hours and it shows no signs of either 1) erroring or 2) running to completion. And, I've tried replacing the UNION ALL with UNION and I get the identical results - nothing returned, no error message.
I've checked the obvious -- same column names, same number of columns, etc. I think that if there were something obvious like that, I would get an error message as soon as I attempted to execute the statements.
I will be happy to post the code if you'd like, but I'm wondering if anyone has experienced these or similar symptoms when joining together SQL statements with UNIONs and if so what you did to get around it.
Thanks in advance,
CarlOr I guess you could use subquery factoring (WITH clause) and MATERIALIZE hints to create the temp tables inline, e.g.
WITH table1 AS
( SELECT /*+ materialize */ somecolumns FROM sometable )
, table2 AS
( SELECT /*+ materialize */ somecolumns FROM othertable )
, table2 AS
( SELECT /*+ materialize */ somecolumns FROM anothertable )
SELECT somecolumns FROM table1
UNION ALL
SELECT somecolumns FROM table2
UNION ALL
SELECT somecolumns FROM table3It might also be valuable to investigate exactly what is happening and why, as this might suggest a way to address the root cause. -
Need help in 'WITH CLAUSE' Query
Hello Gurus,
I am trying to calculate the count of distinct members for each provid.
I am using the with clause to get information regarding the provid.
WITH T AS
(SELECT a.UD_ID MRR_ID,
A.UD_LASTNAME LAST_NAME,
A.UD_FIRSTNAME FIRST_NAME,
COUNT(DISTINCT DP.PA_PROVIDERID) PROVIDERS_ASSIGNED
FROM (SELECT UD.UD_ID,
UD_LASTNAME,
UD_FIRSTNAME
FROM USER_DETAILS UD,
MAP_USERS_TO_ROLES MR
WHERE MR.MUR_UR_ID_REF = 1000
AND MR.MUR_UD_ID_REF = UD.UD_ID) A,
D4C_PROVIDER_ASSIGNMENT DP
WHERE A.UD_ID = DP.PA_ASSIGNEDTO
AND dp.pa_status ='A'
GROUP BY A.UD_ID,
A.UD_LASTNAME,
A.UD_FIRSTNAME
ORDER BY 3 DESC) OUTPUT of just above query without WITH clause.
MRR_ID LAST_NAME FIRST_NAME PROVIDERS_ASSIGNED
1229 mrrTest mrrTest 4
1228 mrr2Last mrr2First 5
1230 mrr1Last mrr1First 7
1226 Panwar SIngh 1
1181 MRRLast MRRTest 4
1221 One MRR 1
1322 Thakuria Bibhuthi 2I am creating this and get all the information to show on front end. Now I want to calculate the no of members as per the providers assigned for each MRR_ID
Below query show the no of members for all the providers assigned to a provider.
ex:
SELECT * FROM (
SELECT COUNT(DISTINCT dmpc_hicn) countmember
FROM D4C_HICN_PROVIDER_claims a WHERE trim(DPMC_PROVIDER_NO) IN
(SELECT trim(pa_providerid)
FROM d4c_provider_assignment
WHERE pa_assignedto = 1181 (mrr_id) --here i have use the mrrid from with clause and get the member count with all the columns coming from WITH CLAUSE.
AND pa_roleid = 1000
AND PA_STATUS ='A'
GROUP BY a.dmpc_ss_id_ref)Right now I am using materialized view i dont wanna use the same..
I am sending the materialized view code as well what i am doing ..
( SELECT SUM(member_count)member_count_bynpi ,mrr_id FROM (
(SELECT count(DISTINCT hp.dmpc_hicn) member_count,hp.DMPC_SS_ID_REF ,a1.ud_id mrr_id FROM D4C_HICN_PROVIDER_claims hp ,
(SELECT a.UD_ID ,DP.PA_PROVIDERID
FROM (SELECT UD.UD_ID, UD_LASTNAME, UD_FIRSTNAME
FROM USER_DETAILS UD, MAP_USERS_TO_ROLES MR
WHERE MR.MUR_UR_ID_REF = 1000
AND MR.MUR_UD_ID_REF = UD.UD_ID) A,
D4C_PROVIDER_ASSIGNMENT DP
WHERE A.UD_ID = DP.PA_ASSIGNEDTO
AND dp.pa_status ='A'
/*AND dp.PA_ASSIGNEDTO = 1221*/) a1
WHERE trim(a1.PA_PROVIDERID) = trim(hp.dpmc_provider_no)
GROUP BY a1.ud_id,hp.DMPC_SS_ID_REF))
GROUP BY mrr_id)Please help me to write the code with the materialized view. Thanks in Advance.
Kind regards,
UP
Edited by: BluShadow on 22-Aug-2011 07:58
fixed {noformat}{noformat} tags. Please use lowercase "code" rather than uppercase "CODE" in the tags.Probably, this may help
WITH T1 as(
SELECT UD.UD_ID,
UD_LASTNAME,
UD_FIRSTNAME
FROM USER_DETAILS UD, MAP_USERS_TO_ROLES MR
WHERE MR.MUR_UR_ID_REF = 1000
AND MR.MUR_UD_ID_REF = UD.UD_ID)
T2 as (SELECT a.UD_ID ,DP.PA_PROVIDERID
FROM T1 A, D4C_PROVIDER_ASSIGNMENT DP
WHERE A.UD_ID = DP.PA_ASSIGNEDTO
AND dp.pa_status ='A')
T3 as(SELECT count(DISTINCT hp.dmpc_hicn) member_count,
hp.DMPC_SS_ID_REF ,
a1.ud_id mrr_id
FROM T2 A1 ,D4C_HICN_PROVIDER_claims hp
WHERE trim(a1.PA_PROVIDERID) = trim(hp.dpmc_provider_no)
GROUP BY a1.ud_id,hp.DMPC_SS_ID_REF)
SELECT SUM(member_count)member_count_bynpi ,mrr_id from T3
GROUP BY mrr_id -
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. -
WITH clause: SP2-0734:
Hello. Got a problem with the WITH clause in SQL*Plus (sqlplus.exe), can't see why this happens.
12:48:49 RAY@CNRTEST1> select * from v$version;
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 Solaris: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
Tried running my large sql statement, which has the WITH clause. This fails with ORA-00933: at the SELECT keyword.
So, to prove a point, I connect as SCOTT/TIGER, and try a simple WITH clause, and hit <Return> expecting a line 2 to appear:
12:00:19 RAY@CNRTEST1> with e1 as (select count(*) from emp)
but get this error:
SP2-0734: unknown command beginning "with e1 as..." - rest of line ignored.
However, if I run this using sqlplusw.exe, it runs fine.
Is there some environment setting I've missed in sqlplus.exe? Any suggestions appreciated.Hi,
With clause it's valid for Oracle 9i release 2 and beyond. You must add Select clause to use it.
WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);Regards, -
Using full outer join of subqueries named using with clause
Hi,
I am trying to create a view which is having 2 subqueries vol1 & vol2 with WITH clause. I am joining those 2 subqueries in the main query with FULL OUTER JOIN.
When i compile that view in a tool like pl/sql developer, It has been compiled successfully.
But when i call the view creation script from SQL command prompt, It is throwing error as
from vol1 FULL JOIN vol2 o ON (vol1.ct_reference = vol2.ct_reference and vol1.table_name = vol2.table_name
ERROR at line 29:
ORA-00942: table or view does not exist
Kindly advise whats going wrong.that's line 29. Maybe you get a better idea if you strip your operation of all the unneccessary elements until it works.
There are some known bugs with subquery factoring (aka with clause) and also with ANSI join syntax, but it is hard to tell what happens here based on your description. But one thing is strange - if it is not a result of formatting (not formatting): I would expect the asterisk beneath the unknown table and not beneath the key word FULL.
P.S.: my editor makes me think it's rather a proportional font thing. Have I already said that I don't like proportional font for SQL code examples? -
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 -
Syntax help needed in update using 'WITH' Clause
Update CP_JP_CORP_FSASA_FEEDUPLOAD_r r
set (
gfrn,
tenor_code,
tenor_description,
exposure_category,
frr,
facility_classification,
limit_amount,
limit_usd,
approval_ccy,
approval_date,
expiry_date,
avail_status_code,
avail_status_desc,
revolving_indicator,
committed_flag,
committed_until_date,
committed_amount,
advised_flag,
advised_amount,
facility_long_description,
booking_unit_code,
extending_unit_code,
extending_unit_short_desc,
approving_unit_code,
approving_unit_short_des,
transaction_type,
branch_no
=
With t as
Select fac.gfrn,fac.tenor_code,fac.tenor_description,fac.exposure_category,fac.frr,
fac.facility_classification,fac.limit_amount,fac.limit_usd,fac.approval_ccy,
fac.approval_date,fac.expiry_date,fac.avail_status_code,fac.avail_status_desc,
fac.revolving_indicator,fac.committed_flag,fac.committed_until_date,fac.committed_amount,
fac.advised_flag,fac.advised_amount,fac.facility_long_description,fac.booking_unit_code,
fac.extending_unit_code,fac.extending_unit_short_desc,fac.approving_unit_code,fac.approving_unit_short_des,
/*'Check' normalflag,
cust.adjusted_orr fsasaorr1stborrower,
'Normal' category1stborrower,
cust.adjusted_orr fsasaorr2ndborrower,
'Normal' category2ndborrower,
cust.adjusted_orr fsasaorrfinal,
'Normal' categoryfinal */
txn.transaction_type,txn.branch_no,txn.gfcid,txn.transaction_id
from cp_fsa_boj_corp_cr_fac_hist fac,
--cp_fsa_boj_corp_cr_cust_hist cust,
cp_fsa_boj_corp_cr_txn_hist txn
where fac.gfcid = txn.gfcid
and fac.facility_id = txn.facility_id
and fac.as_of_date = txn.as_of_date
and to_char(fac.as_of_date,'yyyymm') = p_financial_period
and fac.as_of_date = last_day(fac.as_of_date)
select t.gfrn,
t.tenor_code,
t.tenor_description,
t.exposure_category,
t.frr,
t.facility_classification,
t.limit_amount,
t.limit_usd,
t.approval_ccy,
t.approval_date,
t.expiry_date,
t.avail_status_code,
t.avail_status_desc,
t.revolving_indicator,
t.committed_flag,
t.committed_until_date,
t.committed_amount,
t.advised_flag,
t.advised_amount,
t.facility_long_description
t.booking_unit_code,
t.extending_unit_code,
t.extending_unit_short_desc,
t.approving_unit_code,
t.approving_unit_short_des,
t.transaction_type,
t.branch_no
from t
where r.financialperiod = p_financial_period
and exists
Select 1
from t
where t.transaction_id = r.ce_trans_id
)I'm facing syntax problemHii All,
This is my actual update.(I stopped performing dml operations in cursors following Karthick Arp) :-)
Update CP_JP_CORP_FSASA_FEEDUPLOAD_r r
set (
gfrn,
tenor_code,
tenor_description,
exposure_category,
frr,
facility_classification,
limit_amount,
limit_usd,
approval_ccy,
approval_date,
expiry_date,
avail_status_code,
avail_status_desc,
revolving_indicator,
committed_flag,
committed_until_date,
committed_amount,
advised_flag,
advised_amount,
facility_long_description,
booking_unit_code,
extending_unit_code,
extending_unit_short_desc,
approving_unit_code,
approving_unit_short_des,
transaction_type,
branch_no
= (
Select fac.gfrn,fac.tenor_code,fac.tenor_description,fac.exposure_category,fac.frr,
fac.facility_classification,fac.limit_amount,fac.limit_usd,fac.approval_ccy,
fac.approval_date,fac.expiry_date,fac.avail_status_code,fac.avail_status_desc,
fac.revolving_indicator,fac.committed_flag,fac.committed_until_date,fac.committed_amount,
fac.advised_flag,fac.advised_amount,fac.facility_long_description,fac.booking_unit_code,
fac.extending_unit_code,fac.extending_unit_short_desc,fac.approving_unit_code,fac.approving_unit_short_des,
txn.transaction_type,txn.branch_no
from cp_fsa_boj_corp_cr_fac_hist fac,
--cp_fsa_boj_corp_cr_cust_hist cust,
cp_fsa_boj_corp_cr_txn_hist txn
where fac.gfcid = txn.gfcid
and fac.facility_id = txn.facility_id
and fac.as_of_date = txn.as_of_date
and to_char(fac.as_of_date,'yyyymm') = p_financial_period
and fac.as_of_date = last_day(fac.as_of_date)
where Exists
Select 1
from cp_fsa_boj_corp_cr_fac_hist fac,
cp_fsa_boj_corp_cr_txn_hist txn
where fac.gfcid = txn.gfcid
and fac.facility_id = txn.facility_id
and fac.as_of_date = txn.as_of_date
and to_char(fac.as_of_date,'yyyymm') = p_financial_period
and fac.as_of_date = last_day(fac.as_of_date)
and txn.transaction_id = r.ce_trans_id
)Now in my update I'm using same 'SELECT' twice once in 'SET' and again in 'EXISTS' clause. I'd like to make use of 'WITH' Clause and avoid unnecessary 'SELECT' . Please help me. -
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.
Maybe you are looking for
-
I am unable to assign breakpoints in Flex Builder 4
I am new to Flex ( a month or so ) and have inherited a big project. This is an odd one. I am unable to assign breakpoints in a particular project. (I've redacted company specifics...) Here is an example: If there is a breakpoint loaded the project w
-
Hi Experts, Program is exeuting syntatically fine, but, when executing its show, time exceed error in ECC 6.0 , initially it is developed in 4.6c. Please, can any one suggest this same code to incorporate in Forall entries. SELECT abukrs aebeln beb
-
I am trying to add a image to portal....Getting error as follows: : Unexpected error encountered in wwv_manage_images.process_create (ORA-06508: PL/SQL: could not find program unit being called) (WWV-00100) I also rerun the ssodatan script and it ran
-
I have a 17inch powebook 2gb ram , 160GB 54000rpm HDD and when im in imovie importing moves from my Panasonic HD-SC1 via USB on the import option i've set it to Full 1920x1080 but below the option there is a message of Selecting Full on this computer
-
Recent update just ruined my iMac
So, i just updated my imac with iTunes 7.6, and the new OSX (10.4.11) update. Well the download went smoothly, and when i restarted it took over 2 hours to boot. Then, when it finished, now over half of my programs will not start. Those programs incl