Very complex query
Hi!!
First of all sorry for the long thread but i really don't know how to explain otherwise. Please let me know if my question is not clear enough. I hope that this is possible.
I have the follwing query:
SELECT MAX(CASE WHEN t.plate_column=1 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c1,
MAX(CASE WHEN t.plate_column=2 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c2,
MAX(CASE WHEN t.plate_column=3 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c3,
MAX(CASE WHEN t.plate_column=4 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c4,
MAX(CASE WHEN t.plate_column=5 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c5,
MAX(CASE WHEN t.plate_column=6 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c6,
MAX(CASE WHEN t.plate_column=7 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c7,
MAX(CASE WHEN t.plate_column=8 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c8,
MAX(CASE WHEN t.plate_column=9 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c9,
MAX(CASE WHEN t.plate_column=10 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c10,
MAX(CASE WHEN t.plate_column=11 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c11,
MAX(CASE WHEN t.plate_column=12 THEN t.u_box_code || '-' || t.u_box_position ELSE null END) c12
FROM ( SELECT plate.plate_id,
aliquot.plate_column,
aliquot.plate_row,
sample_user.u_box_code,
sample_user.u_box_position,
sample_user.u_box_column,
sample_user.u_box_row
FROM lims_sys.plate, lims_sys.aliquot,lims_sys.sample,lims_sys.sample_user
WHERE plate.plate_id = aliquot.plate_id
AND aliquot.sample_id = sample.sample_id
AND sample.sample_id = sample_user.sample_id
AND plate.plate_id IN (874)
) t
GROUP BY t.plate_id,t.plate_row
ORDER BY t.plate_id,t.plate_row
[pre/]
The results of this query are:
[pre]
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12
F515-C07 F515-C08 F515-C09 F515-C10 F515-G05 F515-G06 F515-G07 F515-G08 F515-G09 F516-G10
F516-G11 F516-G12 F516-H01 F516-H02 F516-H03 F516-H04 F516-H05 F516-H06 F516-H07 N451-C04 N451-C05 N451-C06
N451-C07 N451-C08 N451-C09 N451-G02 N451-G03 N451-G04 N452-F11 N452-F12 N452-G01 N452-G02 N452-G03
N452-G04 N452-G05 N452-G06 N452-G07 N452-G08 N453-B05 N453-B06 N458-A11 N458-A12 N458-B03
N458-B04 N458-B05 N458-B06 N458-B07 N458-B08 N458-B09 N458-B10 N458-B11 N458-B12 N458-C01 N458-C02 N458-C03
N458-C04 N458-C05 N458-C06 N458-C07 N458-C08 N458-C09 N458-C10 N458-C11 N458-C12 N458-D01 N458-D02 N458-D03
N458-D04 N458-D05 N458-D06 N458-D07 N458-D08 N458-D09 N458-D10 N458-D11 N458-D12 N458-E03
N458-E04 N458-E05 N458-E06 N458-E07 N458-E08 N458-E09 N458-E10 N458-E11 N458-E12 N458-F01 N458-F02 N458-F03
[pre/]
What I actually want is that for each new u_box_code a status (C1status, C2status etc.) for every value change from 0 to 1 to 0 to 1 etc.Then the wanted results looks like:
[pre]
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C1status C2status C3status C4status C5status C6status C7status C8status C9status C10status C11status C12status
F515-C07 F515-C08 F515-C09 F515-C10 F515-G05 F515-G06 F515-G07 F515-G08 F515-G09 F516-G10 0 0 0 0 0 0 0 0 0 1
F516-G11 F516-G12 F516-H01 F516-H02 F516-H03 F516-H04 F516-H05 F516-H06 F516-H07 N451-C04 N451-C05 N451-C06 1 1 1 1 1 1 1 1 1 0 0 0
N451-C07 N451-C08 N451-C09 N451-G02 N451-G03 N451-G04 N452-F11 N452-F12 N452-G01 N452-G02 N452-G03 0 0 0 0 0 0 1 1 1 1 1
N452-G04 N452-G05 N452-G06 N452-G07 N452-G08 N453-B05 N453-B06 N458-A11 N458-A12 N458-B03 1 1 1 1 1 0 0 1 1 1
N458-B04 N458-B05 N458-B06 N458-B07 N458-B08 N458-B09 N458-B10 N458-B11 N458-B12 N458-C01 N458-C02 N458-C03 1 1 1 1 1 1 1 1 1 1 1 1
N458-C04 N458-C05 N458-C06 N458-C07 N458-C08 N458-C09 N458-C10 N458-C11 N458-C12 N458-D01 N458-D02 N458-D03 1 1 1 1 1 1 1 1 1 1 1 1
N458-D04 N458-D05 N458-D06 N458-D07 N458-D08 N458-D09 N458-D10 N458-D11 N458-D12 N458-E03 1 1 1 1 1 1 1 1 1 1
N458-E04 N458-E05 N458-E06 N458-E07 N458-E08 N458-E09 N458-E10 N458-E11 N458-E12 N458-F01 N458-F02 N458-F03 1 1 1 1 1 1 1 1 1 1 1 1
[pre/]
The most difficult part is the combination of the wanted results from thread complex query and this.
Conclusion:
The final results must be :
[pre]
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C1status C2status C3status C4status C5status C6status C7status C8status C9status C10status C11status C12status
C07-F515 C10-F515 G05-F515 G09-F515 G10-F516 0 0 0 0 0 0 0 0 0 1
G11-F516 G12-F516 H01-F516 H07-F516 C04-N451 C06-N451 1 1 1 1 1 1 1 1 1 0 0 0
C07-N451 C09-N451 G02-N451 G03-N451 G04-N451 F11-N452 F12-N452 G01-N452 G02-N452 G03-N452 0 0 0 0 0 0 1 1 1 1 1
G04-N452 G08-N452 B05-N453 B06-N453 A11-N458 A12-N458 B03-N458 1 1 1 1 1 0 0 1 1 1
B04-N458 B12-N458 C01-N458 C03-N458 1 1 1 1 1 1 1 1 1 1 1 1
C04-N458 C12-N458 D01-N458 D03-N458 1 1 1 1 1 1 1 1 1 1 1 1
D04-N458 D12-N458 E03-N458 1 1 1 1 1 1 1 1 1 1
E04-N458 E12-N458 F01-N458 F03-N458 1 1 1 1 1 1 1 1 1 1 1 1
[pre/]
The query of this without the statusses is:
[pre]
SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,plate_id,plate_row
FROM ( SELECT c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12, rownum r1,plate_id,plate_row
FROM ( SELECT MAX(CASE WHEN t.plate_column=1 THEN t.u_box_position|| '-' || t.u_box_code ELSE null END) c1,
MAX(CASE WHEN t.plate_column=2 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c2,
MAX(CASE WHEN t.plate_column=3 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c3,
MAX(CASE WHEN t.plate_column=4 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c4,
MAX(CASE WHEN t.plate_column=5 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c5,
MAX(CASE WHEN t.plate_column=6 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c6,
MAX(CASE WHEN t.plate_column=7 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c7,
MAX(CASE WHEN t.plate_column=8 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c8,
MAX(CASE WHEN t.plate_column=9 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c9,
MAX(CASE WHEN t.plate_column=10 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c10,
MAX(CASE WHEN t.plate_column=11 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c11,
MAX(CASE WHEN t.plate_column=12 THEN t.u_box_position || '-' || t.u_box_code ELSE null END) c12,
t.plate_id,
t.plate_row
FROM ( SELECT plate.plate_id,
aliquot.plate_column,
aliquot.plate_row,
sample_user.u_box_code,
sample_user.u_box_position,
sample_user.u_box_column,
sample_user.u_box_row
FROM lims_sys.plate, lims_sys.aliquot,lims_sys.sample,lims_sys.sample_user
WHERE plate.plate_id = aliquot.plate_id
AND aliquot.sample_id = sample.sample_id
AND sample.sample_id = sample_user.sample_id
AND plate.plate_id IN (874)
) t,
( SELECT min(u_box_column) min_col ,max(u_box_column) max_col,u_box_row,u_box_code,plate_row
FROM ( SELECT plate.plate_id,
aliquot.plate_column,
aliquot.plate_row,
sample_user.u_box_code,
sample_user.u_box_position,
sample_user.u_box_column,
sample_user.u_box_row
FROM lims_sys.plate, lims_sys.aliquot,lims_sys.sample,lims_sys.sample_user
WHERE plate.plate_id = aliquot.plate_id
AND aliquot.sample_id = sample.sample_id
AND sample.sample_id = sample_user.sample_id
AND plate.plate_id IN (874))
GROUP BY u_box_row,u_box_code,plate_row
) s
WHERE t.u_box_column in (min_col,max_col)
AND t.u_box_row = s.u_box_row
AND t.plate_row = s.plate_row
GROUP BY t.plate_id,t.plate_row
ORDER BY t.plate_id,t.plate_row)
) a,
( SELECT rownum r2
FROM all_objects
WHERE rownum <9
) b
WHERE a.r1(+) = b.r2
[pre/]
Hi,
Your query does not work. when i exttract the subqueries i get the message "not a group by expression" on plate_column with subquery:I fired the below query posted by me and got the output. Its not giving any error for me.
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 S1 S2 S3 S4 S5 S6 S7 S8 S9 S10 S11 S12
F515-C07 F515-C10 F515-G05 F515-G09 F516-G10 0 0 0 0 0 0 0 0 0 1
F516-G11 F516-G12 F516-H01 F516-H07 N451-C04 N451-C06 1 1 1 1 1 1 1 1 1 0 0 0
N451-C07 N451-C09 N451-G02 N451-G03 N451-G04 N452-F11 N452-F12 N452-G01 N452-G02 N452-G03 0 0 0 0 0 0 1 1 1 1 1
N452-G04 N452-G08 N453-B05 N453-B06 N458-A11 N458-A12 N458-B03 1 1 1 1 1 0 0 1 1 1
N458-B04 N458-B12 N458-C01 N458-C03 1 1 1 1 1 1 1 1 1 1 1 1
N458-C04 N458-C12 N458-D01 N458-D03 1 1 1 1 1 1 1 1 1 1 1 1
N458-D04 N458-D12 N458-E03 1 1 1 1 1 1 1 1 1 1
N458-E04 N458-E12 N458-F01 N458-F03 1 1 1 1 1 1 1 1 1 1 1 1
Also the subquery you posted me saying that it gives error also gives the output.
select rownum rn,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11,s12
from (
select (select status from stat where box_code = st1) s1,
(select status from stat where box_code = st2) s2,
(select status from stat where box_code = st3) s3,
(select status from stat where box_code = st4) s4,
(select status from stat where box_code = st5) s5,
(select status from stat where box_code = st6) s6,
(select status from stat where box_code = st7) s7,
(select status from stat where box_code = st8) s8,
(select status from stat where box_code = st9) s9,
(select status from stat where box_code = st10) s10,
(select status from stat where box_code = st11) s11,
(select status from stat where box_code = st12) s12
from (
select MAX(CASE WHEN plate_column=1 THEN t.u_box_code ELSE null END) st1,
MAX(CASE WHEN plate_column=2 THEN t.u_box_code ELSE null END) st2,
MAX(CASE WHEN plate_column=3 THEN t.u_box_code ELSE null END) st3,
MAX(CASE WHEN plate_column=4 THEN t.u_box_code ELSE null END) st4,
MAX(CASE WHEN plate_column=5 THEN t.u_box_code ELSE null END) st5,
MAX(CASE WHEN plate_column=6 THEN t.u_box_code ELSE null END) st6,
MAX(CASE WHEN plate_column=7 THEN t.u_box_code ELSE null END) st7,
MAX(CASE WHEN plate_column=8 THEN t.u_box_code ELSE null END) st8,
MAX(CASE WHEN plate_column=9 THEN t.u_box_code ELSE null END) st9,
MAX(CASE WHEN plate_column=10 THEN t.u_box_code ELSE null END) st10,
MAX(CASE WHEN plate_column=11 THEN t.u_box_code ELSE null END) st11,
MAX(CASE WHEN plate_column=12 THEN t.u_box_code ELSE null END) st12
from t
group by plate_id,plate_row
o/p :
RN S1 S2 S3 S4 S5 S6 S7 S8 S9 S10 S11 S12
1 0 0 0 0 0 0 0 0 0 1
2 1 1 1 1 1 1 1 1 1 0 0 0
3 0 0 0 0 0 0 1 1 1 1 1
4 1 1 1 1 1 0 0 1 1 1
5 1 1 1 1 1 1 1 1 1 1 1 1
6 1 1 1 1 1 1 1 1 1 1 1 1
7 1 1 1 1 1 1 1 1 1 1
8 1 1 1 1 1 1 1 1 1 1 1 1
Rgds
Sandesh
Similar Messages
-
Sql-need help very complex query
Hi all,
I have a big SELECT statement hen i run it,it gives ORA-22813 error.
Ora-22813:- The Collection value from one of the inner sub queries has exceeded the system limits and hence this error.
It would be really great if you all could please help me in re-writing the inner join SELECTS in any other way so that we can process large amout of data.
SELECT * FROM (SELECT
mcat.CATALOG_ITEM_ID,
mcat.CATALOG_ITEM_NAME ,
mcat.DESCRIPTION,
mcat.CATALOG_ITEM_TYPE,
mcat.DELIVERY_METHOD,
XMLElement("TRAINING_PLAN",XMLAttributes( TP.TPLAN_ID as "id" ),
XMLELEMENT("COMPLETE_QUANTITY", TP.COMPLETE_QUANTITY),
XMLELEMENT("COMPLETE_UNIT", TP.COMPLETE_UNIT),
XMLElement("TOTAL_CREDITS", TP.numberOfCredits ),
XMLELEMENT("IS_CREDIT_BASED", TP.IS_CREDIT_BASED),
XMLELEMENT("IS_FOR_CERT", TP.IS_FOR_CERT),
XMLELEMENT("ACCREDIT_ORG_NAME", TP.ACCRED_ORG_NAME),
XMLELEMENT("ACCREDIT_ORG_ID", TP.accredit_org_id ),
XMLElement("OBJECTIVE_LIST", TP.OBJECTIVE_LIST )
).extract('/').getClobVal() AS PLAN_LIST
FROM
student_master_catalog mcat
INNER JOIN
(SELECT stu_tp.TPLAN_ID,
stu_tp.COMPLETE_QUANTITY,
stu_tp.COMPLETE_UNIT,
stu_tp.TPLAN_XML_DATA.extract('//numberOfCredits/text()').getStringVal() as numberOfCredits,
stu_tp.IS_CREDIT_BASED,
stu_tp.IS_FOR_CERT,
stu_oa.ACCRED_ORG_NAME,
stu_tp.TPLAN_XML_DATA.extract('//accreditingOrg/text()').getStringVal() as accredit_org_id,
objective_list.OBJECTIVE_LIST
FROM
student_training_catalog stu_tp
LEFT OUTER JOIN
stu_accrediting_org stu_oa on stu_tp.TPLAN_XML_DATA.extract('//accreditingOrg/text()').getStringVal() = stu_oa.ACCRED_ORG_ID
INNER JOIN
(SELECT *
FROM
(SELECT
stu_tpo.TPLAN_ID AS OBJECTIVE_TPLAN_ID,
XMLAgg(
XMLElement("OBJECTIVE",
XMLElement("OBJECTIVE_ID",stu_tpo.T_OBJECTIVE_ID ),
XMLElement("OBJECTIVE_NAME",stu_to.T_OBJECTIVE_NAME ),
XMLElement("OBJECTIVE_REQUIRED_CREDITS_OR_ACTIVITIES",stu_tpo.REQUIRED_CREDITS ),
XMLElement("ITEM_ORDER", stu_tpo.ITEM_ORDER ),
XMLElement("ACTIVITY_LIST", activity_list.ACTIVITY_LIST )
) as OBJECTIVE_LIST
FROM
stu_TP_OBJECTIVE stu_tpo
INNER JOIN
stu_TRAINING_OBJECTIVE stu_to ON stu_tpo.T_OBJECTIVE_ID = stu_to.T_OBJECTIVE_ID
INNER JOIN
(SELECT *
FROM
(SELECT stu_toa.T_OBJECTIVE_ID AS ACTIVITY_TOBJ_ID, XMLAgg(
XMLElement("ACTIVITY",
XMLElement("ACTIVITY_ID",stu_toa.ACTIVITY_ID ),
XMLElement("CATALOG_ID",COALESCE(stu_c.CATALOG_ID, COALESCE( stu_e.CATALOG_ID, stu_t.CATALOG_ID ) ) ),
XMLElement("CATALOG_ITEM_ID",COALESCE(stu_c.CATALOG_ITEM_ID, COALESCE( stu_e.CATALOG_ITEM_ID, stu_t.CATALOG_ITEM_ID ) ) ),
XMLElement("DELIVERY_METHOD",COALESCE(stu_c.DELIVERY_METHOD, COALESCE( stu_e.DELIVERY_METHOD, stu_t.DELIVERY_METHOD ) ) ),
XMLElement("ACTIVITY_NAME",COALESCE(stu_c.COURSE_NAME, COALESCE( stu_e.EVENT_NAME, stu_t.TEST_NAME ) ) ),
XMLElement("ACTIVITY_TYPE",initcap( stu_toa.ACTIVITY_TYPE ) ),
XMLElement("IS_REQUIRED",stu_toa.IS_REQUIRED ),
XMLElement("IS_PREFERRED",stu_toa.IS_PREFERRED ),
XMLElement("NUMBER_OF_CREDITS",stu_lac.CREDIT_HOURS),
XMLElement("ITEM_ORDER", stu_toa.ITEM_ORDER )
)) as ACTIVITY_LIST
FROM stu_TRAIN_OBJ_ACTIVITY stu_toa
LEFT OUTER JOIN
SELECT distinct lac.LEARNING_ACTIVITY_ID, lac.CREDIT_HOURS
FROM student_training_catalog tp
INNER JOIN stu_TP_OBJECTIVE tpo on tp.TPLAN_ID = tpo.TPLAN_ID
INNER JOIN stu_TRAIN_OBJ_ACTIVITY toa on tpo.T_OBJECTIVE_ID = toa.T_OBJECTIVE_ID
INNER JOIN stu_LEARNINGACTIVITY_CREDITS lac on lac.LEARNING_ACTIVITY_ID = toa.ACTIVITY_ID and tp.TPLAN_XML_DATA.extract ('//accreditingOrg/text()').getStringVal() = lac.ACC_ORG_ID
where tp.tplan_id ='************'*
) stu_lac ON stu_lac.LEARNING_ACTIVITY_ID = stu_toa.ACTIVITY_ID ------>This Select returns correct no. of rows
This below SELECT inside the LEFT OUTER JOIN is the Problem.it returns too much because 3 tables are joined directly without any value qualification.
LEFT OUTER JOIN
( SELECT ch.COURSE_HISTORY_ID, stu_c.COURSE_NAME, mca.catalog_item_id, mca.catalog_id, mca.delivery_method
FROM stu_COURSE stu_c
LEFT OUTER JOIN stu_course_history ch on stu_c.course_id = ch.ch_course_id -
If i can qualify here with ch.ch_course_id = stu_toa.ACTIVITY_ID (stu_toa.ACTIVITY_ID from the above select with correct no. of rows )
Here,i get erros because i cant access outside values inside a left outer join
LEFT OUTER JOIN student_master_catalog mca on ch.course_history_id = mca.catalog_item_id
) stu_c ON stu_c.COURSE_HISTORY_ID = stu_toa.ACTIVITY_ID
LEFT OUTER JOIN
(SELECT stu_e.EVENT_ID, stu_e.EVENT_NAME, mca.catalog_item_id, mca.catalog_id, mca.delivery_method FROM stu_EVENTS stu_e LEFT OUTER JOIN student_master_catalog mca on stu_e.event_Id = mca.catalog_item_id ) stu_e ON stu_e.EVENT_ID = stu_toa.ACTIVITY_ID
LEFT OUTER JOIN
(SELECT stu_t.TEST_HISTORY_ID, stu_t.TEST_NAME, mca.catalog_item_id, mca.catalog_id, mca.delivery_method FROM stu_TEST_HISTORY stu_t LEFT OUTER JOIN student_master_catalog mca on stu_t.test_history_id = mca.catalog_item_id) stu_t ON stu_t.test_history_id = stu_toa.ACTIVITY_ID
GROUP BY stu_toa.T_OBJECTIVE_ID) ) activity_list ON activity_list.ACTIVITY_TOBJ_ID = stu_tpo.T_OBJECTIVE_ID
GROUP BY stu_tpo.TPLAN_ID) ) objective_list ON objective_list.OBJECTIVE_TPLAN_ID = stu_tp.TPLAN_ID
)TP ON TP.TPLAN_ID = mcat.CATALOG_ITEM_ID
WHERE
mcat.CATALOG_ITEM_ID = '*****************' and mcat.CATALOG_ORG_ID = '********')
Edited by: user10817659 on Jan 16, 2009 12:49 PM
Edited by: user10817659 on Jan 16, 2009 12:51 PM
Edited by: user10817659 on Jan 16, 2009 12:52 PM
Edited by: user10817659 on Jan 16, 2009 12:54 PMSELECT distinct lac.LEARNING_ACTIVITY_ID, lac.CREDIT_HOURS
FROM student_training_catalog tp
INNER JOIN stu_TP_OBJECTIVE tpo on tp.TPLAN_ID = tpo.TPLAN_ID
INNER JOIN stu_TRAIN_OBJ_ACTIVITY toa on tpo.T_OBJECTIVE_ID = toa.T_OBJECTIVE_ID
INNER JOIN stu_LEARNINGACTIVITY_CREDITS lac on lac.LEARNING_ACTIVITY_ID = toa.ACTIVITY_ID and tp.TPLAN_XML_DATA.extract ('//accreditingOrg/text()').getStringVal() = lac.ACC_ORG_ID
where tp.tplan_id ='************'*
*) stu_lac ON stu_lac.LEARNING_ACTIVITY_ID = stu_toa.ACTIVITY_ID ------>This Select returns 117 records which is correct no. of rows*
This below SELECT inside the LEFT OUTER JOIN is the Problem.it returns too much because 3 tables are joined directly without any value qualification.
LEFT OUTER JOIN
( SELECT ch.COURSE_HISTORY_ID, stu_c.COURSE_NAME, mca.catalog_item_id, mca.catalog_id, mca.delivery_method
FROM stu_COURSE stu_c
LEFT OUTER JOIN stu_course_history ch on stu_c.course_id = ch.ch_course_id -
If i can qualify here with ch.ch_course_id = stu_toa.ACTIVITY_ID (stu_toa.ACTIVITY_ID from the above select with correct no. of rows )
Here,i get erros because i cant access outside values inside a left outer join
LEFT OUTER JOIN student_master_catalog mca on ch.course_history_id = mca.catalog_item_id
**Here this query gives me huge 22k*
*) stu_c ON stu_c.COURSE_HISTORY_ID = stu_toa.ACTIVITY_ID--->This join queriess 117 records from top with 22k records found by the nested selected and fails processing.*
then here above
LEFT OUTER JOIN
(SELECT stu_e.EVENT_ID, stu_e.EVENT_NAME, mca.catalog_item_id, mca.catalog_id, mca.delivery_method FROM stu_EVENTS stu_e LEFT OUTER JOIN student_master_catalog mca on stu_e.event_Id = mca.catalog_item_id ) stu_e ON stu_e.EVENT_ID = stu_toa.ACTIVITY_ID
LEFT OUTER JOIN
(SELECT stu_t.TEST_HISTORY_ID, stu_t.TEST_NAME, mca.catalog_item_id, mca.catalog_id, mca.delivery_method FROM stu_TEST_HISTORY stu_t LEFT OUTER JOIN student_master_catalog mca on stu_t.test_history_id = mca.catalog_item_id) stu_t ON stu_t.test_history_id = stu_toa.ACTIVITY_ID
GROUP BY stu_toa.T_OBJECTIVE_ID) ) activity_list ON activity_list.ACTIVITY_TOBJ_ID = stu_tpo.T_OBJECTIVE_ID
GROUP BY stu_tpo.TPLAN_ID) ) objective_list ON objective_list.OBJECTIVE_TPLAN_ID = stu_tp.TPLAN_ID
)TP ON TP.TPLAN_ID = mcat.CATALOG_ITEM_ID
WHERE
mcat.CATALOG_ITEM_ID = '*****************' and mcat.CATALOG_ORG_ID = '********')
We’re getting the following error:*
ORA-22813: operand value exceeds system limits*
Cause: Object or Collection value was too large.*
The size of the value might have exceeded 30k in a SORT context, or the size might be too big for available memory.* -
Creating a complex query in CR designer
I'm running Crystal Reports XI Release 2 on Windows NT. I have a very complex query and cannot figure out how to make it work in CR. Basically, I've got a table A with fields containing reference codes that may or may not be present. I created a SQL query that imbeds select statements in the select fields (the field names between SELECT and FROM). So, it would read something like SELECT field1, field2, (select Code_Name from REF_Table where REF_Table.code=TableA.code and TableA.code is not null) FROM TableA. That works great in SQL Developer.
In CR, I tried a command field but that produces a cartesian set where I get a line for every record that has the field for each and every record in TableA. No good. I just want the name for the value in the TableA record translated to the name if it is present. Is there a way to do this in CR? I used to be able to edit the SQL directly in CR but now it won't let me do that. Anyone know a better way to solve this kind of problem?I'm not an Oracle guy so I don't know what you can or can't do in PL SQL.
I'm surprised that you are getting different results between SQL Developer and the CR Command. CR should pass the query, exactly as it's written back to the server, same as SQL Dev.
IN T-SQL...If you want to make sure that the sub-query is returning only 1 row, try placing "TOP 1" behind the SELECT.
(select top 1 loc.location_name
from ref_location loc
where loc.location_num=obd.location_num and odb.location_num is not null)
I'm thinking that Oracle doesn't have the the TOP N option but it does have a rownum feature. So maybe...
(SELECT x.location_name
FROM (
select loc.location_name
from ref_location loc
where loc.location_num=obd.location_num and odb.location_num is not null) x
WHERE rownum = 1
ORDER BY rownum)
This is assuming that Oracle allows you to use ORDER BY in a sub-query. T-SQL only allows it if it's used in conjunction with TOP N...
Just a thought,
Jason -
BEx query design: defining VERY complex filters
Hello to everybody,
I have to build a query with a very complex filter and need some help.
I got two main characteristics (Cost Center and Cost Element) and I want to list all data in my InfoCube EXCEPT when theres certain combinations of this characteristics.
It can be resumed as follows:
If Cost Center = A AND Cost Element = Z
OR
Cost Center = B AND Cost Element = Y
dont show results
Else, show results
Ive tried using hierarchies but it adds the results in only one line per hierarchy listed and I havent been able to unfold data.
Ive tried using conditions but :
If I use only one structure (the one with key figures that Im already using), it requires all the cells to include a key figure, and I need some cells to define the filtering values that will be exceptions; those are defined as characteristics, hence the problem.
If I use variables instead of fix values to define the exceptions, I havent been able to enter a text as predefined value (Cost Center is alphanumerically codified).
If I use two structures, the second one is a characteristics structure, so the problems listed above are solved, but then I have to define a condition based on both structures... and havent found a way to make it work!
Ive been thinking on using exception cells but it wont work, will it?
Maybe creating more simple queries and glueing them together would work, but can it be done?
And last, maybe I could create a new InfoObject concatenating Cost Center and Cost Element, and a table with the filtering values to serve as reference, but I dont know how to do it and if it will work.
Thanks in advance!Hi A, how's it going?
In the rows I`ve put Cost Center and Cost Element. In the columns a lot of key figures (about 8).
I think excluding the exception values in the filtering area is not possible because the requirements are so complex It's necessary to use a estructure and It can't be placed in this area.
Excluding those values from selection... well, I've tried everything I could think of! Any idea?
J.
Message was edited by: Jaume Saumell -
How can I make this complex query?
I donât know how can I do this complex query...
I have the tables Customers, Vehicles and Models. A customer can have 0-N Vehicles and a vehicle have 1 Model.
Customers: ID_Customer (PK), Name, LastName...
Vehicles: ID_Vehicle (PK), ID_Customer, ID_Model, date_sale
Models: ID_Modelo (PK), Model_Name
Then I need to know the buy average time (date_sale) between 2 models (ID_Model)
For example I have these Vehicles:
ID_Vehicle (PK)...ID_Customer...ID_Model....date_sale
1................................123....................1.............21/05/2000
2................................459....................3............ 16/08/2000
3................................123....................2............ 28/06/2001
4................................516....................1............ 09/09/2001
5................................459....................4............ 18/10/2002
6................................516....................2............ 20/12/2002
If I want to know the buy average time (date_sale) between ID_Model 1 and ID_Model 2 it is. (403 + 467) / 2 = 435 days !!!
* Customer 123 (model 1 to 2): 25/06/2001 â 21/05/2000 ==> 403 days
* Customer 516 (model 1 to 2): 01/12/2002 â 01/05/2000 ==> 467 days.
I need to do the query for all the combination models. For example If I have the Models 1, 2 and 3 I need to obtain:
Source_Model.....Target_Model....Buy_Average_Time
1......................................1....................... xxx days
1......................................2....................... xxx days
1......................................3....................... xxx days
2......................................1....................... xxx days
2......................................2....................... xxx days
2......................................3....................... xxx days
3......................................1....................... xxx days
3......................................2....................... xxx days
3......................................3....................... xxx days
If it is necessary I could create a table with the fields Source_Model, Target_Model and Buy_Average_Time for accelerate the query time response.
But how can I do this query??????????
Thanks very much!!!Hi,
Thanks for your reply but It is not correct for me because the Customer could have several vehicles in her life.
If I make the query with my examples data it is OK
ID_Vehicle (PK)...ID_Customer...ID_Model....date_sale
1................................123....................1.............21/05/2000
2................................459....................3............ 16/08/2000
3................................123....................2............ 28/06/2001
4................................516....................1............ 09/09/2001
5................................459....................4............ 18/10/2002
6................................516....................2............ 20/12/2002
select v1.id_model as source_model,
v2.id_model as target_model,
avg (v2.date_sale - v1.date_sale) || ' days' as buy_average_time
from vehicles v1,
vehicles v2
where v1.id_customer = v2.id_customer
and v2.date_sale > v1.date_sale
group by v1.id_model, v2.id_model;
I received:
SOURCE_MODEL TARGET_MODEL BUY_AVERAGE_TIME
..............1................................2.....................................435 days
..............3................................4.....................................793 days
But If I insert a new vehicle:
ID_Vehicle (PK)...ID_Customer...ID_Model....date_sale
7................................516....................3............ 16/08/2003
And I execute the query now I receive:
SOURCE_MODEL TARGET_MODEL BUY_AVERAGE_TIME
..............1................................2...........................435 days
..............1................................3...........................706 days
..............2................................3...........................239 days
..............3................................4...........................793 days
It is NO correct because I donât have the 1 (Source_model) to 3 (Target_model) combination in the Vehicles data!!! (I have 1 to 2 and 2 to 3 but NO 1 to 3!!!!).
Then the query is not OK for me!
Besides If I modify the Vehicle 7 data and put ID_Model = 2:
ID_Vehicle (PK)...ID_Customer...ID_Model....date_sale
7................................516....................2............ 16/08/2003
I received:
SOURCE_MODEL TARGET_MODEL BUY_AVERAGE_TIME
..............1................................2...........................525,33 days
..............2................................2...........................239 days
..............3................................4...........................793 days
It is NO correct because the BUY_AVERAGE_TIME for 1 to 2 combination is 435 days (the same of the original query) because I aggregate only the combination 2 to 2 with the ID_VEHICLE 7
Below I attach the examples scripts.
Could you help me with this query, please?
Thanks very much!
CREATE TABLE VEHICLES
ID_VEHICLE NUMBER(9),
ID_CUSTOMER NUMBER(9),
ID_MODEL NUMBER(9),
DATE_SALE DATE
TABLESPACE USERS
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
LOGGING
NOCACHE
NOPARALLEL;
INSERT INTO VEHICLES ( ID_VEHICLE, ID_CUSTOMER, ID_MODEL, DATE_SALE ) VALUES (
1, 123, 1, TO_Date( '05/21/2000 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO VEHICLES ( ID_VEHICLE, ID_CUSTOMER, ID_MODEL, DATE_SALE ) VALUES (
2, 459, 3, TO_Date( '08/16/2000 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO VEHICLES ( ID_VEHICLE, ID_CUSTOMER, ID_MODEL, DATE_SALE ) VALUES (
3, 123, 2, TO_Date( '06/28/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO VEHICLES ( ID_VEHICLE, ID_CUSTOMER, ID_MODEL, DATE_SALE ) VALUES (
4, 516, 1, TO_Date( '09/09/2001 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO VEHICLES ( ID_VEHICLE, ID_CUSTOMER, ID_MODEL, DATE_SALE ) VALUES (
5, 459, 4, TO_Date( '10/18/2002 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO VEHICLES ( ID_VEHICLE, ID_CUSTOMER, ID_MODEL, DATE_SALE ) VALUES (
6, 516, 2, TO_Date( '12/20/2002 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
INSERT INTO VEHICLES ( ID_VEHICLE, ID_CUSTOMER, ID_MODEL, DATE_SALE ) VALUES (
7, 516, 2, TO_Date( '08/16/2003 12:00:00 AM', 'MM/DD/YYYY HH:MI:SS AM'));
COMMIT;
CREATE TABLE MODELS
ID_MODELO NUMBER(9),
MODEL_NAME VARCHAR2(25 BYTE)
TABLESPACE USERS
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
LOGGING
NOCACHE
NOPARALLEL;
INSERT INTO MODELS ( ID_MODELO, MODEL_NAME ) VALUES (
1, 'MODEL 1');
INSERT INTO MODELS ( ID_MODELO, MODEL_NAME ) VALUES (
2, 'MODEL 2');
INSERT INTO MODELS ( ID_MODELO, MODEL_NAME ) VALUES (
3, 'MODEL 3');
INSERT INTO MODELS ( ID_MODELO, MODEL_NAME ) VALUES (
4, 'MODEL 4');
COMMIT;
CREATE TABLE CUSTOMERS
ID_CUSTOMER NUMBER(9),
NAME VARCHAR2(25 BYTE),
LASTNAME VARCHAR2(25 BYTE)
TABLESPACE USERS
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS 2147483645
PCTINCREASE 0
BUFFER_POOL DEFAULT
LOGGING
NOCACHE
NOPARALLEL;
INSERT INTO CUSTOMERS ( ID_CUSTOMER, NAME, LASTNAME ) VALUES (
123, 'Customer 123', 'A');
INSERT INTO CUSTOMERS ( ID_CUSTOMER, NAME, LASTNAME ) VALUES (
459, 'Customer 459', 'B');
INSERT INTO CUSTOMERS ( ID_CUSTOMER, NAME, LASTNAME ) VALUES (
516, 'Customer 516', 'C');
INSERT INTO CUSTOMERS ( ID_CUSTOMER, NAME, LASTNAME ) VALUES (
318, 'Customer 318', 'D');
COMMIT; -
1. How to create an explain plan with rowsource statistics for a complex query that include multiple table joins ?
When multiple tables are involved , and the actual number of rows returned is more than what the explain plan tells. How can I find out what change is needed in the stat plan ?
2. Does rowsource statistics gives some kind of understanding of Extended stats ?You can get Row Source Statistics only *after* the SQL has been executed. An Explain Plan midway cannot give you row source statistics.
To get row source statistics either set STATISTICS_LEVEL='ALL' in the session that executes theSQL OR use the Hint "gather_plan_statistics" in the SQL being executed.
Then use dbms_xplan.display_cursor
Hemant K Chitale -
I am novice in PL/Sql. I need some help in writing a complex query.
I imagine the following table structure.
Type(string) Date(date) count(int)
Given a date range
The query needs to group the records on type and return the record (type and count) which has the max date (its just date, no time is involved) for each group (which is based on type) . If there are more than one records which have the max date then the average count should be returned for that type
i would be glad if someone could give any ideas as to how to go about this query. Thanking you in advance.
regards.Heres the query ... Forget the period .... wht this query is supposed to do is group on assigned ki for a given date range. then it has to get the value which is for the last record for that Ki in the date range. If there are 2 records which have max date then it should give an average.
e.g
assignedKI / date / value
a 1st may 2008 10
b 2nd may 2008 12
c 1st may 2008 13
a 30 - apr-2008 16
b 4th may 2008 17
a 1st may 2008 20
The query should return
a 1st may 2008 15 (which is the average as there are 2 values for the max date for a)
b 4th may 2008 17
c 1st may 2008 13
the following query doesnot work ....
SELECT
kiv2.assigned_k_i,
kiv2.ki_name,
kiv.period,
max( kiv2.ki_value_date) ,
avg(kiv2.ki_value)
FROM
SELECT
assigned_k_i,
period p1,
TO_CHAR(ki_value_date, period) period,
MAX(ki_value_date) ki_value_date
FROM
v_ki_value,
(select ? period from dual)
WHERE
(status = 'APPROVED' OR status = 'AUTOAPPROVED')
AND ( trunc(to_date(to_char(ki_value_date, 'DD/MM/YYYY'), 'DD/MM/YYYY')) >= ?)
AND ( trunc(to_date(to_char(ki_value_date, 'DD/MM/YYYY'), 'DD/MM/YYYY')) <= ?)
AND (INSTR(?, TO_CHAR(assigned_k_i)) > 0)
GROUP BY
assigned_k_i,
TO_CHAR(ki_value_date, period)
) kiv,
v_ki_value kiv2
WHERE
kiv.assigned_k_i = kiv2.assigned_k_i
AND to_char(kiv.ki_value_date, kiv.p1) = to_char(kiv2.ki_value_date, kiv.p1)
GROUP BY
kiv2.assigned_k_i,
kiv2.ki_name,
kiv.period,
kiv2.ki_value
ORDER BY
kiv2.assigned_k_i -
How to bind local variables in complex query in PL/SQL
Hi
I have long complex query with local varibales; when I run in Dynamic SQL I get an error. Not sure which is the easiest way to bind local variables.
L_QUERY := q'[select m.segment1 col1,
'1' col2,
'13' col3,
l.operand col4,
0 col5,
'Y' col8,
'N' col9,
'N' col10,
decode(h.name,'09_UKOR_*','EOS Credit','10_UKOR_*','TopUp','11_UKOR_*','Main') col18
from qp_list_headers_v h,
qp_list_lines_v l,
mtl_system_items m
where h.name in ('09_UKOR_*','10_UKOR_*','11_UKOR_*')
and h.list_header_id = l.list_header_id
and m.inventory_item_id = l.product_attr_value
and m.organization_id = 84
UNION all
SELECT qs.PRODUCT_ATTR_VALUE col1,
hca.account_number col2,
decode (su.site_use_code,'BILL_TO','21','SHIP_TO','18') col3,
0 col4,
qs.operand col5,
null col6,
null col7,
'N' col8,
'Y' col9,
'N' col10,
--Local variables
(case when (trunc(nvl(qq.start_date_active,sysdate)) between l_cur_year_from and l_cur_year_to) and
( trunc(nvl(qq.end_date_active,l_cur_year_to)) >= trunc(l_cur_year_to) ) then 'TopUp'
when (trunc(qq.start_date_active) between l_next_year_from and l_next_year_to) and
( trunc(nvl(qq.end_date_active,l_next_year_to)) >= l_next_year_to) then 'Main'
when (trunc(qq.start_date_active) between l_last_year_from and l_last_year_to) and
( trunc(nvl(qq.end_date_active,l_last_year_to)) >= l_last_year_to ) then 'EOS Return'
end )col18,
--Local variables
(case when (trunc(qq.start_date_active) between l_next_year_from and l_next_year_to) and
( trunc(nvl(qq.end_date_active,l_next_year_to)) >= l_cur_year_to ) then 'N'
else
'Y' end ) col21
FROM qp.qp_list_headers_b qlhb,
qp.qp_list_headers_tl qlht,
qp.qp_qualifiers qq,
qp_modifier_summary_v qs,
hz_cust_site_uses_all su,
hz_cust_acct_sites_all sa,
hz_cust_accounts hca
WHERE 1=1
and qlhb.LIST_TYPE_CODE = 'DLT'
and nvl(qlhb.active_flag,'Y') = 'Y'
and qlhb.list_header_id = qlht.list_header_id
AND qq.list_header_id = qlhb.list_header_id
and qq.list_type_code = qlhb.LIST_TYPE_CODE
and qq.QUALIFIER_CONTEXT = 'CUSTOMER'
and qs.list_header_id = qlhb.list_header_id
and qs.LIST_LINE_TYPE_CODE = 'DIS'
and su.site_use_id = qq.qualifier_attr_value --1064
AND su.cust_acct_site_id = sa.cust_acct_site_id
AND sa.cust_account_id = hca.cust_account_id ]';
--call Dynamic SQL function
l_cursor := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(l_cursor, p_QUERY , DBMS_SQL.NATIVE);
l_rows := DBMS_SQL.EXECUTE(l_cursor);
Any help will be much appreciated.
iI guess I should use DBMS_SQL.BIND_VARIABLE to bind all variables seperately so not to mess with query1) What is the error you get?
2) If you use bind variables inside a query, you should prefix them with colons, so use :l_cur_year_from etc.
3) Why are you using dbms_sql instead of native dynamic sql and/or ref-cursors?
Toon -
Trying to form complex query - need help
I have a fairly complex query that I need to join the results of to show actual and goal by day. The actuals are an aggregation of records that get put in every day, while the targets are a single entry in range format indicating an active range for which the target applies. I'm working on a query that will put things together by month and I'm running into a snag. Can someone please point out where appropriate naming needs to go to get this to come together?
This one works:
(select DATE_INDEX, SUM(LDS) as TTLLDS, SUM(TONS) as TTLTONS from
(select DATE_INDEX, VEH_LOC, SUM(LDS) as LDS, SUM(WT) as TONS from
(select c.DATE_INDEX, c.VEH_LOC, COUNT(j.LOAD_JOB_ID) as LDS,
CASE WHEN SUM(w.SPOT_WEIGHT) = 0 THEN SUM(j.MAN_SPOT_WT)
ELSE SUM(w.SPOT_WEIGHT)
END as WT
from TC c, TC_LOAD_JOBS j, LOAD_RATES r, SPOT_WEIGHTS w
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX = w.DATE_INDEX and j.LOAD_RATE_ID = w.LOAD_RATE_ID
and c.VEH_LOC in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
group by c.DATE_INDEX, c.VEH_LOC
union
select c.DATE_INDEX, c.VEH_LOC, COUNT(j.JOB_ID) as LDS,
DECODE(SUM(j.AVG_SPOT_WEIGHT),0,SUM(r.BID_TONS),SUM(j.AVG_SPOT_WEIGHT)) as WT
from TC_3RDPARTY c, TC_3RDPARTY_JOBS j, LOAD_RATES r
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
and j.FACTORY_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
group by c.DATE_INDEX, c.VEH_LOC)
group by DATE_INDEX, VEH_LOC)
group by DATE_INDEX)Now I need to add in the following query:
select (u.MACH_TPH_D+u.MACH_TPH_N)/2 as MTPH_TGT, (u.LABOR_TPH_D+u.LABOR_TPH_N)/2 as LTPH_TGT
from UTIL_TARGET_LOADERS u
where u.ORG_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)The join needs to be based on VEH_LOC and DAY in the form:
... WHERE u.ORG_ID = x.VEH_LOC
AND x.DATE_INDEX between u.START_DATE and NVL(u.END_DATE,sysdate)I had one that worked just fine when only one entity was involved; the complication arises in that this is a division-level report so I have to individually resolve the subordinates and their goals before I can aggregate. This is one of two queries I need to tie together using a WITH clause so I can pivot the whole thing and present it in month-by-month fashion. When I try to tie it together like the query below, I get: invalid relational operator.
select ttls.DATE_INDEX, SUM(ttls.LDS) as TTLLDS, SUM(ttls.TONS) as TTLTONS, u.TARGET_LTPH, u.TARGET_MTPH
from UTIL_TARGET_LOADERS u,
(select DATE_INDEX, VEH_LOC, SUM(LDS) as LDS, SUM(WT) as TONS from
(select c.DATE_INDEX, c.VEH_LOC, COUNT(j.LOAD_JOB_ID) as LDS,
CASE WHEN SUM(w.SPOT_WEIGHT) = 0 THEN SUM(j.MAN_SPOT_WT)
ELSE SUM(w.SPOT_WEIGHT)
END as WT
from TC c, TC_LOAD_JOBS j, LOAD_RATES r, SPOT_WEIGHTS w
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX = w.DATE_INDEX and j.LOAD_RATE_ID = w.LOAD_RATE_ID
and c.VEH_LOC in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
group by c.DATE_INDEX, c.VEH_LOC
union
select c.DATE_INDEX, c.VEH_LOC, COUNT(j.JOB_ID) as LDS,
DECODE(SUM(j.AVG_SPOT_WEIGHT),0,SUM(r.BID_TONS),SUM(j.AVG_SPOT_WEIGHT)) as WT
from TC_3RDPARTY c, TC_3RDPARTY_JOBS j, LOAD_RATES r
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
and j.FACTORY_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
group by c.DATE_INDEX, c.VEH_LOC)
group by DATE_INDEX, VEH_LOC) ttls
where ttls.DATE_INDEX beween u.START_DATE and NVL(u.END_DATE,sysdate)
and ttls.VEH_LOC = u.ORG_ID
group by ttls.DATE_INDEXI know this is a nested mess, as it has to grab the production from two tables for a range of VEH_LOC values and sum and aggregate by day and VEH_LOC, then I have to try and match that to the targets based on VEH_LOC and day. My final query is to aggregate the whole mess of sums and averages by month.
I'd appreciate it if someone can point me in the right direction.Figured it out.
select ttl.DATE_INDEX, SUM(ttl.LDS) as TTLLDS, SUM(ttl.TONS) as TTLTONS,
AVG((u.MACH_TPH_D+u.MACH_TPH_N)/2) as MTPH_TGT,
AVG((u.LABOR_TPH_D+u.LABOR_TPH_N)/2) as LTPH_TGT
from
(select DATE_INDEX, VEH_LOC, SUM(LDS) as LDS, SUM(WT) as TONS from
(select c.DATE_INDEX, c.VEH_LOC, COUNT(j.LOAD_JOB_ID) as LDS,
CASE WHEN SUM(w.SPOT_WEIGHT) = 0 THEN SUM(j.MAN_SPOT_WT)
ELSE SUM(w.SPOT_WEIGHT)
END as WT
from TC c, TC_LOAD_JOBS j, LOAD_RATES r, SPOT_WEIGHTS w
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX = w.DATE_INDEX and j.LOAD_RATE_ID = w.LOAD_RATE_ID
and c.VEH_LOC in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
group by c.DATE_INDEX, c.VEH_LOC
union
select c.DATE_INDEX, c.VEH_LOC, COUNT(j.JOB_ID) as LDS,
DECODE(SUM(j.AVG_SPOT_WEIGHT),0,SUM(r.BID_TONS),SUM(j.AVG_SPOT_WEIGHT)) as WT
from TC_3RDPARTY c, TC_3RDPARTY_JOBS j, LOAD_RATES r
where c.TC_ID = j.TC_ID and j.LOAD_RATE_ID = r.LOAD_RATE_ID
and c.DATE_INDEX between to_date('08/01/2010','MM/DD/YYYY') and to_date('07/31/2011','MM/DD/YYYY')
and j.FACTORY_ID in (select ORG_ID from ORG_ENTITIES where MNG_ORG_ID = 200)
group by c.DATE_INDEX, c.VEH_LOC)
group by DATE_INDEX, VEH_LOC) ttl, UTIL_TARGET_LOADERS u
where u.ORG_ID = ttl.VEH_LOC
and ttl.DATE_INDEX between u.START_DATE and NVL(U.END_DATE,sysdate)
group by ttl.DATE_INDEX, (u.LABOR_TPH_D+u.LABOR_TPH_N)/2 -
Need complex query with joins and AGGREGATE functions.
Hello Everyone ;
Good Morning to all ;
I have 3 tables with 2 lakhs record. I need to check query performance.. How CBO rewrites my query in materialized view ?
I want to make complex join with AGGREGATE FUNCTION.
my table details
SQL> select from tab;*
TNAME TABTYPE CLUSTERID
DEPT TABLE
PAYROLL TABLE
EMP TABLE
SQL> desc emp
Name
EID
ENAME
EDOB
EGENDER
EQUAL
EGRADUATION
EDESIGNATION
ELEVEL
EDOMAIN_ID
EMOB_NO
SQL> desc dept
Name
EID
DNAME
DMANAGER
DCONTACT_NO
DPROJ_NAME
SQL> desc payroll
Name
EID
PF_NO
SAL_ACC_NO
SALARY
BONUS
I want to make complex query with joins and AGGREGATE functions.
Dept names are : IT , ITES , Accounts , Mgmt , Hr
GRADUATIONS are : Engineering , Arts , Accounts , business_applications
I want to select records who are working in IT and ITES and graduation should be "Engineering"
salary > 20000 and < = 22800 and bonus > 1000 and <= 1999 with count for males and females Separately ;
Please help me to make a such complex query with joins ..
Thanks in advance ..
Edited by: 969352 on May 25, 2013 11:34 AM969352 wrote:
why do you avoid providing requested & NEEDED details?I do NOT understand what do you expect ?
My Goal is :
1. When executing my own query i need to check expalin plan.please proceed to do so
http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_9010.htm#SQLRF01601
2. IF i enable query rewrite option .. i want to check explain plan ( how optimizer rewrites my query ) ? please proceed to do so
http://docs.oracle.com/cd/E11882_01/server.112/e16638/ex_plan.htm#PFGRF009
3. My only aim is QUERY PERFORMANCE with QUERY REWRITE clause in materialized view.It is an admirable goal.
Best Wishes on your quest for performance improvements. -
Complex Query in toplink descriptor
I have a complex query with "Order by" and "Max" aggregation function and I am trying to convert it to a named query. I am using toplink descriptor in Jdeveloper 11G.
1. Is it possible to write a named query which returns values from more than two different table?
2. how to specify Max operation in the Toplink descriptor.
Here is my Query
select * from table1 t1 ,table2 t2
where
t1.id = t2.id and
t1.name ='Name' and
t1.status<>'DELETE' and
t2.a_id = (select max(a_id) from table2 t21 where
t21.a_id = t1.id and
group by id)TopLink supports sub-selects through sub-queries in TopLink Expressions, and through JPQL. I'm not sure on the JDev support though, you may need to use a code customizer or amendment to add the named query.
James : http://www.eclipselink.org -
Complex Query which needs tuning
Hello :
I have a complex query that needs to be tuned. I have little experience in tuning the sql and hence taking the help of your guys.
The Query is as given below:
Database version 11g
SELECT DISTINCT P.RESPONSIBILITY, P.PRODUCT_MAJOR, P.PRODUCT_MINOR,
P.PRODUCT_SERIES, P.PRODUCT_CATEGORY AS Category1, SO.REGION_CODE,
SO.STORE_CODE, S.Store_Name, SOL.PRODUCT_CODE, PRI.REPLENISHMENT_TYPE,
PRI.SUPPLIER_CODE,
SOL.SOLD_WITH_NIC, SOL.SUGGESTED_PRICE,
PRI.INVOICE_COST, SOL.FIFO_COST,
SO.ORDER_TYPE_CODE, SOL.DOCUMENT_NUM,
SOS.SLSP_CD, '' AS FNAME, '' AS LNAME,
SOL.PRICE_EXCEPTION_CODE, SOL.AS_IS,
SOL.STATUS_DATE,
Sum(SOL.QUANTITY) AS SumOfQUANTITY,
Sum(SOL.EXTENDED_PRICE) AS SumOfEXTENDED_PRICE
--Format([SALES_ORDER].[STATUS_DATE],"mmm-yy") AS [Month]
FROM PRODUCT P,
PRODUCT_MAJORS PM,
SALES_ORDER_LINE SOL,
STORE S,
SALES_ORDER SO,
SALES_ORDER_SPLITS SOS,
PRODUCT_REGIONAL_INFO PRI,
REGION_MAP R
WHERE P.product_major = PM.PRODUCT_MAJOR
and SOL.PRODUCT_CODE = P.PRODUCT_CODE
and SO.STORE_CODE = S.STORE_CODE
AND SO.REGION_CODE = S.REGION_CODE
AND SOL.REGION_CODE = SO.REGION_CODE
AND SOL.DOCUMENT_NUM = SO.DOCUMENT_NUM
AND SOL.DELIVERY_SEQUENCE_NUM = SO.DELIVERY_SEQUENCE_NUM
AND SOL.STATUS_CODE = SO.STATUS_CODE
AND SOL.STATUS_DATE = SO.STATUS_DATE
AND SO.REGION_CODE = SOS.REGION_CODE
AND SO.DOCUMENT_NUM = SOS.DOCUMENT_NUM
AND SOL.PRODUCT_CODE = PRI.PRODUCT_CODE
AND PRI.REGION_CODE = R.CORP_REGION_CODE
AND SO.REGION_CODE = R.DS_REGION_CODE
AND P.PRODUCT_MAJOR In ('STEREO','TELEVISION','VIDEO')
AND SOL.STATUS_CODE = 'D'
AND SOL.STATUS_DATE BETWEEN '01-JUN-09' AND '30-JUN-09'
AND SO.STORE_CODE NOT IN
('10','20','30','40','70','91','95','93','94','96','97','98','99',
'9V','9W','9X','9Y','9Z','8Z',
'8Y','92','CZ','FR','FS','FT','FZ','FY','FX','FW','FV','GZ','GY','GU','GW','GV','GX')
GROUP BY
P.RESPONSIBILITY, P.PRODUCT_MAJOR, P.PRODUCT_MINOR, P.PRODUCT_SERIES, P.PRODUCT_CATEGORY,
SO.REGION_CODE, SO.STORE_CODE, /*S.Short Name, */
S.Store_Name, SOL.PRODUCT_CODE,
PRI.REPLENISHMENT_TYPE, PRI.SUPPLIER_CODE,
SOL.SOLD_WITH_NIC, SOL.SUGGESTED_PRICE, PRI.INVOICE_COST,
SOL.FIFO_COST, SO.ORDER_TYPE_CODE, SOL.DOCUMENT_NUM,
SOS.SLSP_CD, '', '', SOL.PRICE_EXCEPTION_CODE,
SOL.AS_IS, SOL.STATUS_DATE
Explain Plan:
SELECT STATEMENT, GOAL = ALL_ROWS Cost=583 Cardinality=1 Bytes=253
HASH GROUP BY Cost=583 Cardinality=1 Bytes=253
FILTER
NESTED LOOPS Cost=583 Cardinality=1 Bytes=253
HASH JOIN OUTER Cost=582 Cardinality=1 Bytes=234
NESTED LOOPS
NESTED LOOPS Cost=571 Cardinality=1 Bytes=229
NESTED LOOPS Cost=571 Cardinality=1 Bytes=207
NESTED LOOPS Cost=569 Cardinality=2 Bytes=368
NESTED LOOPS Cost=568 Cardinality=2 Bytes=360
NESTED LOOPS Cost=556 Cardinality=3 Bytes=435
NESTED LOOPS Cost=178 Cardinality=4 Bytes=336
NESTED LOOPS Cost=7 Cardinality=1 Bytes=49
HASH JOIN Cost=7 Cardinality=1 Bytes=39
VIEW Object owner=CORP Object name=index$_join$_015 Cost=2 Cardinality=3 Bytes=57
HASH JOIN
INLIST ITERATOR
INDEX UNIQUE SCAN Object owner=CORP Object name=PRODMJR_PK Cost=0 Cardinality=3 Bytes=57
INDEX FAST FULL SCAN Object owner=CORP Object name=PRDMJR_PR_FK_I Cost=1 Cardinality=3 Bytes=57
VIEW Object owner=CORP Object name=index$_join$_016 Cost=4 Cardinality=37 Bytes=740
HASH JOIN
INLIST ITERATOR
INDEX RANGE SCAN Object owner=CORP Object name=PRDMNR1 Cost=3 Cardinality=37 Bytes=740
INDEX FAST FULL SCAN Object owner=CORP Object name=PRDMNR_PK Cost=4 Cardinality=37 Bytes=740
INDEX UNIQUE SCAN Object owner=CORP Object name=PRODMJR_PK Cost=0 Cardinality=1 Bytes=10
MAT_VIEW ACCESS BY INDEX ROWID Object owner=CORP Object name=PRODUCTS Cost=171 Cardinality=480 Bytes=16800
INDEX RANGE SCAN Object owner=CORP Object name=PRD2 Cost=3 Cardinality=681
TABLE ACCESS BY INDEX ROWID Object owner=DS Object name=SALES_ORDER_LINE Cost=556 Cardinality=1 Bytes=145
BITMAP CONVERSION TO ROWIDS
BITMAP INDEX SINGLE VALUE Object owner=DS Object name=SOL2
TABLE ACCESS BY INDEX ROWID Object owner=DS Object name=SALES_ORDER Cost=4 Cardinality=1 Bytes=35
INDEX RANGE SCAN Object owner=DS Object name=SO1 Cost=3 Cardinality=1
TABLE ACCESS BY INDEX ROWID Object owner=DS Object name=REGION_MAP Cost=1 Cardinality=1 Bytes=4
INDEX RANGE SCAN Object owner=DS Object name=REGCD Cost=0 Cardinality=1
MAT_VIEW ACCESS BY INDEX ROWID Object owner=CORP Object name=PRODUCT_REGIONAL_INFO Cost=2 Cardinality=1 Bytes=23
INDEX UNIQUE SCAN Object owner=CORP Object name=PRDRI_PK Cost=1 Cardinality=1
INDEX UNIQUE SCAN Object owner=CORP Object name=BI_STORE_INFO_PK Cost=0 Cardinality=1
MAT_VIEW ACCESS BY INDEX ROWID Object owner=CORP Object name=BI_STORE_INFO Cost=1 Cardinality=1 Bytes=22
VIEW Object owner=DS cost=11 Cardinality=342 Bytes=1710
HASH JOIN Cost=11 Cardinality=342 Bytes=7866
MAT_VIEW ACCESS FULL Object owner=CORP Object name=STORE_CORP Cost=5 Cardinality=429 Bytes=3003
NESTED LOOPS Cost=5 Cardinality=478 Bytes=7648
MAT_VIEW ACCESS FULL Object owner=CORP Object name=STORE_GROUP Cost=5 Cardinality=478 Bytes=5258
INDEX UNIQUE SCAN Object owner=CORP Object name=STORE_REGIONAL_INFO_PK Cost=0 Cardinality=1 Bytes=5
INDEX RANGE SCAN Object owner=DS Object name=SOS_PK Cost=2 Cardinality=1 Bytes=19
Regards,
BMPFirst thing that i notice in this query is you are Using Distinct as well as Group by.
Your group by will always give you distinct results ,then again why do you need the Distinct?
For example
WITH t AS
(SELECT 'clm1' col1, 'contract1' col2,10 value
FROM DUAL
UNION ALL
SELECT 'clm1' , 'contract1' ,10 value
FROM DUAL
UNION ALL
SELECT 'clm1', 'contract2',10
FROM DUAL
UNION ALL
SELECT 'clm2', 'contract1',10
FROM DUAL
UNION ALL
SELECT 'clm3', 'contract1',10
FROM DUAL
UNION ALL
SELECT 'clm4', 'contract2',10
FROM DUAL)
SELECT distinct col1,col2,sum(value) from t
group by col1,col2Is always same as
WITH t AS
(SELECT 'clm1' col1, 'contract1' col2,10 value
FROM DUAL
UNION ALL
SELECT 'clm1' , 'contract1' ,10 value
FROM DUAL
UNION ALL
SELECT 'clm1', 'contract2',10
FROM DUAL
UNION ALL
SELECT 'clm2', 'contract1',10
FROM DUAL
UNION ALL
SELECT 'clm3', 'contract1',10
FROM DUAL
UNION ALL
SELECT 'clm4', 'contract2',10
FROM DUAL)
SELECT col1,col2,sum(value) from t
group by col1,col2And also
AND SOL.STATUS_DATE BETWEEN '01-JUN-09' AND '30-JUN-09'It would be best to use a to_date when hard coding your dates.
Edited by: user5495111 on Aug 6, 2009 1:32 PM -
How to store data from a complex query and only fresh hourly or daily?
We have a report which runs quite slow (1-2 minutes) because the query is quite complicate, so we would like to run this query daily only and store in a table so for those procedures that need to use this complex query as a subquery, can just join to this table directly to get results.
However, I am not sure what kind of object I should use to store data for this complex query. Is data in global temp table only persist within transaction? I need something that can persist the data and be access by procedures.
Any suggestions are welcome,
CheersThank you for your reply. I looked at the materialized view earlier on, but have some difficulties to use it. So I have some questions here:
1.The complex query is not a sum or aggregate functions, it just need to get data from different tables based on different conditions, in this case is it still appropriate to use meterialized view?
2.If it is, I created one, but how to use it in my procedure? From the articles I read, it seems I can't just query from this view directly. So do I need to keep the complex query in my procedure and how the procedure will use the meterialized view instead?
3. I also put the complex query in a normal view, then create a materialized view for this normal view (I expect the data from the complex query will be cache here), then in the procedure I just select * from my_NormalView, but it takes the same time to run even when I set the QUERY_REWRITE_ENABLED to true in the alter session. So I am not sure what else I need to do to make sure the procedure use the materialized view instead of the normal view. Can I query from the Materialized View directly?
Below in the code I copied from one of the article to create the materialized view based on my normal view:
CREATE MATERIALIZED VIEW HK3ControlDB.MW_RIRating
PCTFREE 5 PCTUSED 60
TABLESPACE "USERS"
STORAGE (INITIAL 50K NEXT 50K)
USING INDEX STORAGE (INITIAL 25K NEXT 25K)
REFRESH START WITH ROUND(SYSDATE + 1) + 11/24
NEXT NEXT_DAY(TRUNC(SYSDATE), 'MONDAY') + 15/24
enable query rewrite
AS SELECT * FROM HK3ControlDB.VW_RIRating;
Cheers -
EJB-QL--- Complex Query --- Two Entites
hi
Can i write Complex Query in EJB-QL
between two entities...
have any buddy wrote the same....
i badly require the same...
bye
RAJRaj -- OC4J will support EJB-QL soon but not yet. You can look at
Re: Data Integrity
for more details.
Now, as for complex query, you may still be able to do this in the current version through the orion-ejb-jar.xml. You can
include complex SQL there in the finder methods.
What exactly do you need to do?
later -- Jeff
hi
Can i write Complex Query in EJB-QL
between two entities...
have any buddy wrote the same....
i badly require the same...
bye
RAJ -
Complex query crashing database!
Hi
Our 10g database runs on Windows 2003 (with 32 GB RAM on server).
Whenever a user is running a particular complex query, the database just crashes suddenly.
In fact, it doesn't write anything in alert log file for any problem.
The user starts the query, it runs for sometime, then suddenly database crashes!
Only way to work there after is to restart the database.
Although I asked the user to not to run that query again but it is generated by some application which may generate similar query again!
So, how I can prevent database from crashing again?
Will creating a resource profile to allocate finite resource help? Problem is since database crashes so suddenly, I can't find any info from log files!
ThanxThe database instance crashes. We have to start the ORACLE.EXE again to up the database.
Here is last few lines of the alert.log
Fri Feb 05 11:14:59 2010
Setting recovery target incarnation to 1
Fri Feb 05 11:14:59 2010
Successful mount of redo thread 1, with mount id 1911309359
Fri Feb 05 11:14:59 2010
Database mounted in Exclusive Mode
Completed: alter database mount exclusive
Fri Feb 05 11:14:59 2010
alter database open
Fri Feb 05 11:15:00 2010
Beginning crash recovery of 1 threads
parallel recovery started with 7 processes
Fri Feb 05 11:15:00 2010
Started redo scan
Fri Feb 05 11:15:00 2010
Completed redo scan
3886 redo blocks read, 276 data blocks need recovery
Fri Feb 05 11:15:00 2010
Started redo application at
Thread 1: logseq 179837, block 3
Fri Feb 05 11:15:00 2010
Recovery of Online Redo Log: Thread 1 Group 6 Seq 179837 Reading mem 0
Mem# 0 errs 0: F:\DB LOG FILES\ORADATA\REDO06.LOG
Mem# 1 errs 0: F:\DB LOG FILES MULTIPLEXED\ORADATA\REDO06.LOG
Fri Feb 05 11:15:00 2010
Completed redo application
Fri Feb 05 11:15:00 2010
Completed crash recovery at
Thread 1: logseq 179837, block 3889, scn 10584777673
276 data blocks read, 276 data blocks written, 3886 redo blocks read
Fri Feb 05 11:15:01 2010
LGWR: STARTING ARCH PROCESSES
ARC0 started with pid=21, OS id=5752
Fri Feb 05 11:15:01 2010
ARC0: Archival started
ARC1: Archival started
ARC1 started with pid=22, OS id=4548
Fri Feb 05 11:15:02 2010
LGWR: STARTING ARCH PROCESSES COMPLETE
Thread 1 advanced to log sequence 179838
Thread 1 opened at log sequence 179838
Current log# 7 seq# 179838 mem# 0: F:\DB LOG FILES\ORADATA\REDO07.LOG
Current log# 7 seq# 179838 mem# 1: F:\DB LOG FILES MULTIPLEXED\ORADATA\REDO07.LOG
Successful open of redo thread 1
Fri Feb 05 11:15:02 2010
ARC1: STARTING ARCH PROCESSES
Fri Feb 05 11:15:02 2010
ARC0: Becoming the 'no FAL' ARCH
ARC0: Becoming the 'no SRL' ARCH
Fri Feb 05 11:15:02 2010
SMON: enabling cache recovery
Fri Feb 05 11:15:02 2010
ARC2: Archival started
ARC1: STARTING ARCH PROCESSES COMPLETE
ARC2 started with pid=23, OS id=6120
Fri Feb 05 11:15:02 2010
Successfully onlined Undo Tablespace 1.
Fri Feb 05 11:15:02 2010
SMON: enabling tx recovery
Fri Feb 05 11:15:02 2010
Database Characterset is WE8MSWIN1252
replication_dependency_tracking turned off (no async multimaster replication found)
Starting background process QMNC
QMNC started with pid=25, OS id=5312
Fri Feb 05 11:15:03 2010
Completed: alter database open
Fri Feb 05 11:15:03 2010
ARC1: Becoming the heartbeat ARCH
Fri Feb 05 11:16:01 2010
Shutting down archive processes
Fri Feb 05 11:16:06 2010
ARCH shutting down
ARC2: Archival stopped
Maybe you are looking for
-
On iOS 7 on iPad mini how do I reset restrictions?
On iOS 7 on iPad mini how do I reset restrictions? I forgot the password and can not disable them
-
TS1717 restoring default factory settings without itunes as both may have malware
How can I restoring default factory settings without itunes on my ipod 1 and ipod 3rd generation, as both may have malware aswell as 3 laptops, 2 mobile, TV snd BluRay Browser. any advice will be greatly appreciated. have tried to disinfect with Kasp
-
Doubt about which driver to use for ATI Technologies Inc SB600 IDE
Currently this is what I get with lspci -v 00:14.1 IDE interface: ATI Technologies Inc SB600 IDE (prog-if 8a [Master SecP PriP]) Subsystem: Device 1631:c109 Flags: bus master, 66MHz, medium devsel, latency 64, IRQ 16 I/O ports at 01f0 [size=8] I/O po
-
Problem with AI File extension association
I have Adobe CS4 and for some reason, my computer doesn't recognize Adobe Illustrator as the default program for .ai files. Instead, it lists AIRegedit.exe. When I open up the Properties, it displays in the type of file field "example.ai" as an .a
-
Hello Experts, I try to made a Unit Conversion in a Infoset but appear one error that said me that the 0material infoobject is not in the query, but is in the query the problem is that for infoset now is INSOFETNAME_FXXX. Is possible made a unit conv