How to formulate this Query ??

Hello Everyone,
I have a table structure like this....
Courses -- which contain the list of courses...with course_id being primary key
Tasks -- which contains the list of courses...with task_id being the primary key
Course History -- contains list of completed courses by the user...using emp_id and course_id being forign keys
Task History -- contains list of completed tasks by the user...using emp_id and task_id being forign keys
Temp_course_registration -- its pretty similar to course_history table, only difference is when the record gets completed i delete from registartion and move to Course_History table.....
Job_2_courses -- tells which courses are related with job...
job_2_tasks -- tells which taks are related to a job......
In a course_history table one record can be twice only difference will be date...I want the record with latest date......There is also a status field that can be different in some repetitive cases most will be same...the records are repetitive because they expire some after 6 months, some after 1 year or so on.......
The process goes something along these lines.....user can only take courses and tasks which are related with their job.....there can be a case when they take a course or task outside a job role....
they register for the course, someone approves it.....then someone schedules it......all these records get updated in temp_course_registration table.....using the status......
and once they successfully complete the course, it gets deleted from temp_course_registration and moved to course_history....
Now i want to show the user complete history what they have done and when, what they are eleigible to take or they are in the process.....
Anyone can let me know the best way to do it...
Thanks,
Harsimrat

Some more Related Information
These are all the DDL's
Course_List Table
CREATE TABLE course_list
course_id VARCHAR2(20) NOT NULL,
description VARCHAR2(4000) NOT NULL,
name VARCHAR2(4000),
expiry_date VARCHAR2(400),
course_type VARCHAR2(400)
PCTFREE 10
PCTUSED
INITRANS 1
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
-- Constraints for COURSE_LIST
ALTER TABLE course_list
ADD CONSTRAINT pk_course_id PRIMARY KEY (course_id)
USING INDEX
PCTFREE 10
INITRANS 2
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
ALTER TABLE course_list
ADD CHECK ("COURSE_ID" IS NOT NULL)
ALTER TABLE course_list
ADD CHECK ("DESCRIPTION" IS NOT NULL)
Task_List Table
CREATE TABLE task_list
task_id NUMBER(*,0) NOT NULL,
description VARCHAR2(250) NOT NULL,
name VARCHAR2(250)
PCTFREE 10
PCTUSED
INITRANS 1
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
-- Constraints for TASK_LIST
ALTER TABLE task_list
ADD CONSTRAINT pk_task_list_task_id PRIMARY KEY (task_id)
USING INDEX
PCTFREE 10
INITRANS 2
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
ALTER TABLE task_list
ADD CHECK ("TASK_ID" IS NOT NULL)
ALTER TABLE task_list
ADD CHECK ("DESCRIPTION" IS NOT NULL)
Course_History Table
CREATE TABLE course_history
employee_email VARCHAR2(50) NOT NULL,
course_id VARCHAR2(20) NOT NULL,
course_date DATE,
manager_name VARCHAR2(150) NOT NULL,
course_status NUMBER(*,0),
decision VARCHAR2(10)
PCTFREE 10
PCTUSED
INITRANS 1
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
-- Constraints for COURSE_HISTORY
ALTER TABLE course_history
ADD CONSTRAINT fk_course_hist_cs FOREIGN KEY (course_status)
REFERENCES COURSE_STATUS(course_status)
ALTER TABLE course_history
ADD CONSTRAINT fk_course_hist_employee_email FOREIGN KEY (employee_email)
REFERENCES EMPLOYEE(employee_email)
ALTER TABLE course_history
ADD CONSTRAINT fk_course_id_history FOREIGN KEY (course_id)
REFERENCES COURSE_LIST(course_id)
ALTER TABLE course_history
ADD CHECK ("EMPLOYEE_EMAIL" IS NOT NULL)
ALTER TABLE course_history
ADD CHECK ("COURSE_ID" IS NOT NULL)
ALTER TABLE course_history
ADD CHECK ("MANAGER_NAME" IS NOT NULL)
Task History Table
CREATE TABLE task_history
employee_email VARCHAR2(50) NOT NULL,
task_id NUMBER(*,0) NOT NULL,
task_date DATE NOT NULL,
sme_name VARCHAR2(150) NOT NULL,
status NUMBER(*,0),
rsme_name VARCHAR2(150)
PCTFREE 10
PCTUSED
INITRANS 1
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
-- Constraints for TASK_HISTORY
ALTER TABLE task_history
ADD CONSTRAINT fk_task_history_a_t_s FOREIGN KEY (status)
REFERENCES ASSOCIATE_TASK_STATUS(status)
ALTER TABLE task_history
ADD CONSTRAINT fk_task_history_employee_email FOREIGN KEY (employee_email)
REFERENCES EMPLOYEE(employee_email)
ALTER TABLE task_history
ADD CONSTRAINT fk_task_history_task_list FOREIGN KEY (task_id)
REFERENCES TASK_LIST(task_id)
ALTER TABLE task_history
ADD CHECK ("EMPLOYEE_EMAIL" IS NOT NULL)
ALTER TABLE task_history
ADD CHECK ("TASK_ID" IS NOT NULL)
ALTER TABLE task_history
ADD CHECK ("TASK_DATE" IS NOT NULL)
ALTER TABLE task_history
ADD CHECK ("SME_NAME" IS NOT NULL)
Temp_Course_Registration
CREATE TABLE temp_course_registration
employee_email VARCHAR2(50) NOT NULL,
course_id VARCHAR2(50) NOT NULL,
course_date DATE NOT NULL,
manager_name VARCHAR2(150) NOT NULL,
approval_manager VARCHAR2(150),
course_status NUMBER(*,0),
notes VARCHAR2(500)
PCTFREE 10
PCTUSED
INITRANS 1
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
-- Constraints for TEMP_COURSE_REGISTRATION
ALTER TABLE temp_course_registration
ADD CHECK ("EMPLOYEE_EMAIL" IS NOT NULL)
ALTER TABLE temp_course_registration
ADD CHECK ("COURSE_ID" IS NOT NULL)
ALTER TABLE temp_course_registration
ADD CHECK ("COURSE_DATE" IS NOT NULL)
ALTER TABLE temp_course_registration
ADD CHECK ("MANAGER_NAME" IS NOT NULL)
Job_2_Courses
CREATE TABLE job_2_courses
course_id VARCHAR2(20) NOT NULL,
job_id NUMBER(*,0) NOT NULL
PCTFREE 10
PCTUSED
INITRANS 1
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
-- Constraints for JOB_2_COURSES
ALTER TABLE job_2_courses
ADD CONSTRAINT fk_job_2_courses_course_id FOREIGN KEY (course_id)
REFERENCES COURSE_LIST(course_id)
ALTER TABLE job_2_courses
ADD CONSTRAINT fk_job_id_j2c FOREIGN KEY (job_id)
REFERENCES JOB_LIST(job_id)
ALTER TABLE job_2_courses
ADD CHECK ("COURSE_ID" IS NOT NULL)
ALTER TABLE job_2_courses
ADD CHECK ("JOB_ID" IS NOT NULL)
Job_2_Tasks
CREATE TABLE job_2_tasks
job_id NUMBER(*,0) NOT NULL,
task_id NUMBER(*,0) NOT NULL
PCTFREE 10
PCTUSED
INITRANS 1
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
-- Constraints for JOB_2_TASKS
ALTER TABLE job_2_tasks
ADD CONSTRAINT fk_j2t_job_id FOREIGN KEY (job_id)
REFERENCES JOB_LIST(job_id)
ALTER TABLE job_2_tasks
ADD CONSTRAINT fk_job_2_tasks_task_id FOREIGN KEY (task_id)
REFERENCES TASK_LIST(task_id)
ALTER TABLE job_2_tasks
ADD CHECK ("JOB_ID" IS NOT NULL)
ALTER TABLE job_2_tasks
ADD CHECK ("TASK_ID" IS NOT NULL)
Job_List Table
CREATE TABLE job_list
job_id NUMBER(*,0) NOT NULL,
description VARCHAR2(4000) NOT NULL,
name VARCHAR2(100)
PCTFREE 10
PCTUSED
INITRANS 1
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
-- Constraints for JOB_LIST
ALTER TABLE job_list
ADD CONSTRAINT pk_job_id_job_list PRIMARY KEY (job_id)
USING INDEX
PCTFREE 10
INITRANS 2
MAXTRANS 255
TABLESPACE users
STORAGE (
INITIAL 65536
NEXT
PCTINCREASE
MINEXTENTS 1
MAXEXTENTS 2147483645
FREELIST GROUPS 0
FREELISTS 0
ALTER TABLE job_list
ADD CHECK ("JOB_ID" IS NOT NULL)
ALTER TABLE job_list
ADD CHECK ("DESCRIPTION" IS NOT NULL)
I did this query, it takes care of everything except COURSE_HISTORY table...I need to include that as well
SELECT cl.description, cl.name, NVL(ch.course_date,to_date('January 01, 2044','Month DD, YYYY' )) "COURSE_DATE",
cl. course_id, NVL(ch.course_status,'5') "STATUS"
from employee e, job_list jl, job_2_courses j2c, course_list cl,
(select course_id,course_status, course_date from temp_course_registration
where employee_email = vUserName) ch
where e.job_id = jl.job_id AND
jl.job_id = j2c.job_id AND
j2c.course_id = cl.course_id AND
cl.course_id = ch.course_id(+) AND
e.employee_email = vUserName order by 1 desc;

Similar Messages

  • How to use this query in R12

    the query below , i am using in valueset :
    select * from AR_LOCATION_VALUES
    Where ar_location_values.location_segment_qualifier =
    'COUNTRY' and ar_location_values.location_structure_id in ( select location_structure_id from ar_system_parameters )
    how to use this query to set in R12?
    Thanks

    hi
    i am using the following query in 11i and i want to use the same in R12 :
    SELECT ar_location_values.location_segment_description,ar_location_values.location_segment_value,location_segment_id
    FROM AR_LOCATION_VALUES
    WHERE ar_location_values.location_segment_qualifier = 'COUNTRY'
    AND ar_location_values.location_structure_id IN
    (SELECT location_structure_id FROM ar_system_parameters)
    note: the table ar_location_values is obsolette in R12 so what is the replacement table in R12 for this?

  • How to modify this query to get the desired output format

    I hv written a Query to display all the parent table names and their primary key columns(relevant to this foreign key of the child table).The query is given below...
    SELECT DISTINCT(TABLE_NAME) AS PARENT_TABLE,COLUMN_NAME AS PARENT_COLUMN
    FROM ALL_CONS_COLUMNS
    WHERE CONSTRAINT_NAME IN (SELECT AC.R_CONSTRAINT_NAME
    FROM ALL_CONSTRAINTS AC
    WHERE AC.TABLE_NAME=TABLE_NAME
    AND AC.TABLE_NAME='&TABLE'
    AND AC.R_CONSTRAINT_NAME IS NOT NULL);
    This query will display all the parent tables and their primary key columns.Now my problem is that how to modify this query to also display the foreign key column name of the child table.
    I want the query result in the following format.The query should display the following columns.1)child table's name,2)child table's foreign key column name,3)the corresponding parent table's name,4)the parent table's primary key column name(which is the foreign key in the child table).
    For Example I want the output as follows...
    TAKE THE CASE OF SCOTT.EMP(AS INPUT TO YOUR QUERY)
    CHILD_TABLE CHILD_COLUMN PARENT_TABLE PARENT_COLUMN
    EMP DEPTNO DEPT DEPTNO
    In this result I hv used alias name for the columns.The query should display this only for the foreign keys in the child table.In the query which I sent to you earlier will give the parent table and the parent column names,But I also want to append the child table and child column names there.
    any help on how to tackle would be appreciated.

    Try this query
    SELECT c.table_name child_table,
         c.column_name child_column,
         p.table_name parent_table,
         p.column_name parent_column
    FROM user_constraints a,user_constraints b,user_cons_columns c,
         user_cons_columns p
    WHERE a.r_constraint_name=b.constraint_name and
          a.constraint_name=c.constraint_name and
          b.constraint_name=p.constraint_name and
          c.position=p.position
    ORDER BY c.constraint_name,c.position
    Anwar

  • How to rewrite this query without sub query please help me

    Hello All Good Evening,
    Could you please help me with this query, how can i write this query without sub query, or how can write this query another ways
    please help me
    select planno, status1, count(*) Counts from
    select a.ValetNO PlanNo  ,
    case 
         when JoinCode in ('00', '01', '02') then 'Actcess'
         when JoinCode in ('20', '21', '22', '23','38', '39') then
         'Secured' else 'Other' end Status1 ---, COUNT (*)
       from  dbo.ppt a(NOLOCK)  left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO  = b.ValetNO
    --group by a.ValetNO
      a group by planno, status1
    order by 2
    Thank you in Advance
    Milan

    Whats your objective here? Sorry, am not able to understand the reason for this change. 
    Try the below:(Not tested)
    ;With cte
    As
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 ---, COUNT (*)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    select planno, status1, count(*) Counts from cte
    a group by planno, status1
    order by 2
    Even below:
    select a.ValetNO PlanNo ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end Status1 , COUNT (1)
    from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
    Group by a.ValetNO ,
    case
    when JoinCode in ('00', '01', '02') then 'Actcess'
    when JoinCode in ('20', '21', '22', '23','38', '39') then
    'Secured' else 'Other' end

  • How to solve this query

    hi,
    i have a product table like
    product month1 month2 month3 .................
    soap 1200 1256 1895 ............
    i want use a query where i can select column name with a parameter.
    like
    select month||:num from product;
    in num variable it cud be 1 to 10 of value that is dependent on my program.
    so how to make this query .
    thxs

    Hi,
    Here is an example that i am helpful.
    In the example , I am using a table 'table_name' which contains columns like
    assign_attribute1
    assign_attribute2
    assign_attribute15
    Now I will pass any number from 1 to 15 to the function.
    create or replace procedure pass_col_number(v_number varchar2) as
    v_sql varchar2(2000);
    v_assign_attribute1   varchar2(150);
    begin
    v_sql := 'select  assign_attribute'||v_number||'  from  table_name where person_id = 1345';
    execute immediate v_sql into v_assign_attribute1;
    dbms_output.put_line('v_assign_attribute1='||v_assign_attribute1);
    end;Edited by: Sreekanth Munagala on Dec 18, 2008 1:18 AM

  • How to combine this query so that i can display the ouput together

    I have no idea how to combine this query together.Someone please help.I want the ouput to display oni 1 result combining all together.
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('6910','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('6912','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('7344','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in ('8344','7976') AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    Edited by: 965547 on Nov 5, 2012 12:55 AM

    The only difference which i am seeing in your queries is the Operation ('6910', '6912','7344','8344')
    Then why don't you put all values in One like this
    Select facility, route, operation, script_id
    from F_ROUTEOPER
    where facility = 'A01' and operation in *('6910', '6912','7344','8344','7976')* AND src_erase_date is null
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null)
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%'AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%')
    Hope this will resolve your problem.
    Oracle-911

  • How to tune this query for the improve performance ?

    Hi All,
    How to tune this query for the improve performance ?
    select a.claim_number,a.pay_cd,a.claim_occurrence_number,
    case
    when sum(case
    when a.payment_status_cd ='0'
    then a.payment_est_amt
    else 0
    end
    )=0
    then 0
    else (sum(case
    when a.payment_status_cd='0'and a.payment_est_amt > 0
    then a.payment_est_amt
    else 0
    end)
    - sum(case
    when a.payment_status_cd<>'0'
    then a.payment_amt
    else 0
    end))
    end as estimate
    from ins_claim_payment a
    where a.as_of_date between '31-jan-03' and '30-aug-06'
    and ( a.data_source = '25' or (a.data_source between '27' and '29'))
    and substr(a.pay_cd,1,1) IN ('2','3','4','8','9')
    group by a.claim_number, a.pay_cd, a.claim_occurrence_number
    Thank you,
    Mcka

    Mcka
    As well as EXPLAIN PLAN, let us know what proportion of rows are visited by this query. It may be that it is not using a full table scan when it should (or vice versa).
    And of course we'd need to know what indexes are available, and how selective they are for the predicated you have in this query ...
    Regards Nigel

  • How to write this query to filter combination of few values

    Hi,
    I have a table CHIMM which is a transaction table and contains information of the vaccines given to a child.
    columns are: child_id, vacc_id, vacc_given_dt. I have to query for remaining vaccines.
    HEXA is a vaccine_id which is composite vaccine of DPT1,POL1,HBV1 & HIB1 (vaccine ids).
    I want to write to query if any of DPT1,POL1,HBV1 & HIB1 given then HEXA should not be displayed in the result.
    OR
    if HEXA is given then of course any of DPT1,POL1,HBV1 & HIB1 should not be displayed in the result.
    How to write this query?
    Regards

    Hi,
    I'm still not sure what the output you want from that sample data is. Do you just want the child_ids, like this
    CHILD_ID
           3
           4? If so, here's one way to get them:
    WITH     all_vacc_ids     AS
         SELECT     c.child_id
         ,     c.vacc_id          AS child_vacc_id
         ,     v.vacc_id
         ,     COUNT ( CASE
                             WHEN  c.vacc_id = 'HEXA'
                       THEN  1
                         END
                    )       OVER ( PARTITION BY  c.child_id
                                       )    AS hexa_itself
         FROM          vacc   v
         LEFT OUTER JOIN     chimm  c     PARTITION BY (c.child_id)
                          ON     c.vacc_id     = v.vacc_id
         WHERE   v.vacc_desc       = 'HEXA'     -- See note below
    SELECT       child_id
    FROM       all_vacc_ids
    WHERE       child_vacc_id     IS NULL
      AND       vacc_id     != 'HEXA'
      AND       hexa_itself     = 0
    GROUP BY  child_id
    rha2 wrote:there are alot of vaccines, i just put 3 for example. this query gives error: invalid relational operatorAre you saying that the vacc table contains other rows, but those other rows are not needed for this problem? It would be good if you included an example in the sample data. The query above considers only the rows in vacc where vacc_desc='HEXA'. You can have other rows in the vacc table, but they won't affect the output of this query. The query above makes no assumptions about the number of rows that have vacc_desc='HEXA'; it will report all child_ids who are missing any of them, regardless of the number (assuming the child does not have the 'HEXA' vacc_id itself, like child_id=1).
    You still haven't said which version of Oracle you're using. The query above will work in Oracle 10 (and higher).

  • How to write this query ?

    how to write this query ?
    list the emp name who is working for the highest avg sal department.
    I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?
    the row_number version is like this
    select emp.* from emp ,
    select deptno, row_number() over (order by avg(sal) desc) r from emp
    group by deptno
    )e
    where e.r = 1
    and emp.deptno = e.deptno

    Hi,
    806540 wrote:
    how to write this query ?
    list the emp name who is working for the highest avg sal department.
    I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?ROW_NUMBER is just plain SQL, and has been since Oracle 8.1.
    ROW_NUMBER (or its close relative, RANK) is the simplest and most efficient way to solve this problem. Why not do this the right way?
    the row_number version is like this
    select emp.* from emp ,
    select deptno, row_number() over (order by avg(sal) desc) r from emp
    group by deptno
    )e
    where e.r = 1
    and emp.deptno = e.deptno
    If there happens to be a tie (that is, two or more departments have the same average sal, and it is the highest), then the query above will only arbitrarily treat one of them (no telling which one) as the highest. Change ROW_NUMBER to RANK to get all departments with a claim to having the highest average sal.
    You could use the ROWNUM pseudo-column instead of ROW_NUMBER, if all you want to do is avoid ROW_NUMBER.
    Without using ROW_NUMBER or RANK, there are lots of ways involving other analytic functions, such as AVG and MAX.
    If you really, really don't want to use analytic functions at all, you can do this:
    SELECT     *
    FROM     scott.emp
    WHERE     deptno     IN  (
                      SELECT       deptno
                      FROM       scott.emp
                      GROUP BY  deptno
                      HAVING       AVG (sal) =  (
                                                       SELECT    MAX (AVG (sal))
                                               FROM          scott.emp
                                               GROUP BY  deptno
    ;

  • How to improve this query speed ?....help me

    How to improve the query speed. Any hints can u suggest in the query or any correction. Here i am using sample tables for checking purpose, When i am trying with my original values, this type of query taking longer time to run
    select ename,sal,comm from emp where(comm is null and &status='ok') or (comm is not null and &status='failed');
    Thanx in advance
    prasanth a.s.

    What about
    select ename,sal,comm from emp where comm is null and &status='ok'
    union all
    select ename,sal,comm from emp where comm is not null and &status='failed';
    Regards
    Vaishnavi

  • How to rewrite this query

    How can i rewrite this query so it uses less inner joins (it causes my temp space to explode :) )
    ( SELECT Waardes.*, Tellers.Nr_Dataitems, Tellers.Nr_Details FROM
    ( SELECT extractvalue(Value(el_node), 'Node/Id') node,
    extractvalue(Value(el_dataitem), 'Detail/Title') Node_Title,
    extractvalue(Value(el_dataitem), 'Detail/Value') Node_Value
    FROM PHILIPS_XML x,
    TABLE (xmlsequence (extract (value(x), '/Tree/Node'))) el_node,
    TABLE (xmlsequence (extract (value(el_node),
    'Node/DataItems/DataItem/DetailSet/Detail'))) el_dataitem
    ) Waardes
    INNER JOIN
    SELECT A.Node, A.Nr_Dataitems, B.Nr_details FROM
    SELECT extractvalue(Value(el_node), 'Node/Id') node,
    count(extract(Value(aaa), 'DataItem')) Nr_Dataitems
    FROM PHILIPS_XML x,
    TABLE (xmlsequence (extract (value(x), '/Tree/Node'))) el_node,
    TABLE (xmlsequence (extract (value(el_node),
    'Node/DataItems/DataItem'))) aaa
    GROUP BY extractvalue(Value(el_node), 'Node/Id')
    ) A
    INNER JOIN
    SELECT extractvalue(Value(el_node), 'Node/Id') node,
    count(extract(Value(bbb), 'Detail')) Nr_details
    FROM PHILIPS_XML x,
    TABLE (xmlsequence (extract (value(x), '/Tree/Node'))) el_node,
    TABLE (xmlsequence (extract (value(el_node),
    'Node/DataItems/DataItem'))) aaa,
    TABLE (xmlsequence (extract (value(aaa),
    'DataItem/DetailSet/Detail' ))) bbb
    GROUP BY extractvalue(Value(el_node), 'Node/Id')
    ) B
    on A.node = B.NODE
    ) Tellers
    ON Waardes.NODE = Tellers.NODE )

    1st Make sure all paths are absolute (start with '/', not just the node name).
    Which release are you using.. Is the document based on an XMLSchema, if so, what annotations were used when registered the XML Schema ?. What does the explain plan look like...

  • How to optmize this query

    How can I optimize this query? it's taking a long time to run
    SELECT A.COL1, B.COL2, C.COL3
    FROM A, B, C
    WHERE A.COL1=B.COL2 (+)
    AND B.COL2=C.COL3 (+)
    Thank you!
    H

    ORA-02393: exceeded call limit on CPU usageContact your dba to increase this resource limit in your profile.
    An other way to write your query :
    select a.col1, null, null
    from a
    where not exists (select null from b where A.COL1=B.COL2)
    union
    select a.col1, b.col2, null
    from a,b
    where A.COL1=B.COL2
    and not exists (select null from c where b.COL1=c.COL2)
    union
    select a.col1, b.col2, c.col3
    from a,b, c
    where A.COL1=B.COL2
    and B.COL2=C.COL3;[pre]
    Nicolas.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • How to update this query and avoid performance issue?

    Hi, guys:
    I wonder how to update the following query to make it weekend day aware. My boss want the query to consider business days only. Below is just a portion of the query:
    select count(distinct cmv.invoicekey ) total ,'3' as type, 'VALID CALL DATE' as Category
    FROM cbwp_mv2 cmv
    where cmv.colresponse=1
    And Trunc(cmv.Invdate)  Between (Trunc(Sysdate)-1)-39 And (Trunc(Sysdate)-1)-37
    And Trunc(cmv.Whendate) Between cmv.Invdate+37 And cmv.Invdate+39the CBWP_MV2 is a materialized view to tune query. This query is written for a data warehouse application, the CBWP_MV2 will be updated every day evening. My boss wants the condition in the query to consider only business days, for example, if (Trunc(Sysdate)-1)-39 falls in weekend, I need to move the range begins from next coming business day, if (Trunc(Sysdate)-1)-37 falls in weekend, I need to move the range ends from next coming business day. but I should always keep the range within 3 business days. If there is overlap on weekend, always push to later business days.
    Question: how to implement it and avoid performance issue? I am afraid that if I use a function, it greatly reduce the performance. This view already contains more than 100K rows.
    thank you in advance!
    Sam
    Edited by: lxiscas on Dec 18, 2012 7:55 AM
    Edited by: lxiscas on Dec 18, 2012 7:56 AM

    You are already using a function, since you're using TRUNC on invdate and whendate.
    If you have indexes on those columns, then they will not be used because of the TRUNC.
    Consider omitting the TRUNC or testing with Function Based Indexes.
    Regarding business days:
    If you search this forum, you'll find lots of examples.
    Here's another 'golden oldie': http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:185012348071
    Regarding performance:
    Steps to take are explained from the links you find here: {message:id=9360003}
    Read them, they are more than worth it for now and future questions.

  • How to interpret this query

    Hi ,
    I have this query written by someone else earlier and need to debug
    SELECT DISTINCT
        TBLA.ID "ID",
        SUBSTR (TBLA.ID, 1, INSTR (TBLA.ID, '.') - 1) "P_NAME",
        TBLB.ID "ST_ID",
        TBLB.PRCD_NAME "ST_NAME",
        TBLA.INST_NUMBER "ORDER",
        TBLA.INST_TYPE "ST_TYPE",
        TBLA.ST_ID "R_STAGE",
        ST_PRCD_INST.R_NAME "R_NAME",
        ST_PRCD_INST.INST_NUMBER "R_ORDER",
        ST_PRCD_INST.INST_TYPE "R_TYPE"
       FROM
        TBLA,
        TBLB,
        TBLA ST_PRCD_INST
       WHERE
        TBLA.CALL_P_NAME IS NOT NULL
        AND TBLA.CALL_PRCD_NAME = TBLB.PRCD_NAME
        AND TBLB.PRCD_ACTIVE_FLAG = 'A'
        AND TBLB.ID = ST_PRCD_INST.ID
        AND (TBLB.OBS_FLAG = 'N' OR TBLB.OBS_FLAG IS NULL)how does Oracle intrprets this query as i do not understand the results returned
    appreciate ur advise
    tks & rdgs

    SELECT DISTINCT TBLA.ID "ID",    
           SUBSTR (TBLA.ID, 1, INSTR (TBLA.ID, '.') - 1) "P_NAME",
           TBLB.ID "ST_ID",
           TBLB.PRCD_NAME "ST_NAME",
           TBLA.INST_NUMBER "ORDER",
           TBLA.INST_TYPE "ST_TYPE",
           TBLA.ST_ID "R_STAGE",
           ST_PRCD_INST.R_NAME "R_NAME",
           ST_PRCD_INST.INST_NUMBER "R_ORDER",
           ST_PRCD_INST.INST_TYPE "R_TYPE"
      FROM TBLA,
           TBLB,    
           TBLA ST_PRCD_INST   
    WHERE TBLA.CALL_P_NAME IS NOT NULL
       AND TBLA.CALL_PRCD_NAME   = TBLB.PRCD_NAME
       AND TBLB.PRCD_ACTIVE_FLAG = 'A'
       AND TBLB.ID               = ST_PRCD_INST.ID(+)
       AND (TBLB.OBS_FLAG = 'N' OR TBLB.OBS_FLAG IS NULL)try to add the join (+) and see if it works.

  • How to do this Query ??

    I have a table structure something like this......Jobs and Courses......Different jobs are related with different courses....
    If I do this query :
    SELECT * FROM
    JOB_2_COURSES
    WHERE JOB_ID = '1'
    Resul set
    S101
    B102
    B103
    SS
    S1
    If I do this query
    SELECT * FROM
    JOB_2_COURSES
    WHERE JOB_ID = '157'
    Result Set
    B102
    B103
    TUPAC
    How can I join the above two queries and I'm only looking for TUPAC....
    Thanks,
    Harsimrat

    Hey I tried before didn't work for me.....
    SELECT * FROM JOB_2_COURSES
    WHERE JOB_ID = '157'
    minus
    SELECT * FROM
    JOB_2_COURSES
    WHERE JOB_ID = '1'
    ----Result ------
    COURSE_ID     JOB_ID
    BUS101     157
    SS     157
    SELECT * FROM
    JOB_2_COURSES
    WHERE JOB_ID = '1'
    ----Result Set -----
    COURSE_ID     JOB_ID
    BUS101     1
    QA102     1
    TTT102     1
    XS     1
    SOL102     1
    BUS102     1
    BUS103     1
    SOL101     1
    610     1
    TTT101     1
    QA101     1
    WHMIS     1
    BC0100     1
    RES101     1
    HS     1
    ESD101     1
    SELECT * FROM
    JOB_2_COURSES
    WHERE JOB_ID = '157'
    ---Result Set-----------
    COURSE_ID     JOB_ID
    BUS101     157
    SS     157
    The only result I'm looking back is SS 157
    Message was edited by:
    Harsimrat

Maybe you are looking for