Help rewriting this XMLTable query.
Hi,
I rewrote this query:
SELECT VALUE(k) ts_value,
s.transactionsetcontrolnumber ts,
t.xmlintid,
i.isaid,
stid,
g.sendorcode || '!' || g.receivercode || '!' || s.transactionsetid || '!I' geteditpparameter,
EXTRACTVALUE(VALUE(k), '/TS_850/BEG/BEG03') || '-' ||
(SELECT N104
FROM xmltable('/TS_850/GROUP_5/N1' passing VALUE(k) columns N101
VARCHAR2(2) path 'N101',
N104 VARCHAR2(32) path 'N104')
WHERE N101 = 'OB') beg_n104,
g.sendorcode || '!' || g.receivercode || '!' ||
(SELECT N103 || '!' || N104
FROM xmltable('/TS_850/GROUP_5/N1' passing VALUE(k) columns N101
VARCHAR2(2) path 'N101',
N103 VARCHAR2(32) path 'N103',
N104 VARCHAR2(32) path 'N104')
WHERE N101 = 'OB') getedicuststxparameter,
(SELECT to_date(dtm02, 'YYYYMMDD')
FROM xmltable('/TS_850/DTM' passing VALUE(k) columns dtm01
VARCHAR2(3) path 'DTM01',
dtm02 VARCHAR2(32) path 'DTM02')
WHERE DTM01 <> '061') requireddate,
(SELECT SUM(SAC07)
FROM xmltable('/TS_850/GROUP_1/SAC' passing VALUE(k) columns
SAC01 VARCHAR2(2) path 'SAC01',
SAC07 VARCHAR2(32) path 'SAC07')
WHERE SAC01 = 'A') sumsac07
FROM edi_xml_int t,
edi_isa i,
edi_gs g,
edi_st s,
TABLE(XMLSEQUENCE(EXTRACT(x12_850, '/X12/TS_850'))) k
WHERE s.gsid = g.gsid
AND g.isaid = i.isaid
AND t.isaid = i.isaid
AND s.transactionsetcontrolnumber =
EXTRACTVALUE(VALUE(k), '/TS_850/ST/ST02')
AND t.processeddate IS NULL
AND s.transactionsetid = '850'
AND TRIM(i.senderid) = '0722717110100'
and s.updateddate is null order by s.stid;
Explain Plan for this is:
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 8077 | 16M| 1056 (2)| 00:00:13 |
|* 1 | COLLECTION ITERATOR PICKLER FETCH | XMLSEQUENCEFROMXMLTYPE | | | | |
|* 2 | COLLECTION ITERATOR PICKLER FETCH | XMLSEQUENCEFROMXMLTYPE | | | | |
|* 3 | COLLECTION ITERATOR PICKLER FETCH | XMLSEQUENCEFROMXMLTYPE | | | | |
| 4 | SORT AGGREGATE | | 1 | 2 | | |
|* 5 | COLLECTION ITERATOR PICKLER FETCH| XMLSEQUENCEFROMXMLTYPE | | | | |
| 6 | NESTED LOOPS | | 8077 | 16M| 1056 (2)| 00:00:13 |
| 7 | NESTED LOOPS | | 45 | 95175 | 55 (0)| 00:00:01 |
| 8 | NESTED LOOPS | | 45 | 3510 | 4 (0)| 00:00:01 |
| 9 | MERGE JOIN CARTESIAN | | 45 | 2070 | 3 (0)| 00:00:01 |
|* 10 | TABLE ACCESS BY INDEX ROWID | EDI_ST | 45 | 1125 | 2 (0)| 00:00:01 |
| 11 | INDEX FULL SCAN | EDI_ST_PK | 45 | | 1 (0)| 00:00:01 |
| 12 | BUFFER SORT | | 1 | 21 | 1 (0)| 00:00:01 |
| 13 | TABLE ACCESS BY INDEX ROWID | EDI_ISA | 1 | 21 | 1 (0)| 00:00:01 |
|* 14 | INDEX RANGE SCAN | EDI_ISA_IX_1 | 1 | | 0 (0)| 00:00:01 |
|* 15 | TABLE ACCESS BY INDEX ROWID | EDI_GS | 1 | 32 | 1 (0)| 00:00:01 |
|* 16 | INDEX UNIQUE SCAN | EDI_GS_PK | 1 | | 0 (0)| 00:00:01 |
|* 17 | TABLE ACCESS FULL | EDI_XML_INT | 1 | 2037 | 1 (0)| 00:00:01 |
|* 18 | COLLECTION ITERATOR PICKLER FETCH| XMLSEQUENCEFROMXMLTYPE | | | | |
Predicate Information (identified by operation id):
1 - filter(CAST(SYS_XQ_UPKXML2SQL(SYS_XQEXVAL(SYS_XQEXTRACT(VALUE(KOKBF$),'/*/N101')),50,1,2) AS
VARCHAR2(2) )='OB')
2 - filter(CAST(SYS_XQ_UPKXML2SQL(SYS_XQEXVAL(SYS_XQEXTRACT(VALUE(KOKBF$),'/*/N101')),50,1,2) AS
VARCHAR2(2) )='OB')
3 - filter(CAST(SYS_XQ_UPKXML2SQL(SYS_XQEXVAL(SYS_XQEXTRACT(VALUE(KOKBF$),'/*/DTM01')),50,1,2) AS
VARCHAR2(3) )<>'061')
5 - filter(CAST(SYS_XQ_UPKXML2SQL(SYS_XQEXVAL(SYS_XQEXTRACT(VALUE(KOKBF$),'/*/SAC01')),50,1,2) AS
VARCHAR2(2) )='A')
10 - filter("S"."TRANSACTIONSETID"='850' AND "S"."UPDATEDDATE" IS NULL)
14 - access(TRIM("SENDERID")='0722717110100')
15 - filter("G"."ISAID"="I"."ISAID")
16 - access("S"."GSID"="G"."GSID")
17 - filter("T"."PROCESSEDDATE" IS NULL AND "T"."ISAID"="I"."ISAID")
18 - filter("S"."TRANSACTIONSETCONTROLNUMBER"=EXTRACTVALUE(VALUE(KOKBF$),'/TS_850/ST/ST02'))
Note
- dynamic sampling used for this statement
47 rows selected.I rewrote this to this:
SELECT k.main ts_value,
s.transactionsetcontrolnumber ts,
t.xmlintid,
i.isaid,
stid,
g.sendorcode || '!' || g.receivercode || '!' || s.transactionsetid || '!I' geteditpparameter,
EXTRACTVALUE(k.main, '/TS_850/BEG/BEG03') || '-' ||
(SELECT N104
FROM xmltable('/TS_850/GROUP_5/N1' passing k.main columns N101
VARCHAR2(2) path 'N101',
N104 VARCHAR2(32) path 'N104')
WHERE N101 = 'OB') beg_n104,
g.sendorcode || '!' || g.receivercode || '!' ||
(SELECT N103 || '!' || N104
FROM xmltable('/TS_850/GROUP_5/N1' passing k.main columns N101
VARCHAR2(2) path 'N101',
N103 VARCHAR2(32) path 'N103',
N104 VARCHAR2(32) path 'N104')
WHERE N101 = 'OB') getedicuststxparameter,
(SELECT to_date(dtm02, 'YYYYMMDD')
FROM xmltable('/TS_850/DTM' passing k.main columns dtm01
VARCHAR2(3) path 'DTM01',
dtm02 VARCHAR2(32) path 'DTM02')
WHERE DTM01 <> '061') requireddate,
(SELECT SUM(SAC07)
FROM xmltable('/TS_850/GROUP_1/SAC' passing k.main columns
SAC01 VARCHAR2(2) path 'SAC01',
SAC07 VARCHAR2(32) path 'SAC07')
WHERE SAC01 = 'A') sumsac07
FROM edi_xml_int t,
edi_isa i,
edi_gs g,
edi_st s,
xmltable('/X12/TS_850' passing x12_850 columns main xmltype path
'/TS_850',
st02 varchar2(32) path '/TS_850/ST/ST02') k
WHERE s.gsid = g.gsid
AND g.isaid = i.isaid
AND t.isaid = i.isaid
AND s.transactionsetcontrolnumber = k.st02
AND t.processeddate IS NULL
AND s.transactionsetid = '850'
AND TRIM(i.senderid) = '0722717110100'
and s.updateddate is null
order by s.stid;
Explain plan for this is:
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 8077 | 16M| 1056 (2)| 00:00:13 |
|* 1 | COLLECTION ITERATOR PICKLER FETCH | XMLSEQUENCEFROMXMLTYPE | | | | |
|* 2 | COLLECTION ITERATOR PICKLER FETCH | XMLSEQUENCEFROMXMLTYPE | | | | |
|* 3 | COLLECTION ITERATOR PICKLER FETCH | XMLSEQUENCEFROMXMLTYPE | | | | |
| 4 | SORT AGGREGATE | | 1 | 2 | | |
|* 5 | COLLECTION ITERATOR PICKLER FETCH| XMLSEQUENCEFROMXMLTYPE | | | | |
| 6 | NESTED LOOPS | | 8077 | 16M| 1056 (2)| 00:00:13 |
| 7 | NESTED LOOPS | | 45 | 95175 | 55 (0)| 00:00:01 |
| 8 | NESTED LOOPS | | 45 | 3510 | 4 (0)| 00:00:01 |
| 9 | MERGE JOIN CARTESIAN | | 45 | 2070 | 3 (0)| 00:00:01 |
|* 10 | TABLE ACCESS BY INDEX ROWID | EDI_ST | 45 | 1125 | 2 (0)| 00:00:01 |
| 11 | INDEX FULL SCAN | EDI_ST_PK | 45 | | 1 (0)| 00:00:01 |
| 12 | BUFFER SORT | | 1 | 21 | 1 (0)| 00:00:01 |
| 13 | TABLE ACCESS BY INDEX ROWID | EDI_ISA | 1 | 21 | 1 (0)| 00:00:01 |
|* 14 | INDEX RANGE SCAN | EDI_ISA_IX_1 | 1 | | 0 (0)| 00:00:01 |
|* 15 | TABLE ACCESS BY INDEX ROWID | EDI_GS | 1 | 32 | 1 (0)| 00:00:01 |
|* 16 | INDEX UNIQUE SCAN | EDI_GS_PK | 1 | | 0 (0)| 00:00:01 |
|* 17 | TABLE ACCESS FULL | EDI_XML_INT | 1 | 2037 | 1 (0)| 00:00:01 |
|* 18 | COLLECTION ITERATOR PICKLER FETCH| XMLSEQUENCEFROMXMLTYPE | | | | |
Predicate Information (identified by operation id):
1 - filter(CAST(SYS_XQ_UPKXML2SQL(SYS_XQEXVAL(SYS_XQEXTRACT(VALUE(KOKBF$),'/*/N101')),50,1,2) AS
VARCHAR2(2) )='OB')
2 - filter(CAST(SYS_XQ_UPKXML2SQL(SYS_XQEXVAL(SYS_XQEXTRACT(VALUE(KOKBF$),'/*/N101')),50,1,2) AS
VARCHAR2(2) )='OB')
3 - filter(CAST(SYS_XQ_UPKXML2SQL(SYS_XQEXVAL(SYS_XQEXTRACT(VALUE(KOKBF$),'/*/DTM01')),50,1,2) AS
VARCHAR2(3) )<>'061')
5 - filter(CAST(SYS_XQ_UPKXML2SQL(SYS_XQEXVAL(SYS_XQEXTRACT(VALUE(KOKBF$),'/*/SAC01')),50,1,2) AS
VARCHAR2(2) )='A')
10 - filter("S"."TRANSACTIONSETID"='850' AND "S"."UPDATEDDATE" IS NULL)
14 - access(TRIM("SENDERID")='0722717110100')
15 - filter("G"."ISAID"="I"."ISAID")
16 - access("S"."GSID"="G"."GSID")
17 - filter("T"."PROCESSEDDATE" IS NULL AND "T"."ISAID"="I"."ISAID")
18 - filter("S"."TRANSACTIONSETCONTROLNUMBER"=CAST(SYS_XQ_UPKXML2SQL(SYS_XQEXVAL(SYS_XQEXTRACT(VALU
E(KOKBF$),'/TS_850/ST/ST02')),50,1,2) AS varchar2(32) ))
Note
- dynamic sampling used for this statement
48 rows selected.Now, I was wondering if you can have just one implementation of XMLTable in the query which will reduce the complexity of this. Both the queries looked like they gave me the same results.
Thank you,
Rahul.
It's quite hard to tune a query without seeing the table structure and available indices as well as relationship between each other but I'll try.
In your sql which you've given the table alias AD, you have this filter criteria
AND FIELD_NAME IN (
SELECT FIELD_NAME
FROM APPLICATION_FIELDS
WHERE FIELD_TYPE IN ('Checkbox','Radio','Select','SelectM', 'RadioHoriz','RadioPara')
) </br>
My question is, is FIELD_NAME a column in the APPLICATION_DATA table? If it is, try moving this clause outside of the current sub-query it is in, i.e. one more level up. It looks like you put them inside the wrong level.
I would also try converting this <COL> IN <SUB-QUERY> to a join, if that wouldn't affect the no. of rows returned. I can't tell for sure since I dont know the relationship between your tables.
It looks to me like you're doing this
SELECT <col list>
FROM ( SELECT -- LEVEL 1
FROM APPLICATION_DATA AD
WHERE AD.APPLICAITON_ID IN ( -- LEVEL 2
SELECT
FROM APPLICATIONS A
WHERE A.JOB_ID IN (
<SUBQUERY>
AND A.ACCOUNT_ID IN (
<SUBQUERY>
AND FIELDNAME IN ( -- SHOULD BE INSIDE LEVEL1
<SUBQUERY>
) AD,
( SELECT ....
) FD
WHERE ....
Similar Messages
-
Need help in this sql query to use Case Statement
hi All,
I have the below query -
SELECT DISTINCT OFFC.PROV_ID
,OFFC.WK_DAY
,CASE
WHEN OFFC.WK_DAY ='MONDAY' THEN 1
WHEN OFFC.WK_DAY ='TUESDAY' THEN 2
WHEN OFFC.WK_DAY ='WEDNESDAY' THEN 3
WHEN OFFC.WK_DAY ='THURSDAY' THEN 4
WHEN OFFC.WK_DAY ='FRIDAY' THEN 5
WHEN OFFC.WK_DAY ='SATURDAY' THEN 6
WHEN OFFC.WK_DAY ='SUNDAY' THEN 7
END AS DOW
,OFFC.OFFC_OPENG_TIME
,OFFC.OFFC_CLSNG_TIME
FROM GGDD.PROV_OFFC_HR OFFC
WHERE OFFC.PROV_ID='0000600'
WITH UR;
this query is bringing results in 6 differnt rows with opening and closing time for each day separately. I want to generate the data in one row with each day having opening and closing time, so for 7 days, total 14 columns with opening and closing time. But i am not able to do that using case statement.
can somebody help me in achieving that.
thanks,
iamhereHi,
Welcome to the forum!
That's called a Pivot .
Instead of having 1CASE expression, have 14, one for the opening and one for the closing time each day, and do GROUP BY to combine them onto one row.
SELECT OFFC.PROV_ID
, MIN (CASE WHEN OFFC.WK_DAY ='MONDAY' THEN OFFC.OFFC_OPENG_TIME END) AS mon_opn
, MIN (CASE WHEN OFFC.WK_DAY ='MONDAY' THEN OFFC.OFFC_CLSNG_TIME END) AS mon_cls
, MIN (CASE WHEN OFFC.WK_DAY ='TUESDAY' THEN OFFC.OFFC_OPENG_TIME END) AS tue_opn
, MIN (CASE WHEN OFFC.WK_DAY ='TUESDAY' THEN OFFC.OFFC_CLSNG_TIME END) AS tue_cls
FROM GGDD.PROV_OFFC_HR OFFC
WHERE OFFC.PROV_ID = '0000600'
GROUP BY offc.prov_id
;This assumes there is (at most) only one row in the table for each distinct prov_id and weekday. If not, what do you want to do? Post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
The staement above works in Oracle 8.1 and up, but there's a better way (SELECT ... PIVOT) available in Oracle 11. What version are you using? (It's always a good idea to include this when you post a question.)
Edited by: Frank Kulash on Jan 6, 2011 8:22 PM -
Help tune this monster query please
Hi, I have a query that runs for a long time. Its a system generated query but I need to improve its performance. I have enough indexes on the tables. Here is the query, its explain plan and the tkprof:
The QUERY
SELECT FD.FORM_ID, FD.FIELD_NAME, FD.FIELD_TEXT, FD.OPTION_TEXT, FD.OPTION_TYPE, FD.FIELD_ORDER, FD.LIST_ORDER,
FD.MULTIPLE_ANSWER, FD.FIELD_INSTANCE, NVL(AD.APPS,0) APPS
FROM (
SELECT FIELD_NAME, FIELD_VALUE, INSTANCE, COUNT(0) APPS
FROM APPLICATION_DATA AD
WHERE APPLICATION_ID IN (
SELECT A.APPLICATION_ID
FROM APPLICATIONS A
WHERE A.DELETED = 0 AND DECODE(A.TRAY,'Incomplete','Incomplete','Complete') = 'Complete' AND A.JOB_ID IN (
SELECT JOB_ID
FROM JOBS J
WHERE J.ACCOUNT_ID IN (
SELECT ACCOUNT_ID
FROM AGENCY_ACCOUNTS CONNECT BY ACCOUNT_ID = PRIOR AGENCY_ID START WITH ACCOUNT_ID = J.ACCOUNT_ID ) ) AND A.ACCOUNT_ID IN (
SELECT ACCOUNT_ID
FROM AGENCY_ACCOUNTS CONNECT BY ACCOUNT_ID = PRIOR AGENCY_ID START WITH ACCOUNT_ID = 113346 ) AND FIELD_NAME IN (
SELECT FIELD_NAME FROM APPLICATION_FIELDS
WHERE FIELD_TYPE IN ('Checkbox','Radio','Select','SelectM', 'RadioHoriz','RadioPara') ) )
GROUP BY FIELD_NAME, FIELD_VALUE, INSTANCE ) AD, (
SELECT AF.FORM_ID, AF.FIELD_NAME, AF.FIELD_TEXT, AFO.OPTION_TEXT, AFO.OPTION_TYPE, AF.FIELD_ORDER, AFO.LIST_ORDER,
DECODE(AF.FIELD_TYPE,'Checkbox',1,'SelectM',1,0) MULTIPLE_ANSWER, AF.FIELD_INSTANCE
FROM APPLICATION_FIELDS AF, APPLICATION_FIELD_OPTIONS AFO, JOB_FORMS JF
WHERE AFO.FIELD_NAME = AF.FIELD_NAME AND AFO.FORM_ID = AF.FORM_ID AND JF.FORM_ID = AF.FORM_ID AND AF.FIELD_TYPE IN
('Checkbox','Radio','Select','SelectM', 'RadioHoriz','RadioPara') AND JF.JOB_ID IN (
SELECT JOB_ID
FROM JOBS J
WHERE J.ACCOUNT_ID IN (
SELECT ACCOUNT_ID
FROM AGENCY_ACCOUNTS CONNECT BY ACCOUNT_ID = PRIOR AGENCY_ID START WITH ACCOUNT_ID = 113346 ) ) ) FD
WHERE AD.FIELD_NAME = FD.FIELD_NAME AND AD.FIELD_VALUE = FD.OPTION_TEXT AND AD.INSTANCE = FD.FIELD_INSTANCE
GROUP BY FD.FORM_ID, FD.FIELD_NAME, FD.FIELD_TEXT, FD.OPTION_TEXT, FD.OPTION_TYPE, FD.FIELD_ORDER, FD.LIST_ORDER,
FD.MULTIPLE_ANSWER, FD.FIELD_INSTANCE, NVL(AD.APPS,0)
ORDER BY FD.FORM_ID ASC, FD.FIELD_ORDER ASCEXPLAIN PLAN OUTPUT
PLAN_TABLE_OUTPUT
Plan hash value: 3519364953
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 120 | 763 (2)| 00:00:10 |
| 1 | SORT GROUP BY | | 1 | 120 | 763 (2)| 00:00:10 |
| 2 | VIEW | | 1 | 120 | 762 (1)| 00:00:10 |
| 3 | HASH GROUP BY | | 1 | 220 | 762 (1)| 00:00:10 |
|* 4 | FILTER | | | | | |
|* 5 | TABLE ACCESS BY INDEX ROWID | APPLICATION_DATA | 1 | 45 | 6 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
| 6 | NESTED LOOPS | | 40 | 8800 | 486 (2)| 00:00:06 |
|* 7 | HASH JOIN | | 60 | 10500 | 125 (4)| 00:00:02 |
|* 8 | HASH JOIN | | 60 | 7320 | 53 (6)| 00:00:01 |
| 9 | NESTED LOOPS | | 11 | 297 | 9 (12)| 00:00:01 |
| 10 | VIEW | VW_NSO_3 | 3 | 15 | 5 (0)| 00:00:01 |
| 11 | HASH UNIQUE | | 3 | 69 | | |
| 12 | TABLE ACCESS BY INDEX ROWID | JOBS | 3 | 30 | 3 (0)| 00:00:01 |
| 13 | NESTED LOOPS | | 3 | 69 | 5 (0)| 00:00:01 |
| 14 | VIEW | VW_NSO_1 | 1 | 13 | 2 (0)| 00:00:01 |
|* 15 | CONNECT BY WITH FILTERING | | | | | |
| 16 | TABLE ACCESS BY INDEX ROWID | AGENCY_ACCOUNTS | | | | |
PLAN_TABLE_OUTPUT
|* 17 | INDEX UNIQUE SCAN | AGENCY_ACCOUNTS_ACCOUNT_ID | 1 | 5 | 1 (0)| 00:00:01 |
| 18 | NESTED LOOPS | | | | | |
| 19 | BUFFER SORT | | | | | |
| 20 | CONNECT BY PUMP | | | | | |
| 21 | TABLE ACCESS BY INDEX ROWID| AGENCY_ACCOUNTS | 1 | 10 | 2 (0)| 00:00:01 |
|* 22 | INDEX UNIQUE SCAN | AGENCY_ACCOUNTS_ACCOUNT_ID | 1 | | 1 (0)| 00:00:01 |
|* 23 | TABLE ACCESS FULL | AGENCY_ACCOUNTS | 1 | 10 | 2 (0)| 00:00:01 |
|* 24 | INDEX RANGE SCAN | JOBS_ACCOUNT_ID | 3 | | 1 (0)| 00:00:01 |
|* 25 | INDEX RANGE SCAN | JOBS_FORMS_JID_FID | 4 | 88 | 1 (0)| 00:00:01 |
|* 26 | TABLE ACCESS FULL | APPLICATION_FIELDS | 3579 | 332K| 43 (3)| 00:00:01 |
| 27 | TABLE ACCESS FULL | APPLICATION_FIELD_OPTIONS | 32897 | 1702K| 72 (3)| 00:00:01 |
PLAN_TABLE_OUTPUT
|* 28 | INDEX RANGE SCAN | IDX$$_B8D40001 | 4 | | 3 (0)| 00:00:01 |
|* 29 | FILTER | | | | | |
| 30 | MERGE JOIN CARTESIAN | | 3 | 225 | 11 (0)| 00:00:01 |
| 31 | NESTED LOOPS | | 1 | 53 | 6 (0)| 00:00:01 |
| 32 | NESTED LOOPS | | 1 | 40 | 4 (0)| 00:00:01 |
|* 33 | TABLE ACCESS BY INDEX ROWID | APPLICATIONS | 1 | 30 | 3 (0)| 00:00:01 |
|* 34 | INDEX UNIQUE SCAN | APPS_APP_ID | 1 | | 2 (0)| 00:00:01 |
| 35 | TABLE ACCESS BY INDEX ROWID | JOBS | 18780 | 183K| 1 (0)| 00:00:01 |
|* 36 | INDEX UNIQUE SCAN | JOBS_JOB_ID | 1 | | 0 (0)| 00:00:01 |
|* 37 | VIEW | VW_NSO_2 | 1 | 13 | 2 (0)| 00:00:01 |
|* 38 | CONNECT BY WITH FILTERING | | | | | |
PLAN_TABLE_OUTPUT
| 39 | TABLE ACCESS BY INDEX ROWID | AGENCY_ACCOUNTS | | | | |
|* 40 | INDEX UNIQUE SCAN | AGENCY_ACCOUNTS_ACCOUNT_ID | 1 | 5 | 1 (0)| 00:00:01 |
| 41 | NESTED LOOPS | | | | | |
| 42 | BUFFER SORT | | | | | |
| 43 | CONNECT BY PUMP | | | | | |
| 44 | TABLE ACCESS BY INDEX ROWID | AGENCY_ACCOUNTS | 1 | 10 | 2 (0)| 00:00:01 |
|* 45 | INDEX UNIQUE SCAN | AGENCY_ACCOUNTS_ACCOUNT_ID | 1 | | 1 (0)| 00:00:01 |
|* 46 | TABLE ACCESS FULL | AGENCY_ACCOUNTS | 1 | 10 | 2 (0)| 00:00:01 |
| 47 | BUFFER SORT | | 3 | 66 | 9 (0)| 00:00:01 |
| 48 | INLIST ITERATOR | | | | | |
|* 49 | INDEX RANGE SCAN | IDX$$_91CA0001 | 3 | 66 | 5 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
|* 50 | FILTER | | | | | |
|* 51 | CONNECT BY WITH FILTERING | | | | | |
| 52 | TABLE ACCESS BY INDEX ROWID | AGENCY_ACCOUNTS | | | | |
|* 53 | INDEX UNIQUE SCAN | AGENCY_ACCOUNTS_ACCOUNT_ID | 1 | 5 | 1 (0)| 00:00:01 |
| 54 | NESTED LOOPS | | | | | |
| 55 | BUFFER SORT | | | | | |
| 56 | CONNECT BY PUMP | | | | | |
| 57 | TABLE ACCESS BY INDEX ROWID | AGENCY_ACCOUNTS | 1 | 10 | 2 (0)| 00:00:01 |
|* 58 | INDEX UNIQUE SCAN | AGENCY_ACCOUNTS_ACCOUNT_ID | 1 | | 1 (0)| 00:00:01 |
|* 59 | TABLE ACCESS FULL | AGENCY_ACCOUNTS | 1 | 10 | 2 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
4 - filter( EXISTS (SELECT 0 FROM "APPLICATIONS" "A","JOBS" "SYS_ALIAS_1", (SELECT "ACCOUNT_ID" "$nso_col_1"
FROM "AGENCY_ACCOUNTS" "AGENCY_ACCOUNTS" WHERE "ACCOUNT_ID"=NULL) "VW_NSO_2","APPLICATION_FIELDS"
"APPLICATION_FIELDS" WHERE EXISTS (SELECT 0 FROM "AGENCY_ACCOUNTS" "AGENCY_ACCOUNTS" WHERE "ACCOUNT_ID"=NULL AND
("ACCOUNT_ID"=:B1)) AND ("FIELD_TYPE"='Checkbox' OR "FIELD_TYPE"='Radio' OR "FIELD_TYPE"='RadioHoriz' OR
"FIELD_TYPE"='RadioPara' OR "FIELD_TYPE"='Select' OR "FIELD_TYPE"='SelectM') AND "FIELD_NAME"=:B2 AND
"A"."ACCOUNT_ID"="$nso_col_1" AND "A"."JOB_ID"="JOB_ID" AND "A"."APPLICATION_ID"=:B3 AND
DECODE("A"."TRAY",'Incomplete','Incomplete','Complete')='Complete' AND "A"."DELETED"=0))
PLAN_TABLE_OUTPUT
5 - filter("FIELD_NAME"="AF"."FIELD_NAME" AND "INSTANCE"="AF"."FIELD_INSTANCE")
7 - access("AFO"."FIELD_NAME"="AF"."FIELD_NAME" AND "AFO"."FORM_ID"="AF"."FORM_ID")
8 - access("JF"."FORM_ID"="AF"."FORM_ID")
15 - filter("ACCOUNT_ID"=113346)
17 - access("ACCOUNT_ID"=113346)
22 - access("ACCOUNT_ID"=NULL)
23 - access("ACCOUNT_ID"=NULL)
24 - access("J"."ACCOUNT_ID"="$nso_col_1")
25 - access("JF"."JOB_ID"="$nso_col_1")
26 - filter("AF"."FIELD_TYPE"='Checkbox' OR "AF"."FIELD_TYPE"='Radio' OR "AF"."FIELD_TYPE"='RadioHoriz' OR
"AF"."FIELD_TYPE"='RadioPara' OR "AF"."FIELD_TYPE"='Select' OR "AF"."FIELD_TYPE"='SelectM')
PLAN_TABLE_OUTPUT FOR THE QUERY
28 - access("FIELD_VALUE"="AFO"."OPTION_TEXT")
29 - filter( EXISTS (SELECT 0 FROM "AGENCY_ACCOUNTS" "AGENCY_ACCOUNTS" WHERE "ACCOUNT_ID"=NULL AND
("ACCOUNT_ID"=:B1)))
33 - filter(DECODE("A"."TRAY",'Incomplete','Incomplete','Complete')='Complete' AND "A"."DELETED"=0)
34 - access("A"."APPLICATION_ID"=:B1)
36 - access("A"."JOB_ID"="JOB_ID")
37 - filter("A"."ACCOUNT_ID"="$nso_col_1")
38 - filter("ACCOUNT_ID"=113346)
40 - access("ACCOUNT_ID"=113346)
45 - access("ACCOUNT_ID"=NULL)
46 - access("ACCOUNT_ID"=NULL)
PLAN_TABLE_OUTPUT
49 - access("FIELD_NAME"=:B1 AND ("FIELD_TYPE"='Checkbox' OR "FIELD_TYPE"='Radio' OR "FIELD_TYPE"='RadioHoriz'
OR "FIELD_TYPE"='RadioPara' OR "FIELD_TYPE"='Select' OR "FIELD_TYPE"='SelectM'))
50 - filter("ACCOUNT_ID"=:B1)
51 - filter("ACCOUNT_ID"=:B1)
53 - access("ACCOUNT_ID"=:B1)
58 - access("ACCOUNT_ID"=NULL)
59 - access("ACCOUNT_ID"=NULL)
106 rows selected.TKPROF FOR THE QUERY
call count cpu elapsed disk query current rows
Parse 1 0.05 0.08 0 0 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 17 1040.45 1018.36 0 48926569 0 229
total 19 1040.50 1018.44 0 48926569 0 229
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 68
Rows Row Source Operation
229 SORT GROUP BY (cr=48926569 pr=0 pw=0 time=1018362160 us)
454 VIEW (cr=48926569 pr=0 pw=0 time=1018361382 us)
454 HASH GROUP BY (cr=48926569 pr=0 pw=0 time=1018360468 us)
185302 FILTER (cr=48926569 pr=0 pw=0 time=877249113 us)
1440799 TABLE ACCESS BY INDEX ROWID APPLICATION_DATA (cr=11309333 pr=0 pw=0 time=213262026 us)
82907114 NESTED LOOPS (cr=174051 pr=0 pw=0 time=166906535 us)
1790 HASH JOIN (cr=625 pr=0 pw=0 time=41617 us)
121 HASH JOIN (cr=310 pr=0 pw=0 time=11263 us)
39 NESTED LOOPS (cr=121 pr=0 pw=0 time=6894 us)
44 VIEW VW_NSO_3 (cr=75 pr=0 pw=0 time=6424 us)
44 HASH UNIQUE (cr=75 pr=0 pw=0 time=6333 us)
44 TABLE ACCESS BY INDEX ROWID JOBS (cr=75 pr=0 pw=0 time=3090 us)
52 NESTED LOOPS (cr=32 pr=0 pw=0 time=58177 us)
7 VIEW VW_NSO_1 (cr=23 pr=0 pw=0 time=1242 us)
7 CONNECT BY WITH FILTERING (cr=23 pr=0 pw=0 time=1212 us)
1 TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=3 pr=0 pw=0 time=185 us)
1 INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=2 pr=0 pw=0 time=100 us)(object id 60086)
6 NESTED LOOPS (cr=20 pr=0 pw=0 time=590 us)
7 BUFFER SORT (cr=0 pr=0 pw=0 time=248 us)
7 CONNECT BY PUMP (cr=0 pr=0 pw=0 time=95 us)
6 TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=20 pr=0 pw=0 time=314 us)
6 INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=14 pr=0 pw=0 time=188 us)(object id 60086)
0 TABLE ACCESS FULL AGENCY_ACCOUNTS (cr=0 pr=0 pw=0 time=0 us)
44 INDEX RANGE SCAN JOBS_ACCOUNT_ID (cr=9 pr=0 pw=0 time=280 us)(object id 60253)
39 INDEX RANGE SCAN JOBS_FORMS_JID_FID (cr=46 pr=0 pw=0 time=968 us)(object id 60299)
3579 TABLE ACCESS FULL APPLICATION_FIELDS (cr=189 pr=0 pw=0 time=14469 us)
32524 TABLE ACCESS FULL APPLICATION_FIELD_OPTIONS (cr=315 pr=0 pw=0 time=32629 us)
82905323 INDEX RANGE SCAN IDX$$_B8D40001 (cr=173426 pr=0 pw=0 time=83870105 us)(object id 60121)
185297 FILTER (cr=37617236 pr=0 pw=0 time=749052070 us)
185297 MERGE JOIN CARTESIAN (cr=37617196 pr=0 pw=0 time=742583363 us)
185297 NESTED LOOPS (cr=35393366 pr=0 pw=0 time=706772617 us)
1139675 NESTED LOOPS (cr=9180841 pr=0 pw=0 time=84949686 us)
1140164 TABLE ACCESS BY INDEX ROWID APPLICATIONS (cr=5760834 pr=0 pw=0 time=46774108 us)
1440060 INDEX UNIQUE SCAN APPS_APP_ID (cr=4320774 pr=0 pw=0 time=24380604 us)(object id 60107)
1139675 TABLE ACCESS BY INDEX ROWID JOBS (cr=3420007 pr=0 pw=0 time=28126171 us)
1139675 INDEX UNIQUE SCAN JOBS_JOB_ID (cr=2280328 pr=0 pw=0 time=14563524 us)(object id 60252)
185297 VIEW VW_NSO_2 (cr=26212525 pr=0 pw=0 time=613602445 us)
6866175 CONNECT BY WITH FILTERING (cr=26212525 pr=0 pw=0 time=614183170 us)
1139675 TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=3419025 pr=0 pw=0 time=24323520 us)
1139675 INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=2279350 pr=0 pw=0 time=13905212 us)(object id 60086)
6838050 NESTED LOOPS (cr=22793500 pr=0 pw=0 time=388173853 us)
7977725 BUFFER SORT (cr=0 pr=0 pw=0 time=178812411 us)
7977725 CONNECT BY PUMP (cr=0 pr=0 pw=0 time=57522094 us)
6838050 TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=22793500 pr=0 pw=0 time=179425469 us)
6838050 INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=15955450 pr=0 pw=0 time=113988543 us)(object id 60086)
0 TABLE ACCESS FULL AGENCY_ACCOUNTS (cr=0 pr=0 pw=0 time=0 us)
185297 BUFFER SORT (cr=2223830 pr=0 pw=0 time=24721936 us)
1208625 INLIST ITERATOR (cr=2223830 pr=0 pw=0 time=26470412 us)
1208625 INDEX RANGE SCAN IDX$$_91CA0001 (cr=2223830 pr=0 pw=0 time=18174073 us)(object id 60129)
2 FILTER (cr=40 pr=0 pw=0 time=1292 us)
2 CONNECT BY WITH FILTERING (cr=40 pr=0 pw=0 time=1276 us)
2 TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=6 pr=0 pw=0 time=86 us)
2 INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=4 pr=0 pw=0 time=46 us)(object id 60086)
10 NESTED LOOPS (cr=34 pr=0 pw=0 time=791 us)
12 BUFFER SORT (cr=0 pr=0 pw=0 time=369 us)
12 CONNECT BY PUMP (cr=0 pr=0 pw=0 time=127 us)
10 TABLE ACCESS BY INDEX ROWID AGENCY_ACCOUNTS (cr=34 pr=0 pw=0 time=376 us)
10 INDEX UNIQUE SCAN AGENCY_ACCOUNTS_ACCOUNT_ID (cr=24 pr=0 pw=0 time=242 us)(object id 60086)
0 TABLE ACCESS FULL AGENCY_ACCOUNTS (cr=0 pr=0 pw=0 time=0 us)Any help in tuning this query to run faster will be highly appreciated. I also noticed merge join certesian on the tkprof and explain plan. Any idea in takling that will also be appreciated.
Cheers,
AdeIt's quite hard to tune a query without seeing the table structure and available indices as well as relationship between each other but I'll try.
In your sql which you've given the table alias AD, you have this filter criteria
AND FIELD_NAME IN (
SELECT FIELD_NAME
FROM APPLICATION_FIELDS
WHERE FIELD_TYPE IN ('Checkbox','Radio','Select','SelectM', 'RadioHoriz','RadioPara')
) </br>
My question is, is FIELD_NAME a column in the APPLICATION_DATA table? If it is, try moving this clause outside of the current sub-query it is in, i.e. one more level up. It looks like you put them inside the wrong level.
I would also try converting this <COL> IN <SUB-QUERY> to a join, if that wouldn't affect the no. of rows returned. I can't tell for sure since I dont know the relationship between your tables.
It looks to me like you're doing this
SELECT <col list>
FROM ( SELECT -- LEVEL 1
FROM APPLICATION_DATA AD
WHERE AD.APPLICAITON_ID IN ( -- LEVEL 2
SELECT
FROM APPLICATIONS A
WHERE A.JOB_ID IN (
<SUBQUERY>
AND A.ACCOUNT_ID IN (
<SUBQUERY>
AND FIELDNAME IN ( -- SHOULD BE INSIDE LEVEL1
<SUBQUERY>
) AD,
( SELECT ....
) FD
WHERE .... -
Need a help with this max query
select SEARCH_ID, SEARCH_KEYWORD, COUNT, ASSET_TYPE from RELEVANCY_TABLE
where SEARCH_KEYWORD = 'search_keyword'
and ASSET_TYPE is not null
558
search_keyword
3
desk
559
search_keyword
7
table
I actually need to get the asset_type for which count is the maximum. In this case it should be 'table'. Any help?adfSonal wrote:
Is there any other way? I have to write this query in Java. So I will prefer avoiding rank or such functions.
Won't I get the desired result just using select, where, max, rownum clauses?
What do you mean by "I have to write this query in Java"? Any ways the query will be run against a Oracle DB, correct?
Any ways using ROWNUM
select *
from (
select search_id
, search_keyword
, count
, asset_type
from relevancy_table
where search_keyword = 'search_keyword'
and asset_type is not null
order
by count desc
where rownum = 1 -
Please need help on this SQL query ..
hi peers,
here is my situation :
Field1 Field2 Field3
xa group1 req_id1
xb group2 req_id2
xc group3 req_id3
xa group4 req_id4
xb group5 req_id5
i need to pull only the group3 record that comes right away after group2. Req_id's are in chronological order and groupx are texts
: i.e : req_id1 < req_id2 < req_id3 <.....
in other terms, i need the record that comes after the re cord flagged by group2..knowing that req_id3 is right after req_id2.
any thoughts ?
thanksHi,
Use the analytic LAG function to get a value from an earlier row:
WITH got_prev_field2 AS
SELECT field1, field2, field3
, LAG (field2) OVER (ORDER BY field3) AS prev_field2
FROM my_situation
SELECT field1, field2, field3
FROM got_prev_field2
WHERE prev_field2 = 'group2'
;Like all analytic functions, LAG is computed after the WHERE clause is applied. To use the value returned by LAG in a WHERE clause, we have to compute it in a sub-query.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data. -
Help with this SQL Query please
How many garments has each dressmaker constructed? You should give the number of garments and the name, house number and post code of each dressmaker.
tables; dressmaker contains D_NO, D_NAME, D_HOUSE_NO, D_POST_CODE
garment contains STYLE_NO, DESCRIPTION, LABOUR_COST, NOTIONS
quantities contains STYLE_Q, SIZE_Q, QUANTITY
The question title is multitableJOINS
I believe i have to use (garment.style_no=quantities.style_q) if it will work, im new to oracle & SQL so all the help i get i am grateful for, thankyou.Not to be a jerk, but this forum is really not intended for homework. Show a little effort, and try it out. Post some results, and maybe someone will help you out. Getting others to do your homework will not help you when you get out of school.
-
I have table a with columns ca1,ca2,ca3
and I have a table b with columns cb1, cb2,cb3
and a and b tables are related with a table's ca2 and b table's cb2
ca1 not null,ca2 can be null and cb2 is a not null column, cb3 not null
I want the output of ca1 and cb3
If ca2 is null then the output should contain ca1 value and null for cb3
If ca2 is not nulll comparing both it has to return cb3 value..
Could anybody help me in tis???
Thanks in advance..Hope I understood your requirement.
SELECT NVL(ca2,ca1) ca1, DECODE(ca2,NULL,ca2,cb3) cb3
FROM a, b
WHERE ca2=cb2 -
the following query returns me the correct no of rows:
select col1 from tab1 where
col1 like '%'||chr(32)||'%';
but i need to use my query in the following form and it doesn't return any row:
select col1 from tab1 where
col1 IN ('%'||chr(32)||'%');
what am I doing worng?
thanks in advance.Or in 10g (just recycling another example):
WITH t AS (SELECT 'OPTI1457' || CHR(32) col1
FROM dual
UNION
SELECT 'OPT123' || CHR(9)
FROM dual
UNION
SELECT 'OPTIM12345'
FROM dual
SELECT t.*
FROM t
WHERE REGEXP_LIKE(t.col1, CHR(32) || '|' || CHR(9))
; C. -
Please help with this inventory query
I want 2 display
item_num(segment1), description(mtl_system_items), on_hand_quantity, reservations, availability
pleseeeeeeeeeeeeeeeeeeeee help meeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
i guess we need to join these three tables ??
mtl_system_items
mtl_onhand_quantities_detail
mtl_reservationsAny body thereeeeeeeeeeeeeeeee :(((((((((
-
I am new to XML parsing. I am trying to convert xml data into relational data and need some help with this.
with table1 AS
(select xmltype(
'<BuildingData_AllResponse xmlns="http://secure.rutgers.edu/spacemanagement/">
<BuildingData_AllResult>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="BldgNum" type="xs:int" minOccurs="0"/>
<xs:element name="BldgName" type="xs:string" minOccurs="0"/>
<xs:element name="Campus" type="xs:string" minOccurs="0"/>
<xs:element name="StreetNo" type="xs:string" minOccurs="0"/>
<xs:element name="Address" type="xs:string" minOccurs="0"/>
<xs:element name="City" type="xs:string" minOccurs="0"/>
<xs:element name="State" type="xs:string" minOccurs="0"/>
<xs:element name="Zip" type="xs:string" minOccurs="0"/>
<xs:element name="Lat" type="xs:double" minOccurs="0"/>
<xs:element name="Long" type="xs:double" minOccurs="0"/>
<xs:element name="BldgDesc" type="xs:string" minOccurs="0"/>
<xs:element name="DeptName" type="xs:string" minOccurs="0"/>
<xs:element name="OrgID" type="xs:int" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
<BldgNum>1113</BldgNum>
<BldgName>Trinity House</BldgName>
<Campus>College Avenue</Campus>
<StreetNo>14</StreetNo>
<Address>Stone Street</Address>
<City>New Brunswick</City>
<State>NJ</State>
<Zip>08901</Zip>
<Lat>0</Lat>
<Long>0</Long>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
<BldgNum>1114</BldgNum>
<BldgName>Second Reformed Church</BldgName>
<Campus>College Avenue</Campus>
<StreetNo>100</StreetNo>
<Address>College Avenue</Address>
<City>New Brunswick</City>
<State>NJ</State>
<Zip>08901</Zip>
</Table>
<Table diffgr:id="Table3" msdata:rowOrder="2">
<BldgNum>3000</BldgNum>
<BldgName>Old Queens </BldgName>
<Campus>College Avenue</Campus>
<StreetNo>83</StreetNo>
<Address>SOMERSET STREET</Address>
<City>New Brunswick</City>
<State>NJ</State>
<Zip>08901-1281</Zip>
<Lat>40.4987594033</Lat>
<Long>-74.4462681014</Long>
<BldgDesc>Old Queens, originally called the Queens Building was erected in 1809. It was named after Charlotte Sophia the wife of George III, King of England. Charlotte Sophia was the youngest daughter of Charles Lewis, brother of Frederic, third Duke of Mecklenburg- Strelitz. She married George III on Sept.8, 1761 and was crowned on Sept. 22. Queen Charlotte was buried in St. George's Chapel, Winsdor. Source: Catalogue of Building and Place Names at Rutgers.</BldgDesc>
<DeptName>Division of Continuing Studies, VP</DeptName>
<OrgID>10003</OrgID>
</Table>
</NewDataSet>
</diffgr:diffgram>
</BuildingData_AllResult>
</BuildingData_AllResponse>'
) ruloc from dual
SELECT loc.*
FROM table1 PO,
XMLTable('/BuildingData_AllResponse/BuildingData_AllResult/diffgr:diffgram/NewDataSet/Table/BldgNum' PASSING PO.ruloc
COLUMNS "BldgNum" CHAR(10) PATH 'BldgNum',
"Name" CHAR(150) PATH 'BldgName',
"Campus" CHAR(50) PATH 'Campus') AS loc
In the above example, for simplicity I am pulling only 3 columns, but I am looking to pull all elements within <TABLE> as columns values.Hi,
Please always give your database version (select * from v$version), and the error message (if any).
The XML document you're querying defines multiple namespaces.
You have to declare those useful to resolve the XPath expression with the XMLNamespaces clause :
SELECT loc.*
FROM table1 PO,
XMLTable(
XMLNamespaces(
'urn:schemas-microsoft-com:xml-diffgram-v1' as "diffgr"
, 'http://secure.rutgers.edu/spacemanagement/' as "ns0"
, '/ns0:BuildingData_AllResponse/ns0:BuildingData_AllResult/diffgr:diffgram/NewDataSet/Table'
PASSING PO.ruloc
COLUMNS
"BldgNum" CHAR(10) PATH 'BldgNum',
"Name" CHAR(150) PATH 'BldgName',
"Campus" CHAR(50) PATH 'Campus'
) AS loc
; -
Help with a simple XMLTable query
Newbie Oracle XML developer here and need a little guidance on how to retrieve multiple records in an xmltype table using xmltable.
My document in the xmltype table looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<echoems:ECHOMetrics xmlns:echoems="http://myURL/myFilename.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://myURL/myFilename.xsd
http://myURL/myFilename.xsd">
<OrderMetrics>
<OrderItemMetric>
<requestor>user1</requestor>
<product>123</product>
</OrderItemMetric>
<OrderItemMetric>
<requestor>0.0.0.0</requestor>
<product>ABC</product>
</OrderItemMetric>
<OrderItemMetric>
<requestor>user3</requestor>
<product>XYZ</product>
</OrderItemMetric>
</OrderMetrics>
</echoems:ECHOMetrics>
I want to get all the products:
Product
123
ABC
XYZ
I know to use XMLTable but am not sure how exactly to write it. If just one record, the following worked fine:
select extractvalue
(object_value,
'echoems:ECHOMetrics/OrderMetrics/OrderItemMetric/product',
'xmlns:echoems="http://myURL/myFilename.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
) object_value
from echoXML;
But with multiple records, I'm trying XMLTable but can't get it right. Trying something like this:
select m.product
from echoXML3
xmlTable(
(object_value,'echoems:ECHOMetrics/OrderMetrics/OrderItemMetric/*','xmlns:echoems="http://myURL/myFilename.xsd')
passing object_value
COLUMNS
product varchar2(85) path 'product') m;
Thanks in advance for any help on this.One more question. Instead of putting the xml doc in the query, how do I reference it from the xmlType table?
You have:
select *
from xmltable
'//OrderMetrics/OrderItemMetric'
passing xmltype(
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<echoems:ECHOMetrics xmlns:echoems="http://myURL/myFilename.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://myURL/myFilename.xsd">
<OrderMetrics>
<OrderItemMetric>
<requestor>user1</requestor>
<product>123</product>
</OrderItemMetric>
<OrderItemMetric>
<requestor>0.0.0.0</requestor>
<product>ABC</product>
</OrderItemMetric>
<OrderItemMetric>
<requestor>user3</requestor>
<product>XYZ</product>
</OrderItemMetric>
</OrderMetrics>
</echoems:ECHOMetrics>')
columns
prod varchar2(20) path 'product',
requestor varchar(50) path 'requestor'
However, the xmlDocument is already in an xmlType table called echoXML3 based on the xsd. I'm just trying to query it to get a relational result set. How, in the query above, do I replace the hardcoded xmlDoc with the xmlDoc located in my xmlType table?
I thought maybe something like this but it's not working:
select *
from xmltable
'//OrderMetrics/OrderItemMetric'
passing xmltype(select value(e)
from echoXML3,
TABLE(xmlsequence(extract(object_value,'echoems:ECHOMetrics/OrderMetrics/OrderItemMetric/*','xmlns:echoems="http://myURL/myFilename.xsd'))) e)
columns
prod varchar2(20) path 'product',
requestor varchar(50) path 'requestor'
Message was edited by:
user619814 -
Need help in rewriting a sql query
Can any one please tell me if there is any utility that can help me correcting the sql I have I need to tune the query as its taking lot of time. I want to use some tool that will help me re-formating the query.
Any help in this regard will be highly appreciated.If you think that Oracle SQL Tuning Tools like SQL Tuning Advisor and SQL Access Advisor are not helping.
You might look into thrid party tools like Quest- SQL Navigator and TOAD.
But I don't advise this based on the following:
Re: Oracle Third Party Tools and Oracle Database
Oracle have enough tools of its own to satisfy the various needs.
Adith -
SQL Query Help - Is this possible or impossible????
Hi guys,
I need help with an SQL query that I'm trying to develop. It's very easy to explain but when trying to implement it, I'm struggling to achieve the results that I want.....
For example,
I have 2 tables
The first table is:
1) COMPANY create table company (manufacturer varchar2(25),
date_established date,
location varchar2(25) );My sample test date is:
insert into company values ('Ford', 1902, 'USA');
insert into company values ('BMW', 1910, 'Germany');
insert into company values ('Tata', 1922, 'India');The second table is:
2) MODELS create table models (manufacturer varchar(25),
model varchar2(25),
price number(10),
year date,
current_production_status varchar2(1) ) ;My sample test data is:
insert into models values ('Ford', 'Mondeo', 10000, 2010, 0);
insert into models values ('Ford', 'Galaxy', 12000, 2008, 0);
insert into models values ('Ford', 'Escort', 10000, 1992, 1);
insert into models values ('BMW', '318', 17500, 2010, 0);
insert into models values ('BMW', '535d', 32000, 2006, 0);
insert into models values ('BMW', 'Z4', 10000, 1992, 0);
insert into models values ('Tata', 'Safari', 4000, 1999, 0);
insert into models values ('Tata', 'Sumo', 5500, 1996, 1);
insert into models values ('Tata', 'Maruti', 3500, 1998, 0);And this is my query:
SELECT
com.manufacturer,
com.date_established,
com.location,
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.model),
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.price),
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.year),
mod.current_production_status
FROM
company com,
models mod
WHERE
mod.manufacturer = com.manufacturer
and com.manufacturer IN ('Ford', 'BMW', 'Tata')
and mod.current_production_status IN (1,0)
ORDER BY
mod.current_production_status DESCWhat I want the query to output is this:
com.manufacturer com.date_established com.location mod.model mod.price mod.year mod.current_production_status
Ford 1902 USA Escort 10000 1992 1
BMW 1910 Germany - - - 0
Tata 1922 India Sumo 5500 1998 1If current_production_status is 1 it means this particular model has been discontinued
If current_production_status is 0 it means the manufacturer does not have any discontinued models and all are in procuction.
The rule is only one record per manufacturer is allowed to have a current_production_status of 1 (so only one model from the selection the manufactuer offers is allowed to be discontinued).
So the query should output the one row where current_production_status is 1 for each manufacturer.
If for a given manufacturer there are no discontinued models and all have a current_production_status of 0 then ouput a SINGLE row that only includes the data from the COMPANY table (as above). The rest of the columns from the MODELS table should be populated with a '-' (hyphen).
My query as it is above will output all the records where current status is 1 or 0 like this
com.manufacturer com.date_established com.location mod.model mod.price mod.year mod.current_production_status
Ford 1902 USA Escort 10000 1992 1
Tata 1922 India Sumo 5500 1998 1
Ford 1902 USA - - - 0
Ford 1902 USA - - - 0
BMW 1910 Germany - - - 0
BMW 1910 Germany - - - 0
BMW 1910 Germany - - - 0
Tata 1922 India - - - 0
Tata 1922 India - - - 0However this is not what I want.
Any ideas how I can achieve the result I need?
Thanks!
P.S. Database version is '10.2.0.1.0'Hi Vishnu,
Karthiks query helped...
But this is the problem I am facing...
SELECT
com.manufacturer,
com.date_established,
com.location,
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.model),
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.price),
DECODE(nvl(mod.current_production_status, '0'), '0', '-', mod.year),
mod.current_production_status
FROM
company com,
models mod
WHERE
mod.manufacturer = com.manufacturer
and com.manufacturer = 'Ford'
and mod.current_production_status IN (1,0)
ORDER BY
mod.current_production_status DESCThe value of:
and com.manufacturer = 'Ford'will be dependent on front end user input....
When I run the query above I get all the rows where current_production_status is either 1 or 0.
I only require the rows where current_production_status is 1.
So if I amend it to look like this:
and mod.current_production_status = 1This works....
BUT if a user now passes in more than one manufacturer EG:
and com.manufacturer IN ('Ford', 'BMW')The query will only return the one row for Ford where current_production_status is 1. However because BMW has no models where current_production_status is 1 (all 3 are 0), I still want this to be output - as one row....
So like this:
com.manufacturer com.date_established com.location mod.model mod.price mod.year mod.current_production_status
Ford 1902 USA Escort 10000 1992 1
BMW 1910 Germany - - - 0So (hopefully you understand), I want both cases to be catered for.....whether a user enters one manufacturer or more than one...
Thanks you so much!
This is really driving me insane :-( -
How can i rewrite this query so it uses less inner joins (it causes my temp space to explode :) )
( SELECT Waardes.*, Tellers.Nr_Dataitems, Tellers.Nr_Details FROM
( SELECT extractvalue(Value(el_node), 'Node/Id') node,
extractvalue(Value(el_dataitem), 'Detail/Title') Node_Title,
extractvalue(Value(el_dataitem), 'Detail/Value') Node_Value
FROM PHILIPS_XML x,
TABLE (xmlsequence (extract (value(x), '/Tree/Node'))) el_node,
TABLE (xmlsequence (extract (value(el_node),
'Node/DataItems/DataItem/DetailSet/Detail'))) el_dataitem
) Waardes
INNER JOIN
SELECT A.Node, A.Nr_Dataitems, B.Nr_details FROM
SELECT extractvalue(Value(el_node), 'Node/Id') node,
count(extract(Value(aaa), 'DataItem')) Nr_Dataitems
FROM PHILIPS_XML x,
TABLE (xmlsequence (extract (value(x), '/Tree/Node'))) el_node,
TABLE (xmlsequence (extract (value(el_node),
'Node/DataItems/DataItem'))) aaa
GROUP BY extractvalue(Value(el_node), 'Node/Id')
) A
INNER JOIN
SELECT extractvalue(Value(el_node), 'Node/Id') node,
count(extract(Value(bbb), 'Detail')) Nr_details
FROM PHILIPS_XML x,
TABLE (xmlsequence (extract (value(x), '/Tree/Node'))) el_node,
TABLE (xmlsequence (extract (value(el_node),
'Node/DataItems/DataItem'))) aaa,
TABLE (xmlsequence (extract (value(aaa),
'DataItem/DetailSet/Detail' ))) bbb
GROUP BY extractvalue(Value(el_node), 'Node/Id')
) B
on A.node = B.NODE
) Tellers
ON Waardes.NODE = Tellers.NODE )1st Make sure all paths are absolute (start with '/', not just the node name).
Which release are you using.. Is the document based on an XMLSchema, if so, what annotations were used when registered the XML Schema ?. What does the explain plan look like... -
Please help me what other way i can tune this select query..
Hello Guru,
I have a select query which retrieve data from 10 tables and around 4 tables having 2-4 Lac record and rest are having 80,000 - 1 Lac record.
It is taking around 7-8 seconds to fetch 55000 record.
I was strictly told by the client that i should not use HINTS in my query. My query is below. Please help me what other way i can tune this select query..
select
CT.CUST_ID
,CT.ROMANISED_SURNAME
,CT.SURNAME
,CT.ROMANISED_GIVEN_NAME
,CT.GIVEN_NAME
,CT.ROMANISED_MIDDLE_NAME
,CT.MIDDLE_NAME
,CT.ROMANISED_NAME_SUFFIX
,CT.NAME_SUFFIX
,CT.ROMANISED_TITLE
,CT.TITLE
,CT.ROMANISED_NAME_INITIALS
,CT.NAME_INITIALS
,CT.NAME_TEXT
,CT.CUST_JRNY_ID
,RK.REMARK_TYPE
,RK.REMARK_ID+CT.CUST_ID as REMARK_ID
,RK.REMARK_STATUS
,RK.REMARK_TEXT
,RK.HOST_ONLY_IND
,RK.SUPERVISORY_IND
,RK.CUST_COMM_IND
,RK.REMARK_SEQ
,RK.REMARK_CODE
,RK.DEFAULT_CUST_REL_IND
,RK.DEFAULT_FLIGHT_SEG_REL_IND
,RK.IATA_CODE
,RK.ICAO_CODE
,CJ.RECORD_LOCATOR "SITA_RECORD_LOCATOR"
,Cjv.Record_Locator "ORIGINATOR_RECORD_LOCATOR"
,FS.TRAVELLING_GROUP_CODE
,CG.GROUP_NAME
FROM FLIGHT_LEG FL
,CUST_FLIGHT_LEG CFL
,CUST CT
,CUST_REMARK CTR
,REMARK RK
,FLIGHT_SEG_FLIGHT_LEG FSFL
,FLIGHT_SEG FS
,CUST_JRNY CJ
,CUST_JRNY_VERSION CJV
,CUST_GROUP CG
WHERE FL.OPR_FLIGHT_NUMBER = 1--I_OPR_FLIGHT_NUMBER
and FL.HISTORY_VERSION_NUMBER = 0
and FL.DEPARTURE_STATION_CODE = 'DEL'--I_DEPARTURE_STATION_CODE
and FL.DEPARTURE_DATETIME = TO_DATE('10-DEC-2012 18.45.00', 'DD-MON-YYYY HH24.MI.SS')
and FL.OPR_SERVICE_PROVIDER_CODE= 'AI'--i_opr_service_provider_code
and FL.OPR_FLIGHT_SUFFIX = 'A'--NVL(I_OPR_FLIGHT_SUFFIX, FL.OPR_FLIGHT_SUFFIX)
AND FL.FLIGHT_LEG_ID = CFL.FLIGHT_LEG_ID
AND CFL.CUST_ID = CT.CUST_ID
AND FL.FLIGHT_LEG_ID=FSFL.FLIGHT_LEG_ID
AND FSFL.FLIGHT_SEG_ID=FS.FLIGHT_SEG_ID
AND CT.CUST_ID = CTR.CUST_ID(+)
AND CTR.REMARK_ID = RK.REMARK_ID(+)
AND FL.CUST_JRNY_ID = CJ.CUST_JRNY_ID
and CJ.CUST_JRNY_ID = CJV.CUST_JRNY_ID
AND CG.CUST_JRNY_ID(+) = CT.CUST_JRNY_ID
AND CFL.HISTORY_VERSION_NUMBER = 0
AND CT.HISTORY_VERSION_NUMBER = 0
AND NVL(CTR.HISTORY_VERSION_NUMBER,0) = 0
AND NVL(RK.HISTORY_VERSION_NUMBER,0) = 0
AND FS.HISTORY_VERSION_NUMBER = 0
AND FSFL.HISTORY_VERSION_NUMBER = 0
-- AND CJ.HISTORY_VERSION_NUMBER = 0
and CJV.VERSION_NUMBER = 0 --- Need to check
AND NVL(CG.HISTORY_VERSION_NUMBER,0) = 0
order by CT.CUST_JRNY_ID,CT.CUST_ID;
The Tables having record:
select COUNT(*) from FLIGHT_LEG -----241756
select COUNT(*) from CUST_FLIGHT_LEG---632585
select COUNT(*) from CUST---240015
select COUNT(*) from CUST_REMARK---73724
select COUNT(*) from REMARK---73654
select COUNT(*) from FLIGHT_SEG_FLIGHT_LEG---241789
select COUNT(*) from FLIGHT_SEG----260004
select COUNT(*) from CUST_JRNY----74288
select COUNT(*) from CUST_JRNY_VERSION----74477
select COUNT(*) from CUST_GROUP----55819
Thanks,
HP..Plan hash value: 3771714931
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time | Pstart| Pstop |
| 0 | SELECT STATEMENT | | 10239 | 2949K| | 7515 (1)| 00:01:31 | | |
| 1 | SORT ORDER BY | | 10239 | 2949K| 3160K| 7515 (1)| 00:01:31 | | |
|* 2 | HASH JOIN | | 10239 | 2949K| | 6864 (1)| 00:01:23 | | |
| 3 | PARTITION HASH ALL | | 73687 | 1079K| | 417 (1)| 00:00:06 | 1 | 512 |
|* 4 | TABLE ACCESS FULL | CUST_JRNY_VERSION | 73687 | 1079K| | 417 (1)| 00:00:06 | 1 | 512 |
|* 5 | HASH JOIN | | 10239 | 2799K| | 6445 (1)| 00:01:18 | | |
| 6 | PARTITION HASH ALL | | 73654 | 863K| | 178 (1)| 00:00:03 | 1 | 512 |
| 7 | TABLE ACCESS FULL | CUST_JRNY | 73654 | 863K| | 178 (1)| 00:00:03 | 1 | 512 |
|* 8 | FILTER | | | | | | | | |
|* 9 | HASH JOIN RIGHT OUTER | | 10239 | 2679K| | 6267 (1)| 00:01:16 | | |
| 10 | PARTITION HASH ALL | | 55315 | 756K| | 137 (1)| 00:00:02 | 1 | 512 |
| 11 | TABLE ACCESS FULL | CUST_GROUP | 55315 | 756K| | 137 (1)| 00:00:02 | 1 | 512 |
|* 12 | FILTER | | | | | | | | |
|* 13 | HASH JOIN OUTER | | 10240 | 2540K| 2056K| 6129 (1)| 00:01:14 | | |
|* 14 | FILTER | | | | | | | | |
|* 15 | HASH JOIN RIGHT OUTER | | 10242 | 1930K| | 5531 (1)| 00:01:07 | | |
| 16 | INDEX FAST FULL SCAN | CUST_REMARK_PK | 73677 | 935K| | 190 (0)| 00:00:03 | | |
|* 17 | HASH JOIN | | 10257 | 1802K| | 5339 (1)| 00:01:05 | | |
|* 18 | HASH JOIN | | 10257 | 701K| | 3516 (1)| 00:00:43 | | |
|* 19 | HASH JOIN | | 3963 | 220K| | 2476 (1)| 00:00:30 | | |
|* 20 | HASH JOIN | | 3963 | 181K| | 1300 (1)| 00:00:16 | | |
| 21 | PARTITION HASH ALL | | 3963 | 131K| | 728 (1)| 00:00:09 | 1 | 512 |
|* 22 | TABLE ACCESS FULL | FLIGHT_LEG | 3963 | 131K| | 728 (1)| 00:00:09 | 1 | 512 |
|* 23 | INDEX FAST FULL SCAN| FLIGHT_SEG_FLIGHT_LEG_PK | 240K| 3059K| | 571 (1)| 00:00:07 | | |
| 24 | PARTITION HASH ALL | | 259K| 2531K| | 1175 (1)| 00:00:15 | 1 | 512 |
|* 25 | TABLE ACCESS FULL | FLIGHT_SEG | 259K| 2531K| | 1175 (1)| 00:00:15 | 1 | 512 |
| 26 | PARTITION HASH ALL | | 631K| 8011K| | 1037 (1)| 00:00:13 | 1 | 512 |
|* 27 | TABLE ACCESS FULL | CUST_FLIGHT_LEG | 631K| 8011K| | 1037 (1)| 00:00:13 | 1 | 512 |
| 28 | PARTITION HASH ALL | | 239K| 25M| | 1822 (1)| 00:00:22 | 1 | 512 |
|* 29 | TABLE ACCESS FULL | CUST | 239K| 25M| | 1822 (1)| 00:00:22 | 1 | 512 |
| 30 | PARTITION HASH ALL | | 73623 | 4385K| | 243 (1)| 00:00:03 | 1 | 512 |
| 31 | TABLE ACCESS FULL | REMARK | 73623 | 4385K| | 243 (1)| 00:00:03 | 1 | 512 |
Predicate Information (identified by operation id):
2 - access("CJ"."CUST_JRNY_ID"="CJV"."CUST_JRNY_ID")
4 - filter("CJV"."VERSION_NUMBER"=0)
5 - access("FL"."CUST_JRNY_ID"="CJ"."CUST_JRNY_ID")
8 - filter(NVL("CG"."HISTORY_VERSION_NUMBER",0)=0)
9 - access("CG"."CUST_JRNY_ID"(+)="CT"."CUST_JRNY_ID")
12 - filter(NVL("RK"."HISTORY_VERSION_NUMBER",0)=0)
13 - access("CTR"."REMARK_ID"="RK"."REMARK_ID"(+))
14 - filter(NVL("CTR"."HISTORY_VERSION_NUMBER",0)=0)
15 - access("CT"."CUST_ID"="CTR"."CUST_ID"(+))
17 - access("CFL"."CUST_ID"="CT"."CUST_ID")
18 - access("FL"."FLIGHT_LEG_ID"="CFL"."FLIGHT_LEG_ID")
19 - access("FSFL"."FLIGHT_SEG_ID"="FS"."FLIGHT_SEG_ID")
20 - access("FL"."FLIGHT_LEG_ID"="FSFL"."FLIGHT_LEG_ID")
22 - filter("FL"."DEPARTURE_STATION_CODE"='DEL' AND "FL"."DEPARTURE_DATETIME"=TO_DATE(' 2012-12-10 18:45:00', 'syyyy-mm-dd
hh24:mi:ss') AND "FL"."OPR_SERVICE_PROVIDER_CODE"='AI' AND "FL"."OPR_FLIGHT_NUMBER"=1 AND "FL"."OPR_FLIGHT_SUFFIX"='A' AND
"FL"."HISTORY_VERSION_NUMBER"=0)
23 - filter("FSFL"."HISTORY_VERSION_NUMBER"=0)
25 - filter("FS"."HISTORY_VERSION_NUMBER"=0)
27 - filter("CFL"."HISTORY_VERSION_NUMBER"=0)
29 - filter("CT"."HISTORY_VERSION_NUMBER"=0)
Maybe you are looking for
-
Urgent Issue with X230 Windows 8.1
I have upgraded my ThinkPad X230 to Windows 8.1. I get the following two message every time I start 1. Power Manager Group: An error occurred while loading a resource DLL. 2. Problem starting C:\Program Files (X86)\Thinkpad Utilities\PVMTR64V.DLL. A
-
I am using iPhoto 9.2.3. 1. How do I know if my library is "managed" or referenced"? 2. On the HD, "iPhoto library" appears twice: {user name} > pictures > iPhoto library Opens up all my photos. The other one: {user name} > iPhoto library
-
Hi, I'm looking for a an external hardrive for backing up my system and storing bulky files. I'd like a Firewire 400/800 drive preferably. The 'Maxtor OneTouch II 300GB Firewire 800/USB2.0' looks promising. Anyone got one? On a seperate matter are th
-
Turn off compatibility mode in Itunes
Ho do I turn off Compatibility mode in Itunes?
-
Made an error moving iTunes Music and need to create a folder ...
In August when I had to re-install iTunes, motherboard melt down, I thought I'd transferred iTunes Music back to the F drive, where I keep my media library, correctly. All downloads installed there and when I clicked Add Folder from my backup drive i