Help with query to return last possible value

Can someone please help me create a query for the situation below?
Table: TABLEA
Columns:
FACID VARCHAR2(10),
DEPTID VARCHAR2(10),
CHARGENUMBER NUMBER(10)
I have the following data:
A, B, 1
A, B, 2
C, D, 3
C, D, 4
Would like to return the following:
A, B, 2
C, D, 4
In other words, I would like to return the last possible CHARGENUMBER for FACID and DEPTID.
The table does not have any indexes and it gets loaded from a spreadsheet in that order.
Thanks so much!

amazongirl wrote:
Can someone please help me create a query for the situation below?
Table: TABLEA
Columns:
FACID VARCHAR2(10),
DEPTID VARCHAR2(10),
CHARGENUMBER NUMBER(10)
I have the following data:
A, B, 1
A, B, 2
C, D, 3
C, D, 4
Would like to return the following:
A, B, 2
C, D, 4
In other words, I would like to return the last possible CHARGENUMBER for FACID and DEPTID.
The table does not have any indexes and it gets loaded from a spreadsheet in that order.
Thanks so much!Hi girl from amazon,
I hope you give me a note because I answered for a lot of questions and nobody aprecieted that. If you don't me a little point I wil leave this forum because isn't so funny to work for nothing.
You are my last chance to stay here!
FACID      DEPTID     CHARGENUMBER
A          B                     1
A          B                     2
C          D                     3
C          D                     4
HR: XE > select x.FACID, x.DEPTID , x.CHARGENUMBER from tableA x,
  2   (select DEPTID, FACID, max(CHARGENUMBER) max_val from tableA
  3    group by  DEPTID, FACID ) y
  4    WHERE x.DEPTID = y.DEPTID and
  5          x.FACID  = y.FACID and
  6          x.CHARGENUMBER =max_val;
FACID      DEPTID     CHARGENUMBER
A          B                     2
C          D                     4Regards,
Ion

Similar Messages

  • Help with query to return results from group within table?

    Hi
    Im a total noob, so please be gentle....!
    I am looking at a table that has a WORD column and a WORD_TYPE_ID column.
    Words can have the same type_id.
    What I am trying to do is write a query that will return the longest word for each word_type_id.
    I have been trying for hours to get this and all I seem to get is either error messages or I return the longest word in the whole WORD column.
    The furthest I can get before things break down is.....
    select word
    from table
    where
    length(word) =
    select
    max(length(word))
    from table
    Any help on this or if I could be pointed in the right direction it would be greatly appreciated.
    Thanks

    Hi,
    Welcome to the forum!
    Here's one way:
    SELECT    word_type_id
    ,       MIN (word) KEEP (DENSE_RANK LAST ORDDER BY LENGTH (word))     AS longest_word
    FROM       table_x
    GROUP BY  word_type_id;If there happens to be a tie within some word_type_id (that is, 2 or more words have exactly the same length, which is the longest in that group) the expression above will return the first one, in alphabetic order. (That's what MIN means here.)
    Edited by: Frank Kulash on Aug 11, 2009 1:56 PM
    You almost had it.
    As you noticed, you were getting the longest row in the whole table. That's because your sub-query was looking at the whole table.
    If you correlate the sub-query to the row in the main table, as shown below, you can get the longest word in each group:
    select  word
    from     table     m                              -- m for main
    where      length (word) = (
                                select  max (length(word))
                   from     table
                   where     word_type_id  = m.word_type_id     -- New
                   );

  • Help with modal dialog returning a value to the calling page

    Greetings,
    Apex Version: 4.1.0.0.32
    What I am trying to do is to create a modal dialog that is called from a form page. The dialog will present the user with an IR report that will allow the him to select a row and return a value from that row to a field on the calling page. I have it working in Firefox, but I get an error using IE 8. I hope someone can show me why it is not working in IE.
    Here is how I am doing it:
    From the calling page:
    Created a button
         Action: Redirect to URL
         URL Target: javascript:var rc = window.showModalDialog('f?p=&APP_ID.:70:&SESSION.::&DEBUG.:::','','resizable:yes;center:yes;dialogWidth:1000px;dialogHeight:500px;');
    On the called page:
    The called page is an IR report where the query returns this as one of the columns:
    *(Note: I had to put a dot '.' in front of the onclick to get it to show in this thread. It is not there in my real code.)*
    select
    <a href="#" name="z" style="color:blue; text-decoration:underline;" .onclick="javascript:passBack(''' || LOT_NO ||''');">Select</a>' SelectThis
    , column1
    , column2
    from sometablename;This resolves the anchor to:
    <a .onclick="javascript:passBack('232158');"  href="#">Select</a>Here is the Javascript function that is called from the anchor onclick:
    function passBack(passVal1)
      opener.document.getElementById("P75_ITEM1").value = passVal1;
      close();
    }When I run this in Firefox, it works as expected. I click on the button on the parent page. The modal dialog is opened and the IR report is displayed. I click on one of the links in the report and it returns the correct value back to the calling page and closes the modal dialog.
    When I run it in IE8, it fails. I click on the button on the parent page. The modal dialog is opened and the IR report is displayed. I click on one of the links in the report and I get this error: “opener.document is null or not an object”.
    I hope that is clear and that someone can help.
    Thanks
    Larry

    A quick google search determines that window.opener doesn't exists when using window.showModalDialog
    Suggestions range from using window.open instead of window.showModalDialog to using dialogArguments instead of window.opener
    Try the following:
    In the parent page define a getPopupValue() function:
    function getPopupValue(){
       var dr =  window.showModalDialog('f?p=&APP_ID.:70:&SESSION.::&DEBUG.:::','','resizable:yes;center:yes;dialogWidth:1000px;dialogHeight:500px;');
        if ( (dr != undefined) && (dr != '') && (dr != false) ){
         $x("P75_ITEM1").value = dr;
    }Change the button url to call the function:
    javascript:getPopupValue(); On the popup page change the passback function to:
    function passBack(passVal1)
      returnValue = passVal1;
      close();
    }

  • 11g: Multicolumn pivot - help with query

    Hi everyone,
    My first attempt at posting here was not very successful. I have now read the rules, and done more homework.
    I'm fairly new to Oracle databases, but have some experience with MySQL and Postgres from earlier.
    First of all, here is an example table of what I have today. This is only an extract of the full table, but these fields are the interesting ones.
    CREATE TABLE trans
         ("FROM_LIC" int, "FROM_LOCATION" varchar2(18), "TO_LOCATION" varchar2(18), "CREATE_DT" timestamp)
    INSERT ALL
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100002563, '215', 'INN_MONO_05', '04-Mar-2013 11:54:21 AM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100002563, 'INN_MONO_05', 'INN_MONO_06_BANE_R', '04-Mar-2013 11:55:08 AM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100002563, 'INN_MONO_06_BANE_R', 'TROLLEY_19', '04-Mar-2013 12:01:06 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100002563, 'TROLLEY_19', 'UT_OPPLAST_5_2', '04-Mar-2013 12:01:56 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100002563, 'UT_OPPLAST_5_2', 'STG010801', '04-Mar-2013 12:01:56 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003259, '231', 'INN_MONO_04', '04-Mar-2013 02:18:31 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003259, 'INN_MONO_04', 'INN_MONO_04_BANE_L', '04-Mar-2013 02:19:28 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003259, 'INN_MONO_04_BANE_L', 'TROLLEY_5', '04-Mar-2013 02:22:41 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003259, 'TROLLEY_5', 'UT_OPPLAST_3_1', '04-Mar-2013 02:23:35 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003262, '243', 'INN_MONO_06', '04-Mar-2013 01:37:49 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003262, 'INN_MONO_06', 'INN_MONO_06_BANE_R', '04-Mar-2013 01:39:09 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003262, 'INN_MONO_06_BANE_R', 'TROLLEY_10', '04-Mar-2013 01:43:48 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003262, 'TROLLEY_10', 'UT_OPPLAST_5_2', '04-Mar-2013 01:44:58 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003262, 'UT_OPPLAST_5_2', 'STG010904', '04-Mar-2013 01:46:30 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003263, '231', 'INN_MONO_04', '04-Mar-2013 02:18:31 PM')
         INTO trans ("FROM_LIC", "FROM_LOCATION", "TO_LOCATION", "CREATE_DT")
               VALUES (4100003263, 'INN_MONO_04', 'INN_MONO_04_BANE_R', '04-Mar-2013 02:19:20 PM')
    SELECT * FROM dual
    ;As of now, I have a query that returns a data grid from the table, looking exactly like the example table I've included.
    Here is a copy of the query that gives the example table as a result from the table in our database:
    select * from (
        select distinct from_lic, from_location, to_location, create_dt from trans where to_location like 'INN_MONO___' and create_dt > sysdate-(3/24)
        union
        select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO___' and to_location like 'INN_MONO%BANE%' and create_dt > sysdate-(3/24)
        union
        select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO%BANE%' and to_location like 'TROLLEY%' and create_dt > sysdate-(3/24)
        union
        select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'TROLLEY%' and to_location like 'UT_OPPLAST____' and create_dt > sysdate-(3/24)
        union
        select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'UT_OPPLAST____%' and (to_location like 'STG%' or to_location like 'UT_OPPLAST%GULV') and create_dt > sysdate-(3/24)
      ) order by from_lic, create_dtI would be delighted if you could help me formulating a new query that gives THIS output, from the same table, with the same constraints as in the original query:
    CREATE TABLE new_table
         ("FROM_LIC" int, "FLOOR" timestamp, "INFEED" timestamp, "TROLLEY" timestamp, "OUTFEED" timestamp, "STAGING" varchar2(16))
    INSERT ALL
         INTO new_table ("FROM_LIC", "FLOOR", "INFEED", "TROLLEY", "OUTFEED", "STAGING")
               VALUES (4100002563, '04-Mar-2013 11:54:00 AM', '04-Mar-2013 11:55:00 AM', '04-Mar-2013 12:01:00 PM', '04-Mar-2013 12:01:00 PM', '03.04.2013 12:01')
         INTO new_table ("FROM_LIC", "FLOOR", "INFEED", "TROLLEY", "OUTFEED", "STAGING")
               VALUES (4100003259, '04-Mar-2013 02:18:00 PM', '04-Mar-2013 02:19:00 PM', '04-Mar-2013 02:22:00 PM', '04-Mar-2013 02:23:00 PM', NULL)
         INTO new_table ("FROM_LIC", "FLOOR", "INFEED", "TROLLEY", "OUTFEED", "STAGING")
               VALUES (4100003262, '04-Mar-2013 01:37:00 PM', '04-Mar-2013 01:39:00 PM', '04-Mar-2013 01:43:00 PM', '04-Mar-2013 01:44:00 PM', '03.04.2013 13:46')
    SELECT * FROM dual
    ;What happened here is that for each instance of from_lic, the line yielded from
    select distinct from_lic, from_location, to_location, create_dt from trans where to_location like 'INN_MONO___' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> in the new column called FLOOR.
    And the following for the rest of the columns:
    select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO___' and to_location like 'INN_MONO%BANE%' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> INFEED.
    select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'INN_MONO%BANE%' and to_location like 'TROLLEY%' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> TROLLEY.
    select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'TROLLEY%' and to_location like 'UT_OPPLAST____' and create_dt > sysdate-(3/24)The value for CREATE_DT ---> OUTFEED.
    select distinct from_lic, from_location, to_location, create_dt from trans where from_location like 'UT_OPPLAST____%' and (to_location like 'STG%' or to_location like 'UT_OPPLAST%GULV') and create_dt > sysdate-(3/24)The value for CREATE_DT ---> STAGING.
    Originally I was thinking of a pivot for this, but there is possibly better ways to accomplish this.
    I would like to have the data in the described format to make data mining easier.
    The query will run on a table with >3M lines.
    I hope this way of formulating the question is better, and more comprehensible.
    Please do not hesitate to ask questions, and I'll do my best to answer.
    I can test any suggested queries in the production database when necessary.
    Edited by: 997749 on Apr 3, 2013 5:18 AM
    Edited by: 997749 on Apr 3, 2013 6:20 AM

    Hi,
    what about something like this:
    with trans as
       SELECT 4100002563 from_lic, 'INN_MONO_05'        to_location, TO_DATE('03.04.2013 11:54:21', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100002563 from_lic, 'INN_MONO_06_BANE_R' to_location, TO_DATE('03.04.2013 11:55:08', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100002563 from_lic, 'TROLLEY_19'         to_location, TO_DATE('03.04.2013 12:01:06', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100002563 from_lic, 'STG010801'          to_location, TO_DATE('03.04.2013 12:01:56', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100002563 from_lic, 'UT_OPPLAST_5_2'     to_location, TO_DATE('03.04.2013 12:01:56', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100005037 from_lic, 'INN_MONO_05'        to_location, TO_DATE('03.04.2013 11:18:31', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100005037 from_lic, 'INN_MONO_04_BANE_R' to_location, TO_DATE('03.04.2013 11:21:54', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100005037 from_lic, 'TROLLEY_16'         to_location, TO_DATE('03.04.2013 11:25:43', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100005037 from_lic, 'UT_OPPLAST_3_4'     to_location, TO_DATE('03.04.2013 11:26:37', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100005037 from_lic, 'STG010703'          to_location, TO_DATE('03.04.2013 11:27:31', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100006658 from_lic, 'INN_MONO_08'        to_location, TO_DATE('03.04.2013 11:00:31', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100006658 from_lic, 'INN_MONO_08_BANE_L' to_location, TO_DATE('03.04.2013 11:02:35', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100006658 from_lic, 'TROLLEY_22'         to_location, TO_DATE('03.04.2013 11:07:54', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL UNION ALL
       SELECT 4100006658 from_lic, 'UT_OPPLAST_7_3'     to_location, TO_DATE('03.04.2013 11:09:03', 'DD.MM.YYYY HH24:MI:SS') create_dt FROM DUAL
    , got_category AS
       SELECT from_lic
            , CASE
                 WHEN to_location like 'INN_MONO%BANE%'
                 THEN 'INFEED'
                 WHEN to_location like 'INN_MONO%'
                 THEN 'FLOOR'
                 WHEN to_location like 'TROLLEY%'
                 THEN 'TROLLEY'
                 WHEN to_location like 'STG' OR to_location like 'UT%GULV' 
                 THEN 'STAGED'
                 WHEN to_location like 'UT_OPPLAST%'
                 THEN 'OUTFEED'
                 ELSE 'UNKNOWN'
              END as cat
            , create_dt
         FROM trans
        WHERE create_dt > (sysdate -10/24) -- adjusted for different timezone
    SELECT * --FROM_LIC, lic_a, lic_b, lic_c, lic_d, lic_e
      FROM got_category
    PIVOT(MAX(create_dt) FOR cat IN('FLOOR'   as floor,
                                     'INFEED'  as infeed,
                                     'TROLLEY' as trolley,
                                     'OUTFEED' as ooutfeed,
                                     'STAGED'  as staged));
      FROM_LIC FLOOR                  INFEED                 TROLLEY                OOUTFEED               STAGED               
    4100002563 03-APR-2013 11:54:21   03-APR-2013 11:55:08   03-APR-2013 12:01:06   03-APR-2013 12:01:56                        
    4100005037 03-APR-2013 11:18:31   03-APR-2013 11:21:54   03-APR-2013 11:25:43   03-APR-2013 11:26:37                        
    4100006658 03-APR-2013 11:00:31   03-APR-2013 11:02:35   03-APR-2013 11:07:54   03-APR-2013 11:09:03                         If not useful, please post your input data (CREATE TABLE and INSERT statements) and your expected output.
    Regards.
    Al

  • Help with query calculations (recursive)

    Hi All,
    I want some help with a query using a base rate and the result use in the next calculation year.
    Here an example:
    create table rate_type(
    rate_type_id    number,
    rate_desc       nvarchar2(50),
    rate_base_year  number
    insert into rate_type(rate_type_id, rate_desc, rate_base_year) values (1, 'Desc1', 4.6590);
    insert into rate_type(rate_type_id, rate_desc, rate_base_year) values (2, 'Desc2', 4.6590);
    create table rates (
    rate_type_id number
    rate_year    number,
    rate_value   number
    insert into rates(rate_type_id, rate_year, rate_value) values (1, 2012, 1.2);
    insert into rates(rate_type_id, rate_year, rate_value) values (1, 2013, 1.3);
    insert into rates(rate_type_id, rate_year, rate_value) values (1, 2014, 1.4);
    insert into rates(rate_type_id, rate_year, rate_value) values (2, 2012, 1.2);
    insert into rates(rate_type_id, rate_year, rate_value) values (2, 2013, 1.3);
    insert into rates(rate_type_id, rate_year, rate_value) values (2, 2014, 1.4);The calculation for the first year should be the base rate of the rate type. The next year should use the result of the previous year and so on.
    The result of my sample data is:
    2012 = 4.659 + 1.2 + 4.659 * (1.2 * 0.01) = 5.9149
    2013 = 5.9149 + 1.3 + 5.9149 * (1.3 * 0.01) = 7.1859
    2014 = 7.1859 + 1.4 + 7.1859 * (1.4 * 0.01) = 8.4721Query result:
    NAME 2012 2013 2014
    Desc1 5.9149 7.1859 8.4721
    Desc2 XXXX XXX XXXX
    How can I do this in one select statement? Any ideas?
    Thanks!

    Assuming you are on 11.2:
    with t as (
               select  a.rate_type_id,
                       rate_desc,
                       rate_year,
                       rate_base_year,
                       rate_value,
                       count(*) over(partition by a.rate_type_id) cnt,
                       row_number() over(partition by a.rate_type_id order by rate_year) rn
                 from  rate_type a,
                       rates b
                 where a.rate_type_id = b.rate_type_id
        r(
          rate_type_id,
          rate_desc,
          rate_year,
          rate_base_year,
          rate_value,
          cnt,
          rn,
          result
         ) as (
                select  rate_type_id,
                        rate_desc,
                        rate_year,
                        rate_base_year,
                        rate_value,
                        cnt,
                        rn,
                        rate_base_year + rate_value + rate_base_year * rate_value * 0.01 result
                  from  t
                  where rn = 1
               union all
                select  t.rate_type_id,
                        t.rate_desc,
                        t.rate_year,
                        t.rate_base_year,
                        t.rate_value,
                        t.cnt,
                        t.rn,
                        r.result + t.rate_value + r.result * t.rate_value * 0.01 result
                  from  r,
                        t
                  where t.rate_type_id = r.rate_type_id
                    and t.rn = r.rn + 1
    select  *
      from  (
             select  rate_desc name,
                     rate_year,
                     result
               from  r
               where rn <= cnt
      pivot (sum(result) for rate_year in (2012,2013,2014))
      order by name
    NAME             2012       2013       2014
    Desc1        5.914908  7.2918018 8.79388703
    Desc2        5.914908  7.2918018 8.79388703
    SQL> Obviously pivoting assumes you know rate_year values upfront. If not, then without pivoting:
    with t as (
               select  a.rate_type_id,
                       rate_desc,
                       rate_year,
                       rate_base_year,
                       rate_value,
                       count(*) over(partition by a.rate_type_id) cnt,
                       row_number() over(partition by a.rate_type_id order by rate_year) rn
                 from  rate_type a,
                       rates b
                 where a.rate_type_id = b.rate_type_id
        r(
          rate_type_id,
          rate_desc,
          rate_year,
          rate_base_year,
          rate_value,
          cnt,
          rn,
          result
         ) as (
                select  rate_type_id,
                        rate_desc,
                        rate_year,
                        rate_base_year,
                        rate_value,
                        cnt,
                        rn,
                        rate_base_year + rate_value + rate_base_year * rate_value * 0.01 result
                  from  t
                  where rn = 1
               union all
                select  t.rate_type_id,
                        t.rate_desc,
                        t.rate_year,
                        t.rate_base_year,
                        t.rate_value,
                        t.cnt,
                        t.rn,
                        r.result + t.rate_value + r.result * t.rate_value * 0.01 result
                  from  r,
                        t
                  where t.rate_type_id = r.rate_type_id
                    and t.rn = r.rn + 1
    select  rate_desc name,
            rate_year,
            result
      from  r
      where rn <= cnt
      order by name,
               rate_year
    NAME        RATE_YEAR     RESULT
    Desc1            2012   5.914908
    Desc1            2013  7.2918018
    Desc1            2014 8.79388703
    Desc2            2012   5.914908
    Desc2            2013  7.2918018
    Desc2            2014 8.79388703
    6 rows selected.
    SQL> SY.

  • Help with query Condition

    --Sample create/insert :
    create table xxdummy(
    id integer,
    name varchar2(40),
    description varchar2(100)
    insert into xxdummy values (1,'L', 'Simple L press');
    insert into xxdummy values (2,'XL', 'XL with no film');
    insert into xxdummy values (2,'web', 'XL with no film');
    insert into xxdummy values (3,'XL', 'XL with Film');
    insert into xxdummy values (3,'Film', 'XL with Film');
    insert into xxdummy values (3,'print', 'XL with Film');
    insert into xxdummy values (4,'S', 'servo with film');
    insert into xxdummy values (4,'Film','servo with film' );
    insert into xxdummy values (5,'S','only servo' );
    insert into xxdummy values (6,'L', 'Simple L press');
    insert into xxdummy values (7,'XL', 'XL with no film');
    insert into xxdummy values (8,'XL', 'XL with Film');
    insert into xxdummy values (8,'Film', 'XL with Film');
    insert into xxdummy values (8,'guide', 'XL with Film');
    --drop table xxdummy;
    select * from xxdummy;
    select id, description, decode(name, 'L', 'L Only', 'XL', 'Got XLs here', 'dont know' ) type
    from xxdummy
    where name in ('L','XL' )
    In above query for column type, I want to display 'L Only', 'XL With Film', 'XL Without Film'
    How can I do this ?
    Thanks

    I should have explained more, but my mind is so fried making up sample replica of the data as its scattered over so many tables in the original query taht I couldnt put it properly:
    another try :
    column Description is just for my reference , So please ignore that for now.
    I have to form a condition only using colums name and id.
    select * from xxdummy :
    id=2 and 7 , has name=XL but doesnt have 'Film' so I want to mark this row as 'XL with no film'
    id=3 and 8 , has name=XL and also has name = Film, So label for this should be - XL With Film
    id=4 , has name=S and also Film, --All the ids that have name=S, no matter if it has film with same Id should be excluded.
    id=5, exclude it
    id=1 and 6 have name = L , Lable them 'L Only' .
    So I should basically have 6 rows returned in my sample data :
    select id, description, name, decode(name, 'L', 'L Only', 'XL', 'Got XLs here' ,'dont know') type
    from xxdummy
    where name in ('L','XL')
    how do I distinguish within id, that has name=XL : which one has film and which one doesnt
    did it sound better ?
    Edited by: OAF-dev on Nov 17, 2009 4:10 PM

  • Need some help with query

    Hey all, I have the following scenario,
    Table A
    Column1
    2
    3
    5
    Table B
    Column2
    7
    5
    4
    Table C
    Column1, Column2
    2,7
    3,8
    5,10
    So basically I need to compare in a way that the combination of the values in A AND B match the recordset in C as such,
    select column1, column2 from Table A, Table B
    where a combination of both those columns exists in Table C. Therefore in the above example, this query should return "2,7" as the output. Any ideas would be appreciated.

    This should do?
    SQL> select c.col1, c.col2
      2  from c
      3  where (c.col1, c.col2) in (select col1, col2
      4                            from (select c.col1 col1
      5                                  from C, a
      6                                  where c.col1 = a.col1) q1
      7                                ,(select c.col2 col2
      8                                  from C, b
      9                                  where c.col2 = b.col1) q2
    10                            )
    11  /
          COL1       COL2
             2          7Cheers
    Sarma.

  • Need help with query joining several tables into a single return line

    what i have:
    tableA:
    puid, task
    id0, task0
    id1, task1
    id2, task2
    tableB:
    puid, seq, state
    id0, 0, foo
    id0, 1, bar
    id0, 2, me
    id1, 0, foo
    id2, 0, foo
    id2, 1, bar
    tableC:
    puid, seq, date
    id0, 0, 12/21
    id0, 1, 12/22
    id0, 2, 12/22
    id1, 0, 12/23
    id2, 0, 12/22
    id2, 1, 12/23
    what i'd like to return:
    id0, task0, 12/21, 12/22, 12/22
    id1, task1, 12/23, N/A, N/A
    id2, task2, 12/22, 12/23, N/A
    N/A doesn't mean return the string "N/A"... it just means there was no value, so we don't need anything in this column (null?)
    i can get output like below through several joins, however i was hoping to condense each "id" into a single line...
    id0, task0, 12/21
    id0, task0, 12/22
    id0, task0, 12/23
    id1, task1, 12/23
    is this possible fairly easily?
    Edited by: user9979830 on Mar 29, 2011 10:53 AM
    Edited by: user9979830 on Mar 29, 2011 10:58 AM

    Hi,
    Welcome to the forum!
    user9979830 wrote:
    what i have:...Thanks for posting that so clearly!
    Whenever you have a question, it's even better if you post CREATE TABLE and INSERT statements for your sample data, like this:
    CREATE TABLE     tablea
    (       puid     VARCHAR2 (5)
    ,     task     VARCHAR2 (5)
    INSERT INTO tablea (puid, task) VALUES ('id0',  'task0');
    INSERT INTO tablea (puid, task) VALUES ('id1',  'task1');
    INSERT INTO tablea (puid, task) VALUES ('id2',  'task2');
    CREATE TABLE     tablec
    (       puid     VARCHAR2 (5)
    ,     seq     NUMBER (3)
    ,     dt     DATE          -- DATE is not a good column name
    INSERT INTO tablec (puid, seq, dt) VALUES ('id0',  0,  DATE '2010-12-21');
    INSERT INTO tablec (puid, seq, dt) VALUES ('id0',  1,  DATE '2010-12-22');
    INSERT INTO tablec (puid, seq, dt) VALUES ('id0',  2,  DATE '2010-12-22');
    INSERT INTO tablec (puid, seq, dt) VALUES ('id1',  0,  DATE '2010-12-23');
    INSERT INTO tablec (puid, seq, dt) VALUES ('id2',  0,  DATE '2010-12-22');
    INSERT INTO tablec (puid, seq, dt) VALUES ('id2',  1,  DATE '2010-12-23');This way, people can re-create the problem and test their ideas.
    It doesn't look like tableb plays any role in this problem, so I didn't post it.
    Explain how you get the results from that data. For example, why do you want this row in the results:
    PUID  TASK  DT1        DT2        DT3
    id0   task0 12/21/2010 12/22/2010 12/22/2010rather than, say
    PUID  TASK  DT1        DT2        DT3
    id0   task0 12/22/2010 12/21/2010 12/22/2010? Does 12/21 have to go in the first column because it is the earliest date, or is it because 12/21 is related to the lowest seq value? Or do you even care about the order, just as long as all 3 dates are shown?
    Always say what version of Oracle you're uisng. The query below will work in Oracle 9 (and up), but starting in Oracle 11, the SELECT ... PIVOT feature could help you.
    i can get output like below through several joins, however i was hoping to condense each "id" into a single line... Condensing the output, so that there's only one line for each puid, sounds like a job for "GROUP BY puid":
    WITH     got_r_num     AS
         SELECT     puid
         ,     dt
         ,     ROW_NUMBER () OVER ( PARTITION BY  puid
                                   ORDER BY          seq          -- and/or dt
                           )         AS r_num
         FROM    tablec
    --     WHERE     ...     -- If you need any filtering, put it here
    SELECT       a.puid
    ,       a.task
    ,       MIN (CASE WHEN r.r_num = 1 THEN r.dt END)     AS dt1
    ,       MIN (CASE WHEN r.r_num = 2 THEN r.dt END)     AS dt2
    ,       MIN (CASE WHEN r.r_num = 3 THEN r.dt END)     AS dt3
    ,       MIN (CASE WHEN r.r_num = 4 THEN r.dt END)     AS dt4
    FROM       tablea    a
    JOIN       got_r_num r  ON   a.puid  = r.puid
    GROUP BY  a.puid
    ,            a.task
    ORDER BY  a.puid
    ;I'm guessing that you want the dates arranged by seq; that is, for each puid, the date related to the lowest seq comes first, regardless of whther that date is the earliest date for that puid or not. If that's not what you need, then change the analytic ORDER BY clause.
    This does not assume that the seq values are always consecutive integers (0, 1, 2, ...) for each puid. You can skip, or even duplicate values. However, if the values are always consecutive integers, starting from 0, then you could simplify this. You won't need a sub-query at all; just use seq instead of r_num in the main query.
    Here's the output I got from the query above:
    PUID  TASK  DT1        DT2        DT3        DT4
    id0   task0 12/21/2010 12/22/2010 12/22/2010
    id1   task1 12/23/2010
    id2   task2 12/22/2010 12/23/2010As posted, the query will display the first 4 dts for each puid.
    If there are fewer than 4 dts for a puid, the query will still work. It will leave some columns NULL at the end.
    If there are more than 4 dts for a puid, the query will still work. It will display the first 4, and ignore the others.
    There's nothing special about the number 4; you could make it 3, or 5, or 35, but whatever number you choose, you have to hard-code that many columns into the query, and always get that many columns of output.
    For various ways to deal with a variable number of pivoted coolumns, see the following thread:
    PL/SQL
    This question actually doesn't have anything to do with SQL*Plus; it's strictly a SQL question, and SQL questions are best posted on the "SQL and PL/SQL" forum:
    PL/SQL
    If you're not sure whether a question is more of a SQL question or a SQL*Plus question, then post it on the SQL forum. Many more people pay attention to that forum than to this one.

  • Help with PL/SQL returning SQL query for Report

    Hi
    I have got a report which I have formatted as PL/SQL function body returning SQL query.
    I have the following code
    declare
    q VARCHAR2(32000); -- query
    w VARCHAR2(4000) ; -- where clause
    begin
    q := 'select min(identified_date) first_identified,' ||
    ' max(actual_resolution_date) last_closed,' ||
    ' count("HELPDESK_CALL_ID) total_issues,' ||
    ' from tbl_helpdesk_calls';
    if :P9_call_type != '-1' then
    w := 'HELPDESK_CALL_TYPE_ID = :P9_call_type';
    end if;
    q := q || ' WHERE '|| w;
    return q;
    end;
    When I apply changes I get this error message
    Query cannot be parsed within the Builder. If you believe your query is syntactically correct, check the ''generic columns'' checkbox below the region source to proceed without parsing. ORA-00972: identifier is too long
    Any Idea where I maybe going wrong?

    thanks its compiling now but when I run the page I get this error message in the reports region
    failed to parse SQL query:
    ORA-00933: SQL command not properly ended
    This is my code that I have at the momment.
    declare
    q VARCHAR2(32000); -- query
    w VARCHAR2(4000) ; -- where clause
    we VARCHAR2(1) := 'N'; -- identifies if where clause exists
    begin
    q := 'select min(identified_date) first_identified,' ||
    ' max(actual_resolution_date) last_closed,' ||
    ' count(HELPDESK_CALL_ID) total_issues,' ||
    ' sum(decode(status,''Open'',1,0)) open_issues, ' ||
    ' sum(decode(status,''On-Hold'',1,0)) onhold_issues,' ||
    ' sum(decode(status,''Closed'',1,0)) closed_issues,' ||
    ' sum(decode(status,''Open'',decode
    (priority,''High'',1,0),0)) open_high_prior,' ||
    ' sum(decode(status,''Open'',decode
    (priority,''Medium'',1,0),0)) open_medium_prior, '||
    ' sum(decode(status,''Open'',decode
    (priority,''Low'',1,0),0)) open_low_prior '||
    ' from tbl_helpdesk_calls';
    if :P9_call_type != '-1' then
    w := 'where HELPDESK_CALL_TYPE_ID = :P9_call_type';
    we := 'Y';
    end if;
    if we = 'Y' then
    q := q ||w;
    end if;
    return q;
    end;
    I have a select list with a list of reports. what I am trying to do build an sql depending on what the :p9_call_type value is. -1 represents the display value of -All Reports- so if the user selects all reports it should display all results thus removing the where clause. However as my code stands when I user clicks on all reports he get the correct results for all reports. If the user selects anything else they get the error message above.

  • Help with dynamic statement returning values into collection

    Hi All
    I am trying to use dynamic statement to return values into a collection using the returning clause. However, I get an ORA-00933 error. Here is a simple setup:
    create table t(
        pk number,
        id_batch varchar2(30),
        date_created date,
        constraint t_pk primary key ( pk )
    create or replace type num_ntt is table of number;
    create or replace type vc2_ntt is table of varchar2(30);
    create or replace
    package pkg
    as
      type rec is record(
          pk        num_ntt,    
          id_batch  vc2_ntt
      procedure p(
          p_count in number,
          p_rt    out nocopy rec
    end pkg;
    create or replace
    package body pkg
    as
      procedure p(
          p_count in number,
          p_rt    out nocopy rec
      is
      begin
          execute immediate '
          insert into t
          select level, ''x'' || level, sysdate
          from   dual
          connect by level <= :p_count
          returning pk, id_batch into :pk, :id_batch'
          using p_count returning bulk collect into p_rt.pk, p_rt.id_batch;
      end p;
    end pkg;
    declare
      r  pkg.rec;
    begin
      pkg.p( 5, r );
    end;
    /

    sanjeevchauhan wrote:
    but I am working with dynamic statement and returning multiple fields into a collection.And using an INSERT...SELECT statement combined with a RETURNING INTO clause still does not work. Whether it's dynamic SQL or not: it doesn't work. The link describes a workaround.
    By the way, I don't see why you are using dynamic SQL here. Static SQL will do just fine. And so you can literally copy Adrian's setup.
    Regards,
    Rob.

  • Help with query to determine if a record value has changed.

    Our auditors want to know which items had their costs updated in March 2006. I'm querying the cst_standard_cost table to find records that were updated in March. This part seems to be working although it pulls back multiple rows for the same item if more than one update was done in that month. I'm still not sure how I'm going to handle that problem.
    ----cst_standard_costs-----
    alter session set NLS_DATE_FORMAT = 'MM/DD/YYYY';
    drop table suss.cost_update_0306b;
    create table suss.cost_update_0306b
    as
    select csc.inventory_item_id, mtl.segment1||','||mtl.segment2 "item", csc.organization_id,csc.standard_cost,csc.creation_date "creation_date"
    from cst_standard_costs csc, mtl_system_items_b mtl
    where csc.inventory_item_id = mtl.inventory_item_id
    and mtl.organization_id = '82'
    and csc.organization_id not in ('0','81')
    and csc.creation_date like '03/%/2006%'
    order by csc.inventory_item_idHere, I'm trying to find if the update that was done in March actually changed the item's cost. But the sql is not exactly right. The results being returned are any cost update where the item's cost is different than it was in March. I'd like to pull back the item's cost from the most recent cost update previous to the March update and see if it differs from the March cost.
    select csc.inventory_item_id,mtl.segment1||','||mtl.segment2, csc.organization_id,csc.standard_cost, csc.creation_date
    from cst_standard_costs csc, mtl_system_items_b mtl, suss.cost_update_0306b scu
    where csc.inventory_item_id = mtl.inventory_item_id
    and mtl.organization_id = '82'
    and scu.inventory_item_id = csc.inventory_item_id
    and csc.organization_id not in ('0','81')
    and csc.creation_date < scu."creation_date"
    and csc.standard_cost <>scu.standard_cost
    order by csc.inventory_item_id ascHere's a simple example
    item id.....cost.........update_date
    24..........45.00........03/01/2006
    24..........45.00........02/01/2006
    24..........30.00........02/22/2006
    40..........45.00........03/01/2006
    40..........30.00........02/01/2006
    40..........28.00........02/22/2006
    The results of the sql should be to return item id 40 because the costs changed in March but not item id 24 because the costs did not change in March

    Something like this:
    SQL> DESC COSTS;
    Name                                      Null?    Type
    ITEM_ID                                            NUMBER
    COST                                               NUMBER(4,2)
    UPDATE_DATE                                        DATE
    SQL> SELECT * FROM COSTS;
       ITEM_ID       COST UPDATE_DAT
            24         40 03/01/2006
            24         45 02/01/2006
            40         45 03/01/2006
            40         30 02/01/2006
    SQL> SELECT b.item_id, b.cost, b.update_date
      2  FROM costs a, costs b
      3  WHERE a.item_id = b.item_id
      4   AND a.cost <> b.cost
      5   AND a.update_date = ADD_MONTHS(b.update_date, -1)
      6  /
       ITEM_ID       COST UPDATE_DAT
            24         40 03/01/2006
            40         45 03/01/2006It is maybe not efficient query but works :)
    Another solution:
      1  SELECT a.item_id, a.cost, a.update_date
      2  FROM costs a
      3  WHERE a.cost <> (SELECT cost
      4               FROM costs
      5               WHERE a.item_id = item_id
      6                AND ADD_MONTHS(a.update_date, -1) = update_date
      7*            )
    SQL> /
       ITEM_ID       COST UPDATE_DAT
            24         40 03/01/2006
            40         45 03/01/2006Peter D.

  • Help with query to exclude invalid values

    creating a one time program. it needs to exclude bogus credit card numbers. all credit card numbers will be in varchar(20) fields.
    1) cannot be null
    2) cannot be empty
    3) cannot be all zeros
    4) must be >= 16 digits and < 19, after removing leading zeros
    5) leading significant digit cannot be 9 or 8
    Having trouble with #3.
    IF OBJECT_ID('tempdb..#tt')IS NOT NULL
    DROP TABLE #tt;
    select * into #tt from
    (select '09876543211234567' union all -- bad card number. not to start with significant 9 or 8
    select '923523425' union all -- bad card number. not to start with significant 9 or 8 and < 16
    select 'ab' union all -- bad
    select null union all -- bad
    select '' union all -- bad
    select '000000000000000000' union all -- bad - numeric value cannot be 0
    select '00543210987654321' union all -- bad < 16
    select '109876543210987654321' union all -- bad > 19
    select '06543210987654321' union all -- good
    select '76543210987654321' -- good
    ) d (cccard)
    select * from #tt
    WHERE cccard IS NOT NULL and ltrim(rtrim(cccard)) <> '' and cccard not like '%[^0-9]%'
    and len(substring(cccard, patindex('%[^0]%',cccard), len(cccard))) > 15
    and len(substring(cccard, patindex('%[^0]%',cccard), len(cccard))) < 20
    and left(substring(cccard, patindex('%[^0]%',cccard), len(cccard)), 1) not in ('9','8')

    Try this
    select * from #tt
    WHERE cccard IS NOT NULL and ltrim(rtrim(cccard)) <> '' and cccard not like '%[^0-9]%'
    and len(substring(cccard, patindex('%[^0]%',cccard), len(cccard))) > 15
    and len(substring(cccard, patindex('%[^0]%',cccard), len(cccard))) < 20
    and left(substring(cccard, patindex('%[^0]%',cccard), len(cccard)), 1) not in ('9','8')
    and PATINDEX('%[^0 ]%',cccard)>0
    --Prashanth

  • Need Help With Query Using Aggregation

    If I have a table, defined like this:
    CREATE TABLE range_test
    range_id NUMBER(20) NOT NULL,
    grade CHAR(1) NOT NULL,
    lower_bound_of_range NUMBER(5,2) NOT NULL,
    upper_bound_of_range NUMBER(5,2) NOT NULL,
    received_date_time_stamp TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL
    And I wanted to query the table to find the range associated with the last inserted row for each 'grade' (e.g. 'A', 'B', 'C', etc), how would I go about that?
    I want something like the following, but I know that this won't work right:
    SELECT
    grade,
    lower_bounding_of_range,
    upper_bounding_of_range,
    max(received_date_time_stamp)
    FROM
    range_test GROUP BY received_date_time_stamp;
    Thanks for your help. . .I'm frustrating myself with this one and I think it should be possible without having to use PL/SQL (i.e. SQL aggregate functions or sub-queries should work).

    Perhaps something along the lines of...
    SQL> ed
    Wrote file afiedt.buf
      1  select deptno, empno, ename, hiredate
      2  from emp
      3* order by deptno, empno
    SQL> /
        DEPTNO      EMPNO ENAME      HIREDATE
            10       7782 CLARK      09-JUN-1981 00:00:00
            10       7839 KING       17-NOV-1981 00:00:00
            10       7934 MILLER     23-JAN-1982 00:00:00
            20       7369 SMITH      17-DEC-1980 00:00:00
            20       7566 JONES      02-APR-1981 00:00:00
            20       7788 SCOTT      19-APR-1987 00:00:00
            20       7876 ADAMS      23-MAY-1987 00:00:00
            20       7902 FORD       03-DEC-1981 00:00:00
            30       7499 ALLEN      20-FEB-1981 00:00:00
            30       7521 WARD       22-FEB-1981 00:00:00
            30       7654 MARTIN     28-SEP-1981 00:00:00
            30       7698 BLAKE      01-MAY-1981 00:00:00
            30       7844 TURNER     08-SEP-1981 00:00:00
            30       7900 JAMES      03-DEC-1981 00:00:00
    14 rows selected.
    SQL> ed
    Wrote file afiedt.buf
      1  select deptno, empno, ename, hiredate
      2  from (
      3        select deptno, empno, ename, hiredate
      4              ,row_number() over (partition by deptno order by hiredate desc) as rn
      5        from emp
      6       )
      7  where rn = 1
      8* order by deptno, empno
    SQL> /
        DEPTNO      EMPNO ENAME      HIREDATE
            10       7934 MILLER     23-JAN-1982 00:00:00
            20       7876 ADAMS      23-MAY-1987 00:00:00
            30       7900 JAMES      03-DEC-1981 00:00:00
    SQL>

  • Query off of Oracle using WinSql - Need help with query

    I am trying to query off of Oracle using program WinSql.
    I have a table(tticpr200110) that has the following sample data:
    ITEM     CODE     T$AMNT
    23500076 ACL     .0049
    23500076 APM     0
    23500076 APO     .0093
    23500076 EXP     .0001
    23500076 RES     .0072
    and what I want it to look like is:
    ITEM     ACL     APM     APO     EXP     RES
    23500076     0.0049     0     0.0093     0.0001     0.0072
    (actually I need the last 2 columns added together to be MATL-but can deal with that down the road).
    Seems simple enough, but I don't know to put into the columns.
    Any help would be GREATLY appreciated as soon as possible would be even better.

    My table - tticpr200110 when it runs I get the following sample data for part number 23500076:
    The first coloumn ITEM is the part number.
    The second column CODE is 1 of 5 different cost codes
    The third column is the cost for that code for that part.
    ITEM CODE AMNT
    23500076 ACL 0.0049
    23500076 APM 0.0000
    23500076 APO 0.0093
    23500076 EXP 0.0001
    23500076 RES 0.0072
    I want to make a query that makes the data look like this:
    ITEM ACL APM APO EXP RES
    23500076 0.0049 0.0000 0.0093 0.0001 0.0072
    (similar to a pivot table in excel or acess)
    I hope this helps better.
    Thanks!

  • [Oracle 8i] Help with query performance

    The following query is running VERY slowly for me:
    SELECT     oord.part_nbr
    ,     sopn.ord_nbr
    ,     sopn.sub_ord_nbr
    ,     sopn.major_seq_nbr
    ,     sopn.wctr_id
    ,     sopn.oper_desc
    ,     SUM(pact.act_dlrs_earned+pact.act_brdn_dls_earned+pact.tool_dlrs_earned+pact.act_fix_brdn_dls_ea)
    ,     pact.activity_date
    FROM     PACT pact
    ,     SOPN sopn
    ,     OORD oord
    WHERE     pact.order_nbr          = sopn.ord_nbr          AND
         pact.maj_seq_nbr     = sopn.major_seq_nbr     AND
         sopn.sub_ord_nbr     = pact.sub_order_nbr     AND
         sopn.ord_nbr          = oord.ord_nbr          AND
         sopn.sub_ord_nbr     = oord.sub_ord_nbr     AND
              pact.activity_date     >= ?          AND
              sopn.rework_ind          = 'N'          AND
              (oord.part_nbr, sopn.major_seq_nbr, sopn.wctr_id)
              NOT IN     (
                        SELECT     rout.doc_nbr
                        ,     rout.major_seq_nbr
                        ,     rout.wctr_id
                        FROM ROUT rout
                        WHERE     (rout.begn_eff_dt    <=SYSDATE)     AND
                             (rout.end_eff_dt    >SYSDATE)     AND
                             (rout.po_rework_ind    ='N')       
    GROUP BY     oord.part_nbr
    ,          sopn.ord_nbr
    ,          sopn.sub_ord_nbr
    ,          sopn.major_seq_nbr
    ,          sopn.wctr_id
    ,          sopn.oper_desc
    ,          pact.activity_dateI sent a request off to my IT department (specifically asking for the explain plan and tkprof, as described in the [main post on this topic|http://forums.oracle.com/forums/thread.jspa?threadID=501834] ), and they replied with a screen shot of the 'explain plan' the tool they use (Toad) provides.
    !http://temp-sample.webs.com/explain_plan.jpg!
    I don't know if anyone can help me based off this, since I know it's not really what the main post says to provide, but it's all I was given.
    My IT department also made a few changes to my original query (see below) and told me it got rid of one of the full scans of the PACT table, but they aren't sure why it helped, and I have to say I'm suspect of any fixes where it's not understood why it helped.
    SELECT     oord.part_nbr
    ,     sopn.ord_nbr
    ,     sopn.sub_ord_nbr
    ,     sopn.major_seq_nbr
    ,     sopn.wctr_id
    ,     sopn.oper_desc
    ,     SUM(pact.act_dlrs_earned+pact.act_brdn_dls_earned+pact.tool_dlrs_earned+pact.act_fix_brdn_dls_ea)
    ,     pact.activity_date
    FROM     PACT pact
    ,     SOPN sopn
    ,     OORD oord
    WHERE     sopn.ord_nbr          = pact.order_nbr     AND
         sopn.major_seq_nbr     = pact.maj_seq_nbr     AND
         pact.sub_order_nbr     = sopn.sub_ord_nbr     AND
         oord.ord_nbr           = sopn.ord_nbr          AND
         oord.sub_ord_nbr     = sopn.sub_ord_nbr     AND
         (pact.activity_date >= ?)               AND
         'N'               = sopn.rework_ind     AND
         pact.order_nbr          = oord.ord_nbr          AND
         oord.sub_ord_nbr = pact.sub_order_nbr          AND
         (oord.part_nbr, pact.maj_seq_nbr, sopn.wctr_id) NOT IN
              SELECT /*+ INDEX_JOIN(ROUT) */     rout.doc_nbr
              ,                    rout.major_seq_nbr
              ,                    rout.wctr_id
              FROM     ROUT rout
              WHERE     rout.begn_eff_dt     <= SYSDATE     AND
                   rout.end_eff_dt      > SYSDATE     AND
                   'N' = rout.po_rework_ind
    GROUP BY     oord.part_nbr
    ,          sopn.ord_nbr
    ,          sopn.sub_ord_nbr
    ,          sopn.major_seq_nbr
    ,          sopn.wctr_id
    ,          sopn.oper_desc
    ,          pact.activity_dateAny help on this would be appreciated... when I run this (right now) for 2-3 months of data, it takes a minimum of 3 hours to complete, and I'll eventually need to run this for up to a year's worth of data.

    Hi,
    Well, let's see.
    You get 156 rows returned using IN and 121 rows using exists.
    You need identify the 'missing records' and conclude if that's correct or not, I'm not able to do that from remote, without knowing your data or system.
    It would be helpful if we could see cost and cardinalities, you (or your IT dept.) can get them easily be running the queries from your SQL*Plus prompt.
    Type
    SET AUTOTRACE TRACEONLYbefore running the queries.
    That gives you the explain plan and additional statistics (sorts in memory and sorts to disk).
    Since you use a group by, and you're on 8i can you also post results of these queries:
    select banner from v$version;
    select name, value, isdefault from v$parameter where name like '%area%';Finally, does below query give you a different plan?
    select oord.part_nbr
    ,      oord.ord_nbr
    ,      oord.sub_ord_nbr
    ,      pact.major_seq_nbr
    ,      sopn.wctr_id
    ,      sopn.oper_desc
    ,      sum(pact.act_dlrs_earned + pact.act_brdn_dls_earned + pact.tool_dlrs_earned + pact.act_fix_brdn_dls_ea)
    ,      pact.activity_date
    from   oord oord 
    ,      pact pact
    ,      sopn sopn
    where  oord.ord_nbr       = pact.order_nbr
    and    oord.sub_ord_nbr   = pact.sub_order_nbr
    and    oord.ord_nbr       = sopn.ord_nbr
    and    oord.sub_ord_nbr   = sopn.sub_ord_nbr
    and    sopn.major_seq_nbr = pact.maj_seq_nbr
    and    (pact.activity_date >= ?)
    and    'N' = sopn.rework_ind
    and    (oord.part_nbr, pact.maj_seq_nbr, sopn.wctr_id) not in ( select rout.doc_nbr
                                                                    ,      rout.major_seq_nbr
                                                                    ,      rout.wctr_id
                                                                    from   rout rout
                                                                    where  rout.begn_eff_dt <= sysdate
                                                                    and    rout.end_eff_dt > sysdate
                                                                    and    'N' = rout.po_rework_ind)
    group  by oord.part_nbr
    ,         oord.ord_nbr
    ,         oord.sub_ord_nbr
    ,         pact.major_seq_nbr
    ,         sopn.wctr_id
    ,         sopn.oper_desc
    ,         pact.activity_date

Maybe you are looking for

  • ITunes not opening because of "wrong" version of quicktime

    It says that I do not have the version 7.1.3 of Quicktime (when I do), and will not open. I've tried reinstalling everything, but it won't work.

  • Extracting photos from a slide show

    Sorry if the Subject is misleading. Here is my situation. I have a large iphoto library that has MANY duplicate pictures AND my wife painstakingly and loving created a very large slideshow with transitions, music etc. I would like to remove the dupli

  • My ipod touch wont light up..help anyone?

    my ipod touch works fully except the screen only lights up for about half a second..help anyone?

  • TIPS(29) : TABLE에 걸려 있는 INDEX 찾아 보기

    제품 : SQL*PLUS 작성날짜 : 1996-12-27 TIPS(29) : TABLE에 걸려 있는 INDEX 찾아 보기 =========================================== /* show_index.sql USAGE: Show the indexes on a table */ /* This script prompts the user for the table owner and name then gets */ /* the i

  • Check if VI is a control (.ctl)

    Given an array of VI references, is there a method to determine which VIs are actually controls (.ctl files)?  (Other than getting the path and checking that the file extension is .ctl.  I was hoping for a more direct way.  ) Thanks, Joel Solved! Go