How this query can be reduced

Hi all,
I have placed some repeating conditions in many places of the query , Please suggest me to reduce the size of the query .
SELECT (CASE
WHEN prs.rqst_ctgry_lkpcd <> 'AR'
AND prp.x12_code_list_qlfr_lkpcd <> 'HC'
THEN prp.x12_code_list_qlfr_lkpcd
END
) x12_code_list_qlfr_lkpcd,
(CASE
WHEN prs.rqst_ctgry_lkpcd <> 'AR'
AND prp.x12_code_list_qlfr_lkpcd <> 'HC'
THEN (SELECT prcdr_code
FROM PROCEDURE
WHERE procedure_iid = prp.procedure_iid) end) procedure_iid,
(CASE
WHEN prs.rqst_ctgry_lkpcd <> 'AR'
AND prp.x12_code_list_qlfr_lkpcd <> 'HC'
THEN prp.mdfr_code end) mdfr_code, (CASE
WHEN prs.rqst_ctgry_lkpcd <> 'AR'
AND prp.x12_code_list_qlfr_lkpcd <> 'HC'
THEN prp.mdfr2_code end) mdfr2_code,
(CASE
WHEN prs.rqst_ctgry_lkpcd <> 'AR'
AND prp.x12_code_list_qlfr_lkpcd <> 'HC'
THEN prp.mdfr3_code end) mdfr3_code, (CASE
WHEN prs.rqst_ctgry_lkpcd <> 'AR'
AND prp.x12_code_list_qlfr_lkpcd <> 'HC'
THEN prp.mdfr4_code end) mdfr4_code,
(CASE
WHEN prs.rqst_ctgry_lkpcd <> 'AR'
AND prp.x12_code_list_qlfr_lkpcd <> 'HC'
THEN prp.drug_desc end) drug_desc, (CASE
WHEN prs.rqst_ctgry_lkpcd <> 'AR'
AND prp.x12_code_list_qlfr_lkpcd <> 'HC'
THEN prsi.sv308 end) product_service_id,
(CASE
WHEN prs.rqst_ctgry_lkpcd <> 'AR'
AND prp.x12_code_list_qlfr_lkpcd <> 'HC'
THEN
(CASE
WHEN LENGTH (SUBSTR (prpt.rqst_prcdr_amt,
INSTR (prpt.rqst_prcdr_amt, '.', 1, 1),
1
) = 1
THEN prpt.rqst_prcdr_amt || '0'
ELSE prpt.rqst_prcdr_amt || ''
END
) end) rqst_prcdr_amt,
prp.oral_cavity_dsgntn2_cid oral_cavity_dsgntn2_cid,
prp.oral_cavity_dsgntn3_cid oral_cavity_dsgntn3_cid,
prp.oral_cavity_dsgntn4_cid oral_cavity_dsgntn4_cid,
prp.oral_cavity_dsgntn5_cid oral_cavity_dsgntn5_cid,
prp.prosthesis_crown_inlay_code prosthesis_crown_inlay_code,
(CASE
WHEN prs.rqst_ctgry_lkpcd <> 'AR'
AND prp.x12_code_list_qlfr_lkpcd <> 'HC'
THEN prpt.rqst_prcdr_units end) rqst_prcdr_units, prp.remark remark
FROM (SELECT pa_rqst_sid,
MAX (DECODE (meta_data_cid, 9715, data_value, '')) sv308
FROM pa_request_situational
GROUP BY pa_rqst_sid) prsi,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf,
pa_request pr,
pa_request_service prs,
pa_request_procedure prp,
pa_rqst_prcdr_transaction prpt
WHERE ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ptr.pa_trnsctn_rqst_sid = pr.pa_trnsctn_rqst_sid
AND pr.pa_rqst_sid = prs.pa_rqst_sid
AND prs.pa_rqst_srvc_sid = prp.pa_rqst_srvc_sid
AND prp.pa_rqst_prcdr_sid = prpt.pa_rqst_prcdr_sid
AND pr.pa_rqst_sid = prsi.pa_rqst_sid
AND pr.oprtnl_flag = 'A'
AND prs.oprtnl_flag = 'A'
AND ptr.oprtnl_flag = 'A'
AND prp.oprtnl_flag = 'A'
AND prpt.oprtnl_flag = 'A'
AND ibf.original_file_name =?
Thanks,
P Prakash

A few days ago, you were already suggested to use the WITH statement.
Did you try using the WITH statement?
Sybrand Bakker
Senior Oracle DBA

Similar Messages

  • When the message arrives, the message tone sound that many times. How this problem can be solved? I installed the new version, but the problem is not resolved, the future is to be taken into account in the new version of this?

    When the message arrives, the message tone sound that many times. How this problem can be solved? I installed the new version, but the problem is not resolved, the future is to be taken into account in the new version of this?

    I'm not sure I understand the question, is the message tone going more then once?
    Turn off repeat message alert here:
    settings > notifications > messages > repeat alert > never
    Some people have found this does not stop the repeat miessage tone,
    if you are one of those then I suggest you contact Apple:
    http://www.apple.com/feedback/iphone.html
    They may not respond, but hopefully will fix in uodate if enough people complain.

  • After upgrade to ios7, few time i am having problem with my sound/speaker with terroble sound effect like machine or something.but after restart it will be fine.but how this thing can be fix? Because i never had this problem before upgrade to ios7.tq

    After upgrade to ios7, few time i am having problem with my sound/speaker with terroble sound effect like machine or something.but after restart it will be fine.but how this thing can be fix? Because i never had this problem before upgrade to ios7.tq

    Try going into your settings and 'reset all settings'
    You will need to retweak your device but it's been said to help with sound issues.

  • HT4623 my iPad has over 5GB of memory used for "other." What is this and can I reduce size?

    my iPad has over 5GB of memory used for "other." What is this and can I reduce size?

    gregorfromny wrote:
    my iPad has over 5GB of memory used for "other." What is this and can I reduce size?
    See Here  >  maclife.com/how_remove_other_data_your_iphone
    Other is usually around 1 GB...
    A  ' Large Other ' usually indicates Corrupt Data...
    First Try a Restore from Backup...
    But... if the Large Other Persists, that is an Indicator of Corrupt Data in the Backup...
    Then a Restore as New is the way to go...
    Details Here  >  http://support.apple.com/kb/HT1414
    More Info about ‘Other’ in this Discussion
    https://discussions.apple.com/message/19958116

  • Can you please explain how this query is fetching the rows?

    here is a query to find the top 3 salaries. But the thing is that i am now able to understand how its working to get the correct data :How the data in the alias table P1 and P2 getting compared. Can you please explain in some steps.
    SELECT MIN(P1.SAL) FROM PSAL P1, PSAL P2
    WHERE P1.SAL >= P2.SAL
    GROUP BY P2.SAL
    HAVING COUNT (DISTINCT P1.SAL) <=3 ;
    here is the data i used :
    SQL> select * from psal;
    NAME SAL
    able 1000
    baker 900
    charles 900
    delta 800
    eddy 700
    fred 700
    george 700
    george 700
    Regards,
    Renu

    ... Please help me in understanding the query.
    Your query looks like anything but a Top-N query.
    If you run it in steps and analyze the output at the end of each step, then you should be able to understand what it does.
    Given below is some brief information on the same:
    test@ora>
    test@ora> --
    test@ora> -- Query 1 - using the non-equi (theta) join
    test@ora> --
    test@ora> with psal as (
      2    select 'able' as name, 1000 as sal from dual union all
      3    select 'baker',   900 from dual union all
      4    select 'charles', 900 from dual union all
      5    select 'delta',   800 from dual union all
      6    select 'eddy',    700 from dual union all
      7    select 'fred',    700 from dual union all
      8    select 'george',  700 from dual union all
      9    select 'george',  700 from dual)
    10  --
    11  SELECT p1.sal AS p1_sal, p1.NAME AS p1_name, p2.sal AS p2_sal,
    12         p2.NAME AS p2_name
    13    FROM psal p1, psal p2
    14   WHERE p1.sal >= p2.sal;
        P1_SAL P1_NAME     P2_SAL P2_NAME
          1000 able          1000 able
          1000 able           900 baker
          1000 able           900 charles
          1000 able           800 delta
          1000 able           700 eddy
          1000 able           700 fred
          1000 able           700 george
          1000 able           700 george
           900 baker          900 baker
           900 baker          900 charles
           900 baker          800 delta
           900 baker          700 eddy
           900 baker          700 fred
           900 baker          700 george
           900 baker          700 george
           900 charles        900 baker
           900 charles        900 charles
           900 charles        800 delta
           900 charles        700 eddy
           900 charles        700 fred
           900 charles        700 george
           900 charles        700 george
           800 delta          800 delta
           800 delta          700 eddy
           800 delta          700 fred
           800 delta          700 george
           800 delta          700 george
           700 eddy           700 eddy
           700 eddy           700 fred
           700 eddy           700 george
           700 eddy           700 george
           700 fred           700 eddy
           700 fred           700 fred
           700 fred           700 george
           700 fred           700 george
           700 george         700 eddy
           700 george         700 fred
           700 george         700 george
           700 george         700 george
           700 george         700 eddy
           700 george         700 fred
           700 george         700 george
           700 george         700 george
    43 rows selected.
    test@ora>
    test@ora>This query joins PSAL with itself using a non equi-join. Take each row of PSAL p1 and see how it compares with PSAL p2. You'll see that:
    - Row 1 with sal 1000 is >= to all sal values of p2, so it occurs 8 times
    - Row 2 with sal 900 is >= to 9 sal values of p2, so it occurs 7 times
    - Row 3: 7 times again... and so on.
    - So, total no. of rows are: 8 + 7 + 7 + 5 + 4 + 4 + 4 + 4 = 43
    test@ora>
    test@ora> --
    test@ora> -- Query 2 - add the GROUP BY
    test@ora> --
    test@ora> with psal as (
      2    select 'able' as name, 1000 as sal from dual union all
      3    select 'baker',   900 from dual union all
      4    select 'charles', 900 from dual union all
      5    select 'delta',   800 from dual union all
      6    select 'eddy',    700 from dual union all
      7    select 'fred',    700 from dual union all
      8    select 'george',  700 from dual union all
      9    select 'george',  700 from dual)
    10  --
    11  SELECT p2.sal AS p2_sal,
    12         COUNT(*) as cnt,
    13         COUNT(p1.sal) as cnt_p1_sal,
    14         COUNT(DISTINCT p1.sal) as cnt_dist_p1_sal,
    15         MIN(p1.sal) as min_p1_sal,
    16         MAX(p1.sal) as max_p1_sal
    17    FROM psal p1, psal p2
    18   WHERE p1.sal >= p2.sal
    19  GROUP BY p2.sal;
        P2_SAL        CNT CNT_P1_SAL CNT_DIST_P1_SAL MIN_P1_SAL MAX_P1_SAL
           700         32         32               4        700       1000
           800          4          4               3        800       1000
           900          6          6               2        900       1000
          1000          1          1               1       1000       1000
    test@ora>
    test@ora>Now, if you group by p2.sal in the output of query 1, and check the number of distinct p1.sal, min of p1.sal etc. you see that for p2.sal values - 800, 900 and 1000, there are 3 or less p1.sal values associated.
    So, the last 3 rows are the ones you are interested in, essentially. As follows:
    test@ora>
    test@ora> --
    test@ora> -- Query 3 - GROUP BY and HAVING
    test@ora> --
    test@ora> with psal as (
      2    select 'able' as name, 1000 as sal from dual union all
      3    select 'baker',   900 from dual union all
      4    select 'charles', 900 from dual union all
      5    select 'delta',   800 from dual union all
      6    select 'eddy',    700 from dual union all
      7    select 'fred',    700 from dual union all
      8    select 'george',  700 from dual union all
      9    select 'george',  700 from dual)
    10  --
    11  SELECT p2.sal AS p2_sal,
    12         COUNT(*) as cnt,
    13         COUNT(p1.sal) as cnt_p1_sal,
    14         COUNT(DISTINCT p1.sal) as cnt_dist_p1_sal,
    15         MIN(p1.sal) as min_p1_sal,
    16         MAX(p1.sal) as max_p1_sal
    17    FROM psal p1, psal p2
    18   WHERE p1.sal >= p2.sal
    19  GROUP BY p2.sal
    20  HAVING COUNT(DISTINCT p1.sal) <= 3;
        P2_SAL        CNT CNT_P1_SAL CNT_DIST_P1_SAL MIN_P1_SAL MAX_P1_SAL
           800          4          4               3        800       1000
           900          6          6               2        900       1000
          1000          1          1               1       1000       1000
    test@ora>
    test@ora>
    test@ora>That's what you are doing in that query.
    The thing is - in order to find out Top-N values, you simply need to scan that one table PSAL. So, joining it to itself is not necessary.
    A much simpler query is as follows:
    test@ora>
    test@ora>
    test@ora> --
    test@ora> -- Top-3 salaries - distinct or not; using ROWNUM on ORDER BY
    test@ora> --
    test@ora> with psal as (
      2    select 'able' as name, 1000 as sal from dual union all
      3    select 'baker',   900 from dual union all
      4    select 'charles', 900 from dual union all
      5    select 'delta',   800 from dual union all
      6    select 'eddy',    700 from dual union all
      7    select 'fred',    700 from dual union all
      8    select 'george',  700 from dual union all
      9    select 'george',  700 from dual)
    10  --
    11  SELECT sal
    12  FROM (
    13    SELECT sal
    14      FROM psal
    15    ORDER BY sal DESC
    16  )
    17  WHERE rownum <= 3;
           SAL
          1000
           900
           900
    test@ora>
    test@ora>
    test@ora>And for Top-3 distinct salaries:
    test@ora>
    test@ora> --
    test@ora> -- Top-3 DISTINCT salaries; using ROWNUM on ORDER BY on DISTINCT
    test@ora> --
    test@ora> with psal as (
      2    select 'able' as name, 1000 as sal from dual union all
      3    select 'baker',   900 from dual union all
      4    select 'charles', 900 from dual union all
      5    select 'delta',   800 from dual union all
      6    select 'eddy',    700 from dual union all
      7    select 'fred',    700 from dual union all
      8    select 'george',  700 from dual union all
      9    select 'george',  700 from dual)
    10  --
    11  SELECT sal
    12  FROM (
    13    SELECT DISTINCT sal
    14      FROM psal
    15    ORDER BY sal DESC
    16  )
    17  WHERE rownum <= 3;
           SAL
          1000
           900
           800
    test@ora>
    test@ora>
    test@ora>You may also want to check out the RANK and DENSE_RANK analytic functions.
    RANK:
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions123.htm#SQLRF00690
    DENSE_RANK:
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions043.htm#SQLRF00633
    HTH
    isotope

  • HT201402 My Mac Book Pro shows 103GB storage as "Other". What is this and can I reduce it?

    How can I reduce the storage shown as "other"?

    When Spotlight runs, it categorizes the files it indexes into the categories you're seeing (e.g., movies, photos, apps) and anything that doesn't fit into one of the categories goes into "Other." You can see what might be included in this discussion and what to do about it.

  • How this query executing internally

    Dear All,
    This query picks the nth highest paid employee details, we can acheive this by rank or desnse_rank functions also
    But those execution is easily understandable.But Iam not able to understand , how this below correlated subquery executing internally .Please help me on this query
    select * from emp a where &n=(select count(distinnct b.sal) from emp b where a.sal<=b.sal)
    Thanks in advance

    >
    how this below correlated subquery executing internally .Please help me on this queryPlease check the Explain plan.
    Explain plan
       set pagesize 25
       set linesize 121
       EXPLAIN PLAN FOR
       < Your Query >;
        SELECT * FROM TABLE(dbms_xplan.display);Example :-
    SQL> EXPLAIN PLAN FOR  select count(1) from dual;
    Explained.
    SQL>  SELECT * FROM TABLE(dbms_xplan.display);
    PLAN_TABLE_OUTPUT
    Plan hash value: 323232323
    | Id  | Operation        | Name | Rows  | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT |      |     1 |     2   (0)| 00:00:01 |
    |   1 |  SORT AGGREGATE  |      |     1 |            |          |
    |   2 |   FAST DUAL      |      |     1 |     2   (0)| 00:00:01 |
    -----------------------------------------------------------------

  • Whats wrong with this query.can anyone help me......

    select CASE WHEN TO_NUMBER(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'DDMMYYYY HH24:MM:SS'),13,2))>0 AND TO_NUMBER(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MM:SS'),13,2)) <14
    THEN TO_DATE(CONCAT(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MM:SS'),13,2)||'00',SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MI:SS'),16)||'00'),'DD-MM-YYYY HH24:MI:SS') end
    from table;
    i have written this query.whats wrong with this query..........
    the error is "literal does not match format string"
    Reegards soumen

    Why does your date_format loose, ununify and not fix ?
    And what is your exact requirement?
    >>
    CASE WHEN
    TO_NUMBER(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'DDMMYYYY HH24:MM:SS'),13,2))>0
    AND TO_NUMBER(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MM:SS'),13,2)) <14
    <<
    This is
    CASE WHEN TO_CHAR(START_TIME_TIMESTAMP,'MM') between '01' and '13'
    >>
    THEN TO_DATE(CONCAT(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MM:SS'),13,2)
    ||'00',
    SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MI:SS'),16)||'00'),
    'DD-MM-YYYY HH24:MI:SS')
    <<
    This is
    TO_DATE(
    TO_CHAR(START_TIME_TIMESTAMP,'MM"00"SS"00"),
    'DD-MM-YYYY HH24:MI:SS')
    Obviously, format is not matching !
    SQL> select to_char(sysdate,'MM"00"SS"00') from dual;
    TO_CHAR(
    06004900
    SQL> select to_date('06004900','DD-MM-YYYY HH24:MI:SS') from dual;
    select to_date('06004900','DD-MM-YYYY HH24:MI:SS') from dual
    ERROR at line 1:
    ORA-01861: literal does not match format string

  • HT1498 I rented The Dark Night Resises and I can not see it on my computer Itunes already charged me for the rental ofthis movies. Please either credit my account or let me know how this matter can get resolved.

    I rented the Dark Night Raises yesterday December 22, 2012 It was charge on my Itune card, but I cant not see it on my computer please creidt me the cost of the rental or advise me on How we can resolve this matter.

    We are all itunes users just like you.  We can do nothing.
    What do you mean "I cant not see it on my computer"?
    You cannot fond the movie?  Where did you look?
    Did it download?  Have you tried Store>Check for Available downloads.
    The movie is there but it won't play?

  • I am getting error : "Safari cannot download this file", while downloading a file with "ipa" extention. Please provide the guidelines, how this issue can be addressed.

    Please provide he solution.

    ipa is for an iPhone. You can't open it on Safari on a Mac.
    Read here >  .ipa (file extension) - Wikipedia, the free encyclopedia

  • THIS QUERY CAN'T RUN KINDLY HELP (NOT MODIFY IN TABLE)

    ALTER TABLE DEP80
    MODIFY (JOB_ID NOT NULL VARCHAR2(20));

    Syntax is wrong.
    SQL> create table dep80 (job_id varchar2(20));
    Table created.
    SQL> alter table dep80 modify (job_id not null varchar2(20));
    alter table dep80 modify (job_id not null varchar2(20))
    ERROR at line 1:
    ORA-00907: missing right parenthesis
    SQL> alter table dep80 modify job_id  varchar2(20) not null;
    Table altered.
    SQL>

  • I am new to oracle, plz..anybody can explain this query step by step....plz

    Select distinct(a.esal) from employee1 a where &N = (select count(distinct(b.esal)) from employee1 b where a.esal<=b.esal);
    this is the query to find Nth largest & Nth smallest value from the table employee1....but, i am unable to understand how this query works ....plz..anybody can explain this query step by step with example....plz

    Hi,
    Welcome to the forum!
    The first step in understanding any code is to format it so you can easily see what is a sub-query, what the major clauses of each sub-query are, and things like that.
    For example:
    Select distinct (a.esal)
    from   employee1      a
    where  &N     = (       select  count (distinct (b.esal))
                             from      employee1      b
                    where      a.esal           <= b.esal
                );The only thing I have added to the code you posted was whitespace: newlines, tabs and spaces.
    Now it's easy to see that you're doing a query with a scalar sub-query in the WHERE clause.
    It usually makes sense to read complicated queries from the inside out, that is, start with the deepest nested sub-query.
    In this example, that means:
    select  count (distinct (b.esal))
    from     employee1      b
    where     a.esal           <= b.esalWhat does this query do? Unfortunately, it references something (b.esal) from the super-query, so you can't run it by itself, to see what it does. Let's replace that reference with some hard-coded value, just for now, and run it:
    select  count (distinct (b.esal))
    from     employee1      b
    --where     a.esal           <= b.esal     -- This is the original WHERE clause
    where   a.esal           <= 1000     -- For testing only
    ;Experiment with this for a while, and compare it to the values in the employee1 table (or a small test table that resembles employee1). Try different numbers in place of 1000.
    You'll see that the query is counting how many different salaries are lower than, or equal to, a given salary (1000 in the example above).
    When you understand the sub-query, look at the original query again.
    It is testing to see if exactly &N different esals are lower than or equal to the esal on each row, and displaying that esal if it is. (&N is a substitution variable. If you haven't already defined a value for it, SQL*Plus will ask you for a value when you run the query.)
    In other words, it is finding the N-th lowest value of esal.

  • [8i] Query with lots of subqueries: a simpler way to do this query?

    Ok, so generally, my problem is that I can't figure out how to write this query without including a bunch of copies of the same subquery (similar to having to join multiple copies of a table). Basically, I have to get information that's in rows in one table into columns in the results of my query. I've created sample tables, with insert statements for sample data, and a trimmed-down version of the query I'm running against those (2) tables. I'll explain what I mean by "trimmed-down" where I provide the SQL for that query.
    My restrictions:
    1) I'm running in 8i
    2) I cannot create any tables in the database (I can only do read-only queries and create views).
    Here are my sample tables and sample data:
    CREATE TABLE     reqs     
    (     req_id          NUMBER     NOT NULL,
         part_no          VARCHAR2(5),
         req_date     DATE,
         req_qty          NUMBER,
         CONSTRAINT reqs_pk PRIMARY KEY (req_id)
    INSERT INTO     reqs
    VALUES (1,'part1',To_Date('01/01/2010','mm/dd/yyyy'),5);
    INSERT INTO     reqs
    VALUES (2,'part2',To_Date('01/10/2010','mm/dd/yyyy'),25);
    INSERT INTO     reqs
    VALUES (3,'part1',To_Date('01/20/2010','mm/dd/yyyy'),3);
    INSERT INTO     reqs
    VALUES (4,'part2',To_Date('02/01/2010','mm/dd/yyyy'),15);
    INSERT INTO     reqs
    VALUES (5,'other',To_Date('02/05/2010','mm/dd/yyyy'),1);
    INSERT INTO     reqs
    VALUES (6,'part1',To_Date('02/07/2010','mm/dd/yyyy'),10);
    INSERT INTO     reqs
    VALUES (7,'part3',To_Date('02/25/2010','mm/dd/yyyy'),22);
    INSERT INTO     reqs
    VALUES (8,'part1',To_Date('03/01/2010','mm/dd/yyyy'),24);
    INSERT INTO     reqs
    VALUES (9,'part3',To_Date('04/01/2010','mm/dd/yyyy'),12);
    CREATE TABLE     part
    (     part_no          VARCHAR2(5)     NOT NULL,
         part_desc     VARCHAR2(25),
         qty_instock     NUMBER,
         CONSTRAINT part_pk PRIMARY KEY (part_no)
    INSERT INTO     part
    VALUES ('part1','description 1 here',5);
    INSERT INTO     part
    VALUES ('part2','description 2 here',10);
    INSERT INTO     part
    VALUES ('part3','description 3 here',0);Now, here is the query I'm running against the two tables:
    SELECT     part.part_no
    ,     part.part_desc
    ,     part.qty_instock
    ,     pd.tot_req_qty          AS qty_past_due
    ,     m1.tot_req_qty          AS qty_this_month
    ,     m2.tot_req_qty          AS qty_next_month
    ,     m3.tot_req_qty          AS qty_month_3
    FROM     part
         SELECT     reqs.part_no
         ,     SUM(reqs.req_qty)                    AS tot_req_qty
         ,     CASE
                   WHEN     reqs.req_date     < sysdate
                   THEN     'Past Due'
                   ELSE     To_Char(reqs.req_date,'MON-yyyy')
              END                              AS month_reqd
         FROM     reqs
         GROUP BY     reqs.part_no
         ,          CASE
                        WHEN     reqs.req_date     < sysdate
                        THEN     'Past Due'
                        ELSE     To_Char(reqs.req_date,'MON-yyyy')
                   END
         ) pd --for past due
         SELECT     reqs.part_no
         ,     SUM(reqs.req_qty)                    AS tot_req_qty
         ,     CASE
                   WHEN     reqs.req_date     < sysdate
                   THEN     'Past Due'
                   ELSE     To_Char(reqs.req_date,'MON-yyyy')
              END                              AS month_reqd
         FROM     reqs
         GROUP BY     reqs.part_no
         ,          CASE
                        WHEN     reqs.req_date     < sysdate
                        THEN     'Past Due'
                        ELSE     To_Char(reqs.req_date,'MON-yyyy')
                   END
         ) m1 --for month 1
         SELECT     reqs.part_no
         ,     SUM(reqs.req_qty)                    AS tot_req_qty
         ,     CASE
                   WHEN     reqs.req_date     < sysdate
                   THEN     'Past Due'
                   ELSE     To_Char(reqs.req_date,'MON-yyyy')
              END                              AS month_reqd
         FROM     reqs
         GROUP BY     reqs.part_no
         ,          CASE
                        WHEN     reqs.req_date     < sysdate
                        THEN     'Past Due'
                        ELSE     To_Char(reqs.req_date,'MON-yyyy')
                   END
         ) m2 --for month 2
         SELECT     reqs.part_no
         ,     SUM(reqs.req_qty)                    AS tot_req_qty
         ,     CASE
                   WHEN     reqs.req_date     < sysdate
                   THEN     'Past Due'
                   ELSE     To_Char(reqs.req_date,'MON-yyyy')
              END                              AS month_reqd
         FROM     reqs
         GROUP BY     reqs.part_no
         ,          CASE
                        WHEN     reqs.req_date     < sysdate
                        THEN     'Past Due'
                        ELSE     To_Char(reqs.req_date,'MON-yyyy')
                   END
         ) m3 --for month 3
    WHERE     part.part_no     = pd.part_no
    AND     pd.part_no     = m1.part_no
    AND     m1.part_no     = m2.part_no
    AND     m2.part_no     = m3.part_no
    AND     pd.month_reqd     = 'Past Due'
    AND     m1.month_reqd     = To_Char(Add_Months(sysdate,1),'MON-yyyy')
    AND     m2.month_reqd     = To_Char(Add_Months(sysdate,2),'MON-yyyy')
    AND     m3.month_reqd     = To_Char(Add_Months(sysdate,3),'MON-yyyy')
    ORDER BY     part.part_noThe sample query above only gets 3 months worth of my tot_req_qty for each part number, which equals 3 (+1 for past due) copies of the subquery. This is how this query is "trimmed down" from my actual query; in my real query, I need to include at least 6 months (up to 12 months) of my tot_req_qty for each part number--which means 6-12 (+1) copies of the subquery...ick. I just can't imagine that being an efficient way to get this done.
    Here's a table of the results I expect to get from my sample query:
    PART_NO     PART_DESC          QTY_INSTOCK     QTY_PAST_DUE     QTY_THIS_MONTH     QTY_NEXT_MONTH     QTY_MONTH_3
    part1     description 1 here     5          8          10          24          0     
    part2     description 2 here     10          40          0          0          0
    part3     description 3 here     0          0          22          0          12Is there anything I can do differently here?
    Thanks!!
    Edited by: user11033437 on Feb 4, 2010 11:53 AM Fixed a typo
    Edited by: user11033437 on Feb 4, 2010 12:08 PM Fixed another typo

    Hi,
    Instead of doing self-joins, you can pivot one result set.
    Since it's just one result set, you can use an in-line view for it:
    SELECT     part.part_no
    ,     part.part_desc
    ,     part.qty_instock
    ,     SUM ( CASE WHEN  m.month_reqd = 'Past Due'
                       THEN  m.tot_req_qty
                 ELSE      0
               END
             )          AS qty_past_due
    ,     SUM ( CASE WHEN  m.month_reqd = To_Char(sysdate,'MON-yyyy')
                     THEN  m.tot_req_qty
                 ELSE      0
               END
             )          AS qty_this_month
    ,     SUM ( CASE WHEN  m.month_reqd = To_Char(Add_Months(sysdate, 1),'MON-yyyy')
                     THEN  m.tot_req_qty
                 ELSE      0
               END
             )          AS qty_next_month
    ,     SUM ( CASE WHEN  m.month_reqd = To_Char(Add_Months(sysdate, 2),'MON-yyyy')
                     THEN  m.tot_req_qty
                 ELSE      0
               END
             )          AS qty_month_3
    FROM     part
    ,     (     -- Begin in-line view m
              SELECT  part_no
              ,       SUM (req_qty)          AS tot_req_qty
              ,     month_reqd
              FROM    (     -- Begin in-line view rd
                        SELECT  part_no
                        ,     req_qty
                        ,       CASE
                                  WHEN  req_date   < sysdate
                                            THEN  'Past Due'
                                            ELSE  To_Char(req_date,'MON-yyyy')
                                END          AS month_reqd
                        FROM     reqs
                        WHERE     req_date     < TRUNC ( ADD_MONTHS (SYSDATE, 3)
                                              , 'MONTH'
                   ) rd     -- End in-line view rd
              GROUP BY   part_no
              ,          month_reqd
           ) m     -- End in-line view m
    WHERE       part.part_no     = m.part_no
    GROUP BY  part.part_no
    ,       part.part_desc
    ,       part.qty_instock
    ORDER BY  part.part_no
    ;Notice I used a nested in-line view to compute month_reqd, rather than repeat the CASE expression. That's independent of the pivot: you can do either one with or without the other.
    Thanks for posting the CREATE TABLE and INSERT statements!

  • Need information on this Query

    Hi guys,
    can someone explain how this query works indetail.
    SELECT DISTINCT (a.sal) FROM EMP A WHERE &N = (SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal);
    Advance thanks for your help.
    Kadiyala
    Edited by: user10223931 on Mar 4, 2009 8:17 PM

    Hi,
    SELECT DISTINCT (a.sal),(SELECT COUNT (DISTINCT (b.sal)) FROM EMP B WHERE a.sal<=b.sal) FROM EMP A order by 2 descI hope this will help you.
    The query will rank the records as per salary amount. and you will get the salary amount of the specified rank.

  • Help with re-writing this query

    Dear experts;
    create table t2
      ID varchar2(1000),
      name varchar2(1000),
       place varchar2(1000)
    create table mappingt2
      grades varchar2(1000)
      individual varchar2(1000)
    insert into t2 values ('James', 'John', 'newyork');
    insert into t2 values ('Linda', 'James', 'London');
    insert into t2 values ('Alex', 'kim', 'Mexico');
    insert into t2 values ('karen', 'Jack', 'Tunis');
    insert into t2 values ('CC', 'Jack', 'Peru');
    insert into t2 values ('Linda', 'James', 'Germany');
    insert into mappingt2 values ('A', 'James');
    insert into mappingt2 values ('C', 'Kim');
    insert into mappingt2 values ('C', 'Linda');
    insert into mappingt2 values ('B', 'CC');I have the following query below
    SELECT (select grades from mappingt2 t
       where t.individual =  pt.ID) as current_grade
          ,(select grades from mappingt2 ty
       where ty.individual =  pt.name) alt_grade
          ,pt.place
      FROM t2 ptI was just wondering whether this query can be better written. All help is appreciated. Thank you

    user13328581 wrote:
    Hi Frank, Tom Kyte claims sub scalar queries is usually betterHe didn't say usually he said if you want the first row faster
    >
    if your goal is to optimize your query for initial response time, you may want to use the former query, with the scalar subquery, because Oracle Database would have to get just the first row from DEPT, run the scalar subquery (select count...) against EMP to get the count, and then return it. The process for returning that first row would be very fast.
    >
    The corollary to this that was not explicitly mentioned in the article that I can see is that this probably comes at the expense of the time taken to return all the rows which can then take longer.

Maybe you are looking for

  • Oracle Client 10g/11g on Windows 2008, Itanium

    Hi, All! I need to install Oracle Client on test Windows 2008 Server, based on Itanium processors. 11g client is preferred, and 10g client at least. I know, that Win2008 is not supported yet for Oracle Database, but, may be, there are way to install

  • AD User not provisioning Instantly after reconciliation

    Hi, I have created an access policy for 'ALL USERS' in OIM to assign AD User resource automatically to every OIM user that is created. However, when a new user is created after reconciliation, AD User is not provisioned instantly to the OIM user, but

  • Plant Section in SAP

    Below is SAP definition of Plant section. Define Plant Sections (PS) You define your plant sections in this step. The plant section enables you to subdivide the maintenance plant from the point of view of production responsibility. The person respons

  • Can't hear .wav as I scrub through frames

    I used to create flash cartoons with characters lip synced to sounds. I did it by placing the sound on its own layer and then drawing each frame's mouth position accurate to the sound in each frame. I synced it by scrubbing (moving) through the frame

  • Address book can't get a new Group Folder & 'large type' function doesn't.

    Since loading Tiger a few months ago the Address Book doesn't function as it did. I'd like to reload just the address book software - as that might solve it, but there doesn't seem to be anywhere to download specific piece of apple software online. T