Need a query(complex query)
Hi All,
I have two tables.
1.Revenue_forecast
colouns
region_name,area_name,fin_year,dcomp(%),fcomp(%)
'RME','DBServices','2008-2009',75,30
'RME','DBServices','2008-2009',75,30
'RME','RDBA-Managed Services','2008-2009',75,90
2.Sales_forecast
region_name, area_name, yr_mth, sales_target
'RME', 'DBServices', '200809', 80000
'RME', 'DBServices', '200810', 70000
I need a repot like this
yr_mth,sales_target,(sales_target*dcomp),(sales_target*fcomp)
200809,80000,(80000*75),(80000*30)
200810, 70000,(70000*75),(70000*30)
In where condition Have to give region_name,area_name,fin_year(2008_2009) based on these value report should generateThanks,
NR
Edited by: user10966033 on 5 juin 2009 06:30
Edited by: user10966033 on 5 juin 2009 21:24
user10966033 wrote:
Thanks for reply
composite unique on( region_name,area_name,fin_year)
duplicate records should not come, need like monthly wise roport just like this
If i select fin_year like 2008-2009 ,for this particular fin_year report should come(from2008jun to 2009uly);
yr_mth sales_target dcomp fcomp
200809 20000 1000000 2000000
200810 20000 1000000 2000000
Something like
SQL> set feedback on;
SQL> WITH revenue_forecast AS
2 (SELECT 'RME' region_name, 'DBServices' area_name, '2008-2009' fin_year,
3 75 dcomp, 30 fcomp
4 FROM DUAL
5 UNION ALL
6 SELECT 'RME', 'DBServices', '2008-2009', 75, 30
7 FROM DUAL
8 UNION ALL
9 SELECT 'RME', 'RDBA-Managed Services', '2008-2009', 75, 90
10 FROM DUAL
11 UNION ALL
12 SELECT 'RME', 'DBServices', '2009-2010', 75, 30
13 FROM DUAL),
14 sales_forecast AS
15 (SELECT 'RME' region_name, 'DBServices' area_name, '200809' yr_mth,
16 80000 sales_target
17 FROM DUAL
18 UNION ALL
19 SELECT 'RME', 'DBServices', '200810', 70000
20 FROM DUAL
21 UNION ALL
22 SELECT 'RME', 'DBServices', '200910', 70000
23 FROM DUAL)
24 SELECT sf.yr_mth, sf.sales_target, sales_target * dcomp,
25 sales_target * fcomp
26 FROM (SELECT DISTINCT region_name, area_name, fin_year, dcomp, fcomp
27 FROM revenue_forecast) rf,
28 sales_forecast sf
29 WHERE rf.region_name = sf.region_name
30 AND rf.area_name = sf.area_name
31 AND rf.fin_year = &financial_year --'2008-2009'
32 AND TO_DATE (sf.yr_mth, 'YYYYMM') BETWEEN TO_DATE
33 ( SUBSTR (rf.fin_year,
34 1,
35 4
36 )
37 || '06',
38 'YYYYMM'
39 )
40 AND TO_DATE
41 ( SUBSTR (rf.fin_year,
42 6,
43 4
44 )
45 || '07',
46 'YYYYMM'
47 )
48 ORDER BY 1
49 /
Enter value for financial_year: '2008-2009'
old 31: AND rf.fin_year = &financial_year --'2008-2009'
new 31: AND rf.fin_year = '2008-2009' --'2008-2009'
YR_MTH SALES_TARGET SALES_TARGET*DCOMP SALES_TARGET*FCOMP
200809 80000 6000000 2400000
200810 70000 5250000 2100000
2 rows selected.
SQL> /
Enter value for financial_year: '2009-2010'
old 31: AND rf.fin_year = &financial_year --'2008-2009'
new 31: AND rf.fin_year = '2009-2010' --'2008-2009'
YR_MTH SALES_TARGET SALES_TARGET*DCOMP SALES_TARGET*FCOMP
200910 70000 5250000 2100000
1 row selected.
SQL>I have added one more sample record (for '2009-2010') to the ones you provided to test.
Hope this helps.
Regards,
Jo
Similar Messages
-
Need Help with complex query for productio database
Hello again,
i need your help again, for an query how Shows me how long every production step takes per Order.
See sample data and what i expect.
Thank you all for your help.
We use Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
Here the sample data tables:
CREATE TABLE TABLE_2
( "ORDER_NR" VARCHAR2 (12)
, "PRIORITY" VARCHAR2 (2)
, "WO_STEP" VARCHAR2 (1)
, "STEP_DATE" DATE
CREATE TABLE TABLE_1
( "ORDER_NR" VARCHAR2 (12) PRIMARY KEY
, "PRIORITY" VARCHAR2 (2)
, "CREATE_DATE" DATE
, "ACT_STEP" VARCHAR2 (2)
, "STEP_DATE" DATE
, "EMPLOYEE" VARCHAR2 (5)
, "DESCRIPTION" VARCHAR2 (20)
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, CREATE_DATE, ACT_STEP, STEP_DATE, EMPLOYEE, DESCRIPTION)
VALUES ('1KKA1T205634', '12', TO_DATE('10-FEB-13 10:00:00','DD-MON-RR HH24:MI:SS'), 'U', TO_DATE('28-FEB-13 12:00:00','DD-MON-RR HH24:MI:SS'), 'W0010', 'CLEAN HOUSE');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, CREATE_DATE, ACT_STEP, STEP_DATE, EMPLOYEE, DESCRIPTION)
VALUES ('1KKA1Z300612', '12', TO_DATE('08-FEB-13 14:00:00','DD-MON-RR HH24:MI:SS'), 'F', TO_DATE('20-FEB-13 16:00:00','DD-MON-RR HH24:MI:SS'), 'K0052', 'REPAIR CAR');
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'A', TO_DATE('12-FEB-13 13:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', '5', TO_DATE('13-FEB-13 09:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'K', TO_DATE('13-FEB-13 10:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', '5', TO_DATE('13-FEB-13 11:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'K', TO_DATE('13-FEB-13 12:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', '5', TO_DATE('13-FEB-13 16:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'C', TO_DATE('14-FEB-13 08:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'B', TO_DATE('14-FEB-13 10:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'E', TO_DATE('18-FEB-13 13:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'F', TO_DATE('20-FEB-13 16:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'S', TO_DATE('21-FEB-13 08:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'R', TO_DATE('21-FEB-13 09:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'U', TO_DATE('28-FEB-13 12:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'A', TO_DATE('12-FEB-13 13:52:42','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', '5', TO_DATE('13-FEB-13 09:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'K', TO_DATE('13-FEB-13 10:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', '5', TO_DATE('13-FEB-13 11:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'K', TO_DATE('13-FEB-13 12:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', '5', TO_DATE('13-FEB-13 16:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'C', TO_DATE('14-FEB-13 08:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'B', TO_DATE('14-FEB-13 10:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'E', TO_DATE('18-FEB-13 13:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'F', TO_DATE('20-FEB-13 16:00:00','DD-MON-RR HH24:MI:SS'));
COMMIT;And here is what i expect from my query:
SYSDATE 28.Feb.13 14:00
ORDER_NR PRIORITYCREATE_DATE STATUS STATUS_DATE DESCRIPTION AGE_1 AGE_2 WAITNG STEP_A STEP_B STEP_C STEP_5 STEP_K STEP_E STEP_F STEP_S STEP_R
1KKA1T205634 12 10.Feb.13 10:00 U 28.Feb.13 12:00 CLEAN HOUSE 18,083 8,833 2,125 0,833 4,125 0,083 0,750 0,208 2,125 0,666 0,042 7,125
1KKA1Z300612 12 08.Feb.13 14:00 F 20.Feb.13 16:00 REPAIR CAR 20,000 16,042 2,125 0,833 4,125 0,083 0,750 0,208 2,125 0,666 And now the explanation to the query result:
The AGE_1 is the difference in days between the 'CREATE_DATE' and IF EXSIST the STEP 'U' then STEP_DATE or if the STEP 'U' is not found in TABLE_2 then it should show the difference in days between the 'CREATE_DATE' and the 'SYSDATE'
The AGE_2 is the difference in days between the STEP 'A' STEP_DATE and IF EXSIST the STEP 'R' then STEP_DATE or if the STEP 'R' is not found in TABLE_2 then it should show the difference in days between the 'CREATE_DATE' and the 'SYSDATE'
The WAITING is the difference in days between CREATE_DATE and STEP 'A' STEP_DATE
The following columns show the days how long the ORDER_NR stays in these STEP, if an ORDER_NR comes into the same STEP more then one time it should be calculated together.
If the ORDER_NR skips a step it should show a zero in the specific field.
I hope my explanation is good enough, my english skills are far away from good.
Thanks all for your help.
Greets Reinhard W.Hi,
i changed this query:
with t2 as (
select t.*,
lead(step_date) over(partition by order_nr order by step_date) next_step_date
from table_2 t
select t1.*,
nvl(
max(
case t2.wo_step
when 'U' then t2.step_date
end
sysdate
) - t1.create_date age_1,
nvl(
max(
case t2.wo_step
when 'R' then t2.step_date
end
sysdate
) - t1.create_date age_2,
sum(
case t2.wo_step
when 'B' then t2.next_step_date - t2.step_date
end
) step_b
from table_1 t1,
t2
where t2.order_nr = t1.order_nr
group by t1.order_nr,
t1.priority,
t1.create_date,
t1.act_step,
t1.step_date,
t1.employee,
t1.descriptionTo this:
with t2 as (
select t.*,
lead(step_date) over(partition by order_nr order by step_date) next_step_date
from table_2 t
select t1.order_nr,
nvl(
max(
case t2.wo_step
when 'U' then t2.step_date
end
sysdate
) - t1.create_date age_1,
nvl(
max(
case t2.wo_step
when 'R' then t2.step_date
end
sysdate
) - t1.create_date age_2,
sum(
case t2.wo_step
when 'B' then t2.next_step_date - t2.step_date
end
) step_b
from table_1 t1,
t2
where t2.order_nr = t1.order_nr
group by t1.order_nrthen i get the ORA-00979 Error.
Whats wrong?
I have another question.
How can i handle i i want to group to or more 'STEP's in one Column.
in Case of this i want that the column 'STEP_B' contains all days for STEP 'B' and STEP '5'.
I tried already with a + Operation like this:
with t2 as (
select t.*,
lead(step_date) over(partition by order_nr order by step_date) next_step_date
from table_2 t
select t1.*,
nvl(
max(
case t2.wo_step
when 'U' then t2.step_date
end
sysdate
) - t1.create_date age_1,
nvl(
max(
case t2.wo_step
when 'R' then t2.step_date
end
sysdate
) - t1.create_date age_2,
Round( sum(
case t2.wo_step
when 'B' then t2.next_step_date - t2.step_date
end
) +
sum(
case t2.wo_step
when '5' then t2.next_step_date - t2.step_date
end
), 3 ) step_b
from table_1 t1,
t2
where t2.order_nr = t1.order_nr
group by t1.order_nr,
t1.priority,
t1.create_date,
t1.act_step,
t1.step_date,
t1.employee,
t1.descriptionBut this does reply evertime a NULL.
Thank You. -
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.* -
Need Help with complex query and computing values
I have an issue trying to combine data from several tables. I need help trying to compute the "Total Hours", "Max Pressure" ,"Average Pressure" while displaying the "Manufacturer",
"Part Type" , "Serial Number", "District", "Status","Truck Type",and "truck number" for a certain Part on all Trucks. I need to be able check and see if the serial number was on
a particular job and calculate the hours of that serial number if it was on that job and the jobdate falls between the install date and removal date. Ive tried but keep getting either
repeating rows, total hrs of the truck instead of the serial number. Ive considered doing a pivot to display it but have been having trouble putting it together.
table 1
(*records of parts*)
Contains serial number,truck number, part type, part number, install date, removal date, status
table 2
(*records of Jobs*)
contains Jobnumber, JobStartdate, Max pressure, average pressure, and Totalhrs
table 3
(records related to jobs and trucks)
contains jobnumber, district , and truck numbers
Table 4
(records of manufacturers and part numbers)
contains partnumber, manufacturer name, and truck type
I would like to get it to show like below
SerialNo PrtType
MFG TruckNo
TrkType TtlHrs
MaxPr AvgPr
Status
Dst
AB345 1200series
NGK 2G34
Boss X 400 10,000
9500 NonOp
NE
Thanks in advanceHope this helps
Note: (Date of Mar 1 2014 was just extended to a further date so the system would know that its still operating when calculating current hours)
Table 1
SerialNo TruckNo InstallDate RemovalDate Status PartNo PartType
BJ1002 F917 2013-09-17 2014-03-01 Oper L871235 BJ 3000 GL
CWS1002 F104 2012-11-21 2013-03-29 NonOper L76088-450 CWS 2000
CWS1003 F104 2013-04-24 2013-08-01 NonOper L76088-450 CWS 2000
CWS1005 F187 2012-11-21 2013-04-04 NonOper L76088-450 CWS 2000
CWS1006 F187 2013-04-24 2013-06-30 NonOper L76088-450 CWS 2000
CWS1007 F187 2013-06-30 2013-03-01 Oper L76088-450 CWS 2000
CWS1009 2F60 2013-05-05 2013-03-01 Oper L76088-450 CWS 2000
CWS1011 F809 2013-05-28 2013-08-28 NonOper L76088-400 CWS 2000
CWS1013 F990 2013-06-11 2013-10-29 NonOper L76088-450 CWS 2000
CWS1015 F783 2013-06-28 2013-03-01 Oper L76088-450 CWS 2000
Table 2
JobNumber Date District PrAvTreat PrMaxTr TotalHrs
553811287 2012-01-19 Fairmount 7337 8319 1.53
652110088 2012-08-20 San Antonio 6340 7075 0.47
652110090 2012-08-21 San Antonio 6134 7131 0.62
652110091 2012-08-22 San Antonio 6180 2950 0.58
652110092 2012-08-23 San Antonio 5959 6789 0.64
652110093 2012-08-23 San Antonio 6165 7466 0.62
Table 3
TruckNo District JobNumber
1F01 Odessa 10011012329
1F01 Odessa 10011012333
1F01 Odessa 10011014831
1F01 Odessa 10011014834
1F01 Odessa 10011012332
1F01 Odessa 10011012328
1F01 Odessa 10011014829
Table 4
PartNumber Manufacturer TruckType
L322020-2 Jimmy Dean Ford T
L322738-2 Lucas Ford T
L47869-1 Jimmy Dean Ford T
L76070-650 NGK Ford T
Sam Howard -
Hi All
I have the following requirement, please see the following table
BIN_DATA_ID TRAFFIC_SAMPLE_ID END_INTV_TIME BIN_1_DATA BIN_2_DATA BIN_3_DATA
1 539 1100 0 34 19
2 539 1200 0 65 18
3 539 1300 0 51 17
4 539 1400 0 65 27
5 539 1500 0 99 48
6 539 1600 0 426 138
7 539 1700 0 151 62
8 539 1800 0 80 32
9 539 1900 0 31 11
10 539 2000 0 37 11
11 539 2100 0 24 9
12 539 2200 0 16 5
13 539 2300 0 27 12
14 539 2400 0 55 20
15 539 100 0 18 9
16 539 200 0 134 52
17 539 300 0 230 69
18 539 400 0 15 7
19 539 500 0 6 5
20 539 600 0 47 23
21 539 700 0 100 41
22 539 800 0 196 43
23 539 900 0 81 20
24 539 1000 0 58 28
25 539 1100 0 58 24
26 539 1200 0 60 22
27 539 1300 0 42 18
28 539 1400 0 53 15
29 539 1500 0 107 43
30 539 1600 0 441 146
31 539 1700 0 128 34
32 539 1800 0 67 27
33 539 1900 0 45 22
34 539 2000 0 24 13
35 539 2100 0 16 11
36 539 2200 0 28 5
37 539 2300 0 23 8
38 539 2400 0 73 11
39 539 100 0 16 3
40 539 200 0 243 82
41 539 300 0 121 55
42 539 400 0 17 4
43 539 500 0 5 5
44 539 600 0 41 15
45 539 700 0 101 34
46 539 800 0 184 43
47 539 900 0 69 15
48 539 1000 0 51 18
49 539 1100 0 61 25The above data showed is for only one"TRAFFIC_SAMPLE_ID"=539, what i want is i should ignore the first record and starting from the second record (in this example "END_INV_TIME" 1200) I need to find its pair which in this case is another 1200 and then find the average of AVG{(BIN_1_DATA+BIN_2_DATA+BIN_3_DATA) of day 1 +(BIN_1_DATA+BIN_2_DATA+BIN_3_DATA) of day 2}, same is the case with rest all of teh records 1300,1400,1500.......
ThanksIs this what you are looking for?
Use a self-join with the "<" condition on the ID columns to make sure you don't join a row to itself or get two rows for each join.
WITH
csvs as
( select '1,539,1100,0,34,19' x from dual union all
select '2,539,1200,0,65,18' x from dual union all
select '3,539,1300,0,51,17' x from dual union all
select '4,539,1400,0,65,27' x from dual union all
select '5,539,1500,0,99,48' x from dual union all
select '6,539,1600,0,426,138' x from dual union all
select '7,539,1700,0,151,62' x from dual union all
select '8,539,1800,0,80,32' x from dual union all
select '9,539,1900,0,31,11' x from dual union all
select '10,539,2000,0,37,11' x from dual union all
select '11,539,2100,0,24,9' x from dual union all
select '12,539,2200,0,16,5' x from dual union all
select '13,539,2300,0,27,12' x from dual union all
select '14,539,2400,0,55,20' x from dual union all
select '15,539,100,0,18,9' x from dual union all
select '16,539,200,0,134,52' x from dual union all
select '17,539,300,0,230,69' x from dual union all
select '18,539,400,0,15,7' x from dual union all
select '19,539,500,0,6,5' x from dual union all
select '20,539,600,0,47,23' x from dual union all
select '21,539,700,0,100,41' x from dual union all
select '22,539,800,0,196,43' x from dual union all
select '23,539,900,0,81,20' x from dual union all
select '24,539,1000,0,58,28' x from dual union all
select '25,539,1100,0,58,24' x from dual union all
select '26,539,1200,0,60,22' x from dual union all
select '27,539,1300,0,42,18' x from dual union all
select '28,539,1400,0,53,15' x from dual union all
select '29,539,1500,0,107,43' x from dual union all
select '30,539,1600,0,441,146' x from dual union all
select '31,539,1700,0,128,34' x from dual union all
select '32,539,1800,0,67,27' x from dual union all
select '33,539,1900,0,45,22' x from dual union all
select '34,539,2000,0,24,13' x from dual union all
select '35,539,2100,0,16,11' x from dual union all
select '36,539,2200,0,28,5' x from dual union all
select '37,539,2300,0,23,8' x from dual union all
select '38,539,2400,0,73,11' x from dual union all
select '39,539,100,0,16,3' x from dual union all
select '40,539,200,0,243,82' x from dual union all
select '41,539,300,0,121,55' x from dual union all
select '42,539,400,0,17,4' x from dual union all
select '43,539,500,0,5,5' x from dual union all
select '44,539,600,0,41,15' x from dual union all
select '45,539,700,0,101,34' x from dual union all
select '46,539,800,0,184,43' x from dual union all
select '47,539,900,0,69,15' x from dual union all
select '48,539,1000,0,51,18' x from dual union all
select '49,539,1100,0,61,25' x from dual
, wdata as
( select to_number(regexp_replace( x , '^(.*),(.*),(.*),(.*),(.*),(.*)$' , '\1' )) as bin_data_id
, to_number(regexp_replace( x , '^(.*),(.*),(.*),(.*),(.*),(.*)$' , '\2' )) as traffic_sample_id
, to_number(regexp_replace( x , '^(.*),(.*),(.*),(.*),(.*),(.*)$' , '\3' )) as end_intv_time
, to_number(regexp_replace( x , '^(.*),(.*),(.*),(.*),(.*),(.*)$' , '\4' )) as bin_1_data
, to_number(regexp_replace( x , '^(.*),(.*),(.*),(.*),(.*),(.*)$' , '\5' )) as bin_2_data
, to_number(regexp_replace( x , '^(.*),(.*),(.*),(.*),(.*),(.*)$' , '\6' )) as bin_3_data
from csvs
SELECT a.traffic_sample_id
, a.end_intv_time
, ((a.bin_1_data + a.bin_2_data + a.bin_3_data) + (b.bin_1_data + b.bin_2_data + b.bin_3_data)) / 2 as av
from wdata a
join wdata b on ( a.traffic_sample_id = b.traffic_sample_id and
a.end_intv_time = b.end_intv_time and
a.bin_data_id < b.bin_data_id )
order by 1, 2 ; -
Need help with complex query with comma seperated
Oracle version - 11.1.0.7.0
Consider there are two tables table1 and table2
Key---- ID
A ---- 1
A ---- 2
A---- 3
B ---- 4
B ---- 5
C ---- 6
C ---- 8
C ---- 9
C ---- 10
D ---- 11
D ---- 12
Table2
ID
1
2
6
8
11
12
I need result as in usedID column I should get comma seperated list of ID which are in table1 and table2 and in NOTUSEDID comma seperated list of ID which are in table1 but not in table2
Key---- USEDID---- NOTUSEDID
A ---- 1,2 ---- 3
B ---- null ---- 4,5
C ---- 6,8 ---- 9,10
D ---- 11,12 ---- nullHi!
Solution for Oracle 11g:
SELECT A.KEY,
listagg(decode(b.id, null, null, a.id), ',') WITHIN GROUP (ORDER BY A.ID) AS USEDID,
listagg(decode(b.id, null, a.id), ',') WITHIN GROUP (ORDER BY A.ID) AS NOTUSEDID
FROM TABLE1 A
LEFT OUTER JOIN table2 b
ON (A.ID = b.ID)
GROUP BY a.key;
Solution for databases prior to Oracle 11g:
Please note that the function wm_concat is undocumented and can't be sorted!
SELECT A.KEY,
wm_concat(decode(b.id, null, null, a.id)) AS USEDID,
wm_concat(decode(b.id, null, a.id)) AS NOTUSEDID
FROM TABLE1 A
LEFT OUTER JOIN table2 b
ON (A.ID = b.ID)
GROUP BY A.KEY;I used the following test case:
create table table1(key varchar2(255), id number);
CREATE TABLE table2(ID NUMBER);
insert into table1 values('A', 1);
INSERT INTO table1 VALUES('A', 2);
INSERT INTO table1 values('A', 3);
INSERT INTO table1 VALUES('B', 4);
INSERT INTO table1 VALUES('B', 5);
INSERT INTO table1 VALUES('C', 6);
INSERT INTO table1 values('C', 8);
INSERT INTO table1 VALUES('C', 9);
INSERT INTO table1 VALUES('C', 10);
INSERT INTO table1 values('D', 11);
insert into table1 values('D', 12);
insert into table2 values(1);
insert into table2 values(2);
insert into table2 values(6);
insert into table2 values(8);
insert into table2 values(11);
insert into table2 values(12);
commit;Best regards,
Matt
Edited by: Matt Schulz on Oct 12, 2011 12:43 PM -
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 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 -
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 -
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 -
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
-
I've got a 2GHz dual-processor G5 that has been running great since new. I recently added additional memory (purchased from OWC) and since then I get crashes from various apps at random times. The most frequent culprit is Activity Monitor; invariably
-
"No Airport card installed" after 10.5.8 update
Just upgraded the latest Leopard update 10.5.8 and, after restart, the Airport icon in my menu bar says "No Airport card installed". I've been into System Prefs and tried to switch it on, but nothing. I've even been into the system profiler and, when
-
Maintenance of Condition records in CRM
Hi all, Environment CRM 4.0, R/3 4.6c, where do i create condition records in CRM? we have pricing procedure,Document pricing procedure,Customer pricing procedure and Sales area in place and also configured Pricing procedure determination, assigned D
-
Someone stole my 27 imac, is there any way to track it?
My 27" IMac was stolen. is there any way to track it by using the serial number?
-
i am using wcf-sql adopter .. Getting bellow warning..! WcfSendPort_SqlAdapterBinding_Procedures_DM_Custom mssql://sdasyapu01//CVM_Testnet_DryRun20140122_CSD? Microsoft.ServiceModel.Channels.Common.XmlReaderParsingException: The start element with na