Query Help (Kind of Analytical query)

Hi,
I have a requirement in which i want to get the following output. I have taken DEPT & EMP as the example tables.
DEPT EMPLOYEES
10 1,2,3,4,5,6
20 7,8,9,10
30 11,12,13,14
Your help will be highly appreciated.
Regards,
Sunil.

Hi Gabe,
Thanks for the quick reply. I checked the asktom site but here he has used PLSQL block to get the output.
Is it possible to get this output using a single SQL statement.
Thanks for the help.
Regards,
Sunil.

Similar Messages

  • Query help on Goods Receipt Query with AP Invoice

    Looking for a little help on a query.  I would like to list all the goods receipts for a given date range and then display the AP Invoice information (if its been copied to an AP Invoice).  I think my problem is in my where clause, I plagerized an SAP query to show GR and AP from a PO as a start.  SBO 2005 SP01.  Any help would be great appreciated.  Thanks
    SELECT distinct 'GR',
    D0.DocStatus,
    D0.DocNum ,
    D0.DocDate,
    D0.DocDueDate,
    D0.DocTotal,
    'AP',
    I0.DocStatus,
    I0.DocNum ,
    I0.DocDate,
    I0.DocDueDate,
    I0.DocTotal,
    I0.PaidToDate
    FROM
    ((OPDN  D0 inner Join PDN1 D1 on D0.DocEntry = D1.DocEntry)
    full outer join
    (OPCH I0 inner join PCH1 I1 on I0.DocEntry = I1.DocEntry)
    on (I1.BaseType=20 AND D1.DocEntry = I1.BaseEntry AND D1.LineNum=I1.BaseLine))
    WHERE
    (D1.BaseType=22 AND D1.DocDate>='[%0]' AND D1.DocDate<='[%1]')
    OR (I1.BaseType=20 AND I1.BaseEntry IN
    (SELECT Distinct DocEntry
    FROM PDN1 WHERE BaseType=22 AND DocDate>='[%0]' AND DocDate<='[%1]'))

    Hi Dalen ,
    I  believe it is because of the condition
    (D1.BaseType=22 AND D1.DocDate>='%0' AND D1.DocDate<='%1')
    OR (I1.BaseType=20 AND I1.BaseEntry IN
    (SELECT Distinct DocEntry FROM PDN1 WHERE PDN1.BaseType=22 AND DocDate>='%0' AND DocDate<='%1'))
    Try changing
    D1.BaseType=22 OR D1.DocDate>='%0' AND D1.DocDate<='%1
    PDN1.BaseType=22 OR DocDate>='%0' AND DocDate<='%1'))
    Lets see what would be the result . Lets have some fun with troubleshooting
    See what would be the difference in the result .
    Thank you
    Bishal

  • Query help in regular expression Query

    Hi all,
    Version details
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
    PL/SQL Release 11.1.0.7.0 - Production
    CORE     11.1.0.7.0     Production
    TNS for 32-bit Windows: Version 11.1.0.7.0 - Production
    NLSRTL Version 11.1.0.7.0 - ProductionI have table RULE with one column as CLOB data type and my requirement is as follows
    Sample Data :
    0-7IfFlowControl0.-7dd670afb-2d41-440f-958d-c19f0f75e91dareErrorsPostedAtHeader(00760)ClaimErrorCollection0.-7-1000FlowControl0-8ThenFlowControl0.-8a0c1c903-0c04-4d68-b5a8-10cbd4a03e9faddRequiredElementErrors(00760 & D)
    Expected Output .
    00760
    00760
    D
    I want the values within braces() and when ever there is an & symbol inside braces then i want it in the next row.
    Please see the above example .Any help in this regard is would be highly appreciated ...........
    Thanks,
    P Prakash
    Edited by: prakash on Nov 10, 2011 9:19 PM

    with t
    as
    select '0-7IfFlowControl0.-7dd670afb-2d41-440f-958d-c19f0f75e91dareErrorsPostedAtHeader(00760)ClaimErrorCollection0.-7-1000FlowControl0-8ThenFlowControl0.-8a0c1c903-0c04-4d68-b5a8-10cbd4a03e9faddRequiredElementErrors(00760 & D)' str
      from dual
    ), t1
    as
    select replace(replace(regexp_substr(str, '\([^)]*\)', 1, level),')'), '(') str
      from t
    connect by level <= regexp_count(str, '\([^)]*\)')
    select regexp_substr(str, '[^&]+', 1, t2.l)
      from t1
    cross join
             select level l
               from (
                      select max(length(regexp_replace(str, '[^&]')))+1 cnt
                        from t1
            connect by level <= cnt
           ) t2
    where regexp_substr(str, '[^&]+', 1, t2.l) is not null

  • Sql query help,i need a query to get like this without union clause

    empno ename sum(sal) sum(deptno)
    200 tom 2000 10
    201 smith 3000 10
    202 alfred 3000 20
    total 8000 40

    Hi,
    group by rollup (empno,ename) add one row in the end of each same group empno,ename : empno ename sum(sal) sum(deptno) and one in the end ( total)
    but group by rollup ((empno,ename)) add only one row in the end total ..... SQL> select nvl(to_char(empno),'Total'), ename,sum(sal),sum(deptno)
      2  from emp
      3  group by rollup (empno,ename)
      4  /
    NVL(TO_CHAR(EMPNO),'TOTAL')              ENAME        SUM(SAL) SUM(DEPTNO)
    7900                                     JAMES             950          30
    7900                                                       950          30
    7369                                     SMITH             800          20
    7369                                                       800          20
    7499                                     ALLEN            1600          30
    7499                                                      1600          30
    7521                                     WARD             1250          30
    7521                                                      1250          30
    7566                                     JONES            2975          20
    7566                                                      2975          20
    7654                                     MARTIN           1250          30
    7654                                                      1250          30
    7698                                     BLAKE            2850          30
    7698                                                      2850          30
    7782                                     CLARK            2450          10
    7782                                                      2450          10
    7788                                     SCOTT            3000          20
    7788                                                      3000          20
    7839                                     KING             5000          10
    7839                                                      5000          10
    7844                                     TURNER           1500          30
    7844                                                      1500          30
    7876                                     ADAMS            1100          20
    7876                                                      1100          20
    7902                                     FORD             3000          20
    7902                                                      3000          20
    7934                                     MILLER           1300          10
    7934                                                      1300          10
    Total                                                    29025         310
    29 rows selected.
    SQL>  select nvl(to_char(empno),'Total'), ename,sum(sal),sum(deptno)
      2   from emp
      3   group by rollup ((empno,ename));
    NVL(TO_CHAR(EMPNO),'TOTAL')              ENAME        SUM(SAL) SUM(DEPTNO)
    7900                                     JAMES             950          30
    7369                                     SMITH             800          20
    7499                                     ALLEN            1600          30
    7521                                     WARD             1250          30
    7566                                     JONES            2975          20
    7654                                     MARTIN           1250          30
    7698                                     BLAKE            2850          30
    7782                                     CLARK            2450          10
    7788                                     SCOTT            3000          20
    7839                                     KING             5000          10
    7844                                     TURNER           1500          30
    7876                                     ADAMS            1100          20
    7902                                     FORD             3000          20
    7934                                     MILLER           1300          10
    Total                                                    29025         310
    15 rows selected.
    SQL> Edited by: Salim Chelabi on May 9, 2009 5:12 PM
    Edited by: Salim Chelabi on May 9, 2009 5:28 PM

  • Query Help Please

    Hi... having problems with a query.  Any assistance would be
    much appreciated.
    Two queries with identical columns: Villages_Query_1 and Villages_Query_2.
    Both have these columns: Village_ID, Village_Name, Player_ID.
    I need to find all records in Villages_Query_2 where the Village_ID's match but the Player_ID's have changed.
    Example Village_Query_1
    Village_ID
    Village_Name
    Player_ID
    1
    Houston
    1
    2
    Dallas
    2
    3
    Chicago
    3
    Example Village_Query_2
    Village_ID
    Village_Name
    Player_ID
    1
    Houston
    1
    2
    Phoenix
    4
    3
    Chicago
    3
    4
    New York
    5
    In this case, Village_ID = 2, has changed names (Dallas to Phoenix) and the Player_ID has changed (2 to 4).  In addition, a new record was added.
    The eventual output I need is to be able to report the following:
    Player 2 village "Dallas" was taken by Player 4 and renamed "Phoenix".
    New York is a new village owned by Player 5.
    How the heck do I do this??  I have been trying query after query... reading about query of queries and JOINS and and and... I am now completely confused.
    Help appreciated.
    Mark

    Well... firstly... you do not use MS Access for that volume of data.  Plain and simple.  MS Access is for DBs like "My CD collection".  It's a desktop application, and is not intended to be used other than as a desktop application.
    Part of the reason for it not being appropriate for the job is that it can't do things like bulk loading data, which is kinda what you're wanting to do here.  That aside, it's a single-user file-based DB which is simply not designed to work as the back-end for a web application (or any sort of serious application).
    Anyway, I would approach this by putting all the data from the CSV files into the DB as is.  Then on the DB run a query which gets all your changes.  You're really going to struggle with the suggestions here to use valueList() to generate a list that is then used for a NOT IN(#list here#), because you're likely to have a mighty long list there.  Even proper DBs like Oracle only allow 2000 entries in a list like that (SQL Server is about the same, from memory), so I doubt QoQ will allow even that.  The reason the DBs put limits on these things is that doing a WHERE IN (#list#) is a really poorly-performing process.
    If you've got all your data in the DB, then your query becomes pretty easy, and I'm sure even Access could cope with it.  it'd be something like this:
    SELECT VB.village_id, VB.village_name AS village_old_name, VB.player_id AS player_old_id,
    VU.village_id AS village_new_id, VU.village_name AS village_new_name, VU.player_id as player_new_id
    FROM villages_base VB
    RIGHT OUTER JOIN villages_updates VU
    ON VB.village_id = VU.village_id
    WHERE VB.village_name != VU.village_name
    (that's untested and I only gave it about 1min thought before typing it in, so don't quote me on that!)
    Where VILLAGE_BASE is your original data, and VILLAGE_UPDATES is the data that indicates the changes.  I'm kinda guessing that this is the sort of thing you want.  Note: the "new" villages will be the ones which have NULLs for the village_id, village_old_name and player_old_id.
    Getting all the data into the DB is going to be a matter of looping over the CSV file and doing an INSERT for each row.  And that will take as long as it takes, so you might need to get some control over your request timeouts.  However doing these inserts will take less time than all the QoQ logic suggested before, so you might be OK.  And the query should be quick.
    What happens to the data once the report is written?  Does the "updated" data become the "live" data?  If so, after you run your report you're gonna want to do something like a TRUNCATE on villages_base, and INSERT all the records from villages_update into it (then TRUNCATE villages_update, ready for the next time you need to run this process).  Although don't take my word for it here, as I'm guessing your requirement here ;-)
    Adam

  • Oracle query help

    Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.8.0 - Production
    Report Builder 10.1.2.0.2
    ORACLE Server Release 10.1.0.4.2
    Oracle Procedure Builder 10.1.2.0.2
    Oracle ORACLE PL/SQL V10.1.0.4.2 - Production
    Oracle CORE    10.1.0.4.0    Production
    Oracle Tools Integration Services 10.1.2.0.2
    Oracle Tools Common Area 10.1.2.0.2
    Oracle Toolkit 2 for Windows 32-bit platforms 10.1.2.0.2
    Resource Object Store 10.1.2.0.2
    Oracle Help 10.1.2.0.2
    Oracle Sqlmgr 10.1.2.0.2
    Oracle Query Builder 10.1.2.0.2 - Production
    PL/SQL Editor (c) WinMain Software (www.winmain.com), v1.0 (Production)
    Oracle ZRC 10.1.2.0.2
    Oracle XML Developers Kit 10.1.0.4.2 - Production
    Oracle Virtual Graphics System 10.1.2.0.2
    Oracle Image 10.1.2.0.2
    Oracle Multimedia Widget 10.1.2.0.2
    Oracle Tools GUI Utilities 10.1.2.0.2
    I have enclosed sample data and also table structure. I need help in getting the query.
    select dept_id,proc_code,override_goal,goal_override_date
      from table2
    where goal_override_date between '02-jan-2014' and '11-jan-2014'
       and dept_id = 10
       and proc_code = 'CP'
    select DEPT_ID, PROC_CODE, DAY_SUNDAY, DAY_MONDAY,
           DAY_TUESDAY, DAY_WEDNESDAY, DAY_THURSDAY,
             DAY_FRIDAY, DAY_SATURDAY
      from table1
    where dept_id =10
    and proc_code = 'CP'; 
    Table1  is kind of maintenance table.
    In Table2 values can be overridden.
    Requirement
    Check to see if there is data in table 2 for the date range . If table2 has no value then take value from table1 for that day the date falls into. Any more clarification please ask me.
    Sundays are all zeros.
    I want this data. and the sum for the date range.
    2-jan-2014  - 3
    3-jan-2014  - 3
    4-jan-2014  - 3
    5-jan-2014  - 0
    6-jan-2014  - 1
    7-jan-2014  - 3
    8-jan-2014  - 5
    9-jan-2014  - 5
    10-jan-2014 - 3
    11-jan-2014 - 3
    Sum for the date range has to be 29
    Sample table and data
    CREATE TABLE TABLE1
      DEPT_ID NUMBER NOT NULL,
      PROC_CODE VARCHAR2(2 BYTE) NOT NULL,
      DAY_SUNDAY NUMBER(4) NOT NULL,
      DAY_MONDAY NUMBER(4) NOT NULL,
      DAY_TUESDAY NUMBER(4) NOT NULL,
      DAY_WEDNESDAY NUMBER(4) NOT NULL,
      DAY_THURSDAY NUMBER(4) NOT NULL,
      DAY_FRIDAY NUMBER(4) NOT NULL,
      DAY_SATURDAY NUMBER(4) NOT NULL
    Insert into TABLE1
      (DEPT_ID, PROC_CODE, DAY_SUNDAY, DAY_MONDAY, DAY_TUESDAY,
      DAY_WEDNESDAY, DAY_THURSDAY, DAY_FRIDAY, DAY_SATURDAY)
    Values
      (10, 'CP', 0, 3, 3,
      3, 3, 3, 3);
    COMMIT;
    CREATE TABLE TABLE2
      DEPT_ID NUMBER NOT NULL,
      PROC_CODE VARCHAR2(2 BYTE) NOT NULL,
      OVERRIDE_GOAL NUMBER(4),
      GOAL_OVERRIDE_DATE DATE NOT NULL
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 1, TO_DATE('01/06/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 3, TO_DATE('01/07/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 5, TO_DATE('01/08/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 5, TO_DATE('01/09/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 3, TO_DATE('01/10/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    Insert into TABLE2
      (DEPT_ID, PROC_CODE, OVERRIDE_GOAL, GOAL_OVERRIDE_DATE)
    Values
      (10, 'CP', 3, TO_DATE('01/11/2014 00:00:00', 'MM/DD/YYYY HH24:MI:SS'));
    COMMIT;
    Help is highly appreciated.

    SELECT dates,
           override_goal,
           SUM(override_goal) OVER()
           FROM(SELECT dates,
                       CASE WHEN EXISTS(SELECT 1
                                        FROM table2 t2
                                        WHERE t2.goal_override_date = qry1.dates
                                        AND t2.dept_id = 10
                                        AND t2.proc_code = 'CP')
                            THEN (SELECT override_goal
                                  FROM table2 t2
                                  WHERE t2.goal_override_date = qry1.dates
                                  AND t2.dept_id = 10
                                  AND t2.proc_code = 'CP')
                       ELSE (SELECT DECODE(days,'SUN',day_sunday,
                                                'MON',day_monday,
                                                'TUE',day_tuesday,
                                                'WED',day_wednesday,
                                                'THU',day_thursday,
                                                'FRI',day_friday,
                                                'SAT',day_saturday )
                              FROM table1)
                       END as override_goal
                FROM (SELECT in_dt1+(LEVEL-1) dates,
                             TO_CHAR(in_dt1+(LEVEL-1),'DY') days
                      FROM(SELECT TO_DATE(&from_date,'DD-MON-YYYY') in_dt1,
                                  TO_DATE(&to_date,'DD-MON-YYYY') in_dt2
                                 FROM dual)
                      CONNECT BY LEVEL <= (in_dt2 - in_dt1)+1) qry1);
    Now run the query it will prompt you for the inputs. You can pass the date values and check the result. Otherwise replace &from_date,&to_date with user inputs

  • Please help me with this query -- i am trying with Dense rank

    version 10g
    i received a quote for an account. if the same quote is received under different account then i should mark the previous account received as deleted.
    please help me .
    /* Formatted on 2010/06/28 14:13 (Formatter Plus v4.8.8) */
    WITH temp AS
         (SELECT '1-11TWQL' quote_id, 'COPS' ACCOUNT, 'Ordered' status,
                 TO_DATE ('12/23/2009 3:37:54',
                          'mm/dd/yyyy hh:mi:ss PM'
                         ) captured_date
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'COPS', 'RFS',
                 TO_DATE ('12/23/2009 3:37:50', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'COPS', 'Rejected',
                 TO_DATE ('12/23/2009 3:37:52', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'COPS', 'Validated',
                 TO_DATE ('12/23/2009 3:37:51', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'D1', 'Ordered',
                 TO_DATE ('12/23/2009 3:04:24', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'D1', 'RFS',
                 TO_DATE ('12/23/2009 3:04:23', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'D1', 'Rejected',
                 TO_DATE ('12/23/2009 3:04:22', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'D1', 'Validated',
                 TO_DATE ('12/23/2009 3:04:23', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'RFS',
                 TO_DATE ('3/5/2010 12:04:24', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'RFS',
                 TO_DATE ('3/16/2010 7:55:50', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'RFS',
                 TO_DATE ('3/16/2010 7:55:51', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Rejected',
                 TO_DATE ('3/5/2010 12:04:24', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Rejected',
                 TO_DATE ('3/16/2010 7:55:50', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Rejected',
                 TO_DATE ('3/16/2010 7:55:51', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Validated',
                 TO_DATE ('12/23/2009 3:37:54', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Validated',
                 TO_DATE ('12/23/2009 3:37:54', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Validated',
                 TO_DATE ('12/23/2009 3:37:54', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'Ordered',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'Ordered',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'RFS',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'RFS',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'Validated',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'Validated',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8Z', 'COPS', 'Validated',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL
          UNION ALL
          SELECT '1-249A8Z', 'COPS', 'Ordered',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
            FROM DUAL)
    SELECT   quote_id, ACCOUNT, status, captured_date,
             DENSE_RANK () OVER (PARTITION BY quote_id ORDER BY quote_id,
              ACCOUNT) rn
    --         ,CASE DENSE_RANK () OVER (PARTITION BY quote_id ORDER BY quote_id,
    --              ACCOUNT)
    --            WHEN 1
    --               THEN 'Y'
    --            ELSE 'N'
    --         END deleted_flag
        FROM temp
    ORDER BY quote_id, captured_date;output required
    WITH temp AS
         (SELECT '1-11TWQL' quote_id, 'COPS' ACCOUNT, 'Ordered' status,
                 TO_DATE ('12/23/2009 3:37:54',
                          'mm/dd/yyyy hh:mi:ss PM'
                         ) captured_date, 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'COPS', 'RFS',
                 TO_DATE ('12/23/2009 3:37:50', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'COPS', 'Rejected',
                 TO_DATE ('12/23/2009 3:37:52', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'COPS', 'Validated',
                 TO_DATE ('12/23/2009 3:37:51', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'D1', 'Ordered',
                 TO_DATE ('12/23/2009 3:04:24', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'D1', 'RFS',
                 TO_DATE ('12/23/2009 3:04:23', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'D1', 'Rejected',
                 TO_DATE ('12/23/2009 3:04:22', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-11TWQL', 'D1', 'Validated',
                 TO_DATE ('12/23/2009 3:04:23', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'RFS',
                 TO_DATE ('3/5/2010 12:04:24', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'RFS',
                 TO_DATE ('3/16/2010 7:55:50', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'RFS',
                 TO_DATE ('3/16/2010 7:55:51', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Rejected',
                 TO_DATE ('3/5/2010 12:04:24', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Rejected',
                 TO_DATE ('3/16/2010 7:55:50', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Rejected',
                 TO_DATE ('3/16/2010 7:55:51', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Validated',
                 TO_DATE ('12/23/2009 3:37:54', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Validated',
                 TO_DATE ('12/23/2009 3:37:54', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'COPS', 'Validated',
                 TO_DATE ('12/23/2009 3:37:54', 'mm/dd/yyyy hh:mi:ss PM'), 'Y' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'Ordered',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'Ordered',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'RFS',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'RFS',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'Validated',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8X', 'D1', 'Validated',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8Z', 'COPS', 'Validated',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL
          UNION ALL
          SELECT '1-249A8Z', 'COPS', 'Ordered',
                 TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM'), 'N' deleted_flag
            FROM DUAL)
    SELECT   quote_id, ACCOUNT, status, captured_date, deleted_flag
        FROM temp
    ORDER BY quote_id, captured_date;

    try to wrap your query to become an in-line view. use a case statement or decode to your derived column RN from the analytic query of DENSE RANK. either way they both will work.
    SQL> WITH temp AS
      2       (SELECT '1-11TWQL' quote_id, 'COPS' ACCOUNT, 'Ordered' status,
      3               TO_DATE ('12/23/2009 3:37:54',
      4                        'mm/dd/yyyy hh:mi:ss PM'
      5                       ) captured_date
      6          FROM DUAL
      7        UNION ALL
      8        SELECT '1-11TWQL', 'COPS', 'RFS',
      9               TO_DATE ('12/23/2009 3:37:50', 'mm/dd/yyyy hh:mi:ss PM')
    10          FROM DUAL
    11        UNION ALL
    12        SELECT '1-11TWQL', 'COPS', 'Rejected',
    13               TO_DATE ('12/23/2009 3:37:52', 'mm/dd/yyyy hh:mi:ss PM')
    14          FROM DUAL
    15        UNION ALL
    16        SELECT '1-11TWQL', 'COPS', 'Validated',
    17               TO_DATE ('12/23/2009 3:37:51', 'mm/dd/yyyy hh:mi:ss PM')
    18          FROM DUAL
    19        UNION ALL
    20        SELECT '1-11TWQL', 'D1', 'Ordered',
    21               TO_DATE ('12/23/2009 3:04:24', 'mm/dd/yyyy hh:mi:ss PM')
    22          FROM DUAL
    23        UNION ALL
    24        SELECT '1-11TWQL', 'D1', 'RFS',
    25               TO_DATE ('12/23/2009 3:04:23', 'mm/dd/yyyy hh:mi:ss PM')
    26          FROM DUAL
    27        UNION ALL
    28        SELECT '1-11TWQL', 'D1', 'Rejected',
    29               TO_DATE ('12/23/2009 3:04:22', 'mm/dd/yyyy hh:mi:ss PM')
    30          FROM DUAL
    31        UNION ALL
    32        SELECT '1-11TWQL', 'D1', 'Validated',
    33               TO_DATE ('12/23/2009 3:04:23', 'mm/dd/yyyy hh:mi:ss PM')
    34          FROM DUAL
    35        UNION ALL
    36        SELECT '1-249A8X', 'COPS', 'RFS',
    37               TO_DATE ('3/5/2010 12:04:24', 'mm/dd/yyyy hh:mi:ss PM')
    38          FROM DUAL
    39        UNION ALL
    40        SELECT '1-249A8X', 'COPS', 'RFS',
    41               TO_DATE ('3/16/2010 7:55:50', 'mm/dd/yyyy hh:mi:ss PM')
    42          FROM DUAL
    43        UNION ALL
    44        SELECT '1-249A8X', 'COPS', 'RFS',
    45               TO_DATE ('3/16/2010 7:55:51', 'mm/dd/yyyy hh:mi:ss PM')
    46          FROM DUAL
    47        UNION ALL
    48        SELECT '1-249A8X', 'COPS', 'Rejected',
    49               TO_DATE ('3/5/2010 12:04:24', 'mm/dd/yyyy hh:mi:ss PM')
    50          FROM DUAL
    51        UNION ALL
    52        SELECT '1-249A8X', 'COPS', 'Rejected',
    53               TO_DATE ('3/16/2010 7:55:50', 'mm/dd/yyyy hh:mi:ss PM')
    54          FROM DUAL
    55        UNION ALL
    56        SELECT '1-249A8X', 'COPS', 'Rejected',
    57               TO_DATE ('3/16/2010 7:55:51', 'mm/dd/yyyy hh:mi:ss PM')
    58          FROM DUAL
    59        UNION ALL
    60        SELECT '1-249A8X', 'COPS', 'Validated',
    61               TO_DATE ('12/23/2009 3:37:54', 'mm/dd/yyyy hh:mi:ss PM')
    62          FROM DUAL
    63        UNION ALL
    64        SELECT '1-249A8X', 'COPS', 'Validated',
    65               TO_DATE ('12/23/2009 3:37:54', 'mm/dd/yyyy hh:mi:ss PM')
    66          FROM DUAL
    67        UNION ALL
    68        SELECT '1-249A8X', 'COPS', 'Validated',
    69               TO_DATE ('12/23/2009 3:37:54', 'mm/dd/yyyy hh:mi:ss PM')
    70          FROM DUAL
    71        UNION ALL
    72        SELECT '1-249A8X', 'D1', 'Ordered',
    73               TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
    74          FROM DUAL
    75        UNION ALL
    76        SELECT '1-249A8X', 'D1', 'Ordered',
    77               TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
    78          FROM DUAL
    79        UNION ALL
    80        SELECT '1-249A8X', 'D1', 'RFS',
    81               TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
    82          FROM DUAL
    83        UNION ALL
    84        SELECT '1-249A8X', 'D1', 'RFS',
    85               TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
    86          FROM DUAL
    87        UNION ALL
    88        SELECT '1-249A8X', 'D1', 'Validated',
    89               TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
    90          FROM DUAL
    91        UNION ALL
    92        SELECT '1-249A8X', 'D1', 'Validated',
    93               TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
    94          FROM DUAL
    95        UNION ALL
    96        SELECT '1-249A8Z', 'COPS', 'Validated',
    97               TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
    98          FROM DUAL
    99        UNION ALL
    100        SELECT '1-249A8Z', 'COPS', 'Ordered',
    101               TO_DATE ('3/26/2010 12:32:27', 'mm/dd/yyyy hh:mi:ss PM')
    102          FROM DUAL)
    103  select vt.quote_id,
    104         vt.account,
    105         vt.status,
    106         vt.captured_date,
    107         case when vt.rn = 1 then 'N'
    108              else 'Y'
    109         end deleted_flag
    110    from (SELECT quote_id, ACCOUNT, status, captured_date,
    111                 DENSE_RANK () OVER (PARTITION BY quote_id ORDER BY quote_id, ACCOUNT) rn
    112            FROM temp
    113          ORDER BY quote_id, captured_date) vt;
    QUOTE_ID ACCOUNT STATUS    CAPTURED_DATE DELETED_FLAG
    1-11TWQL D1      Rejected  23-Dec-2009 3 Y
    1-11TWQL D1      RFS       23-Dec-2009 3 Y
    1-11TWQL D1      Validated 23-Dec-2009 3 Y
    1-11TWQL D1      Ordered   23-Dec-2009 3 Y
    1-11TWQL COPS    RFS       23-Dec-2009 3 N
    1-11TWQL COPS    Validated 23-Dec-2009 3 N
    1-11TWQL COPS    Rejected  23-Dec-2009 3 N
    1-11TWQL COPS    Ordered   23-Dec-2009 3 N
    1-249A8X COPS    Validated 23-Dec-2009 3 N
    1-249A8X COPS    Validated 23-Dec-2009 3 N
    1-249A8X COPS    Validated 23-Dec-2009 3 N
    1-249A8X COPS    RFS       05-Mar-2010 1 N
    1-249A8X COPS    Rejected  05-Mar-2010 1 N
    1-249A8X COPS    RFS       16-Mar-2010 7 N
    1-249A8X COPS    Rejected  16-Mar-2010 7 N
    1-249A8X COPS    Rejected  16-Mar-2010 7 N
    1-249A8X COPS    RFS       16-Mar-2010 7 N
    1-249A8X D1      Ordered   26-Mar-2010 1 Y
    1-249A8X D1      Ordered   26-Mar-2010 1 Y
    1-249A8X D1      RFS       26-Mar-2010 1 Y
    1-249A8X D1      RFS       26-Mar-2010 1 Y
    1-249A8X D1      Validated 26-Mar-2010 1 Y
    1-249A8X D1      Validated 26-Mar-2010 1 Y
    1-249A8Z COPS    Validated 26-Mar-2010 1 N
    1-249A8Z COPS    Ordered   26-Mar-2010 1 N
    25 rows selected
    SQL>

  • Need help on oracle sql query

    Hi team,
    Please help me on below query,
    I have table like given below
    Tran_Id  tran_date   amount. Actorid
       1.         10-apr-15.   100.         1
       2.         11-apr-15.   100.         1
       3.         11-apr-15.   900.         1
       4.         12-apr-15.   100.         1
       5.         13-apr-15.   350.         1
       6.         14-apr-15.   400.         1
    Now please find the query,
    I want all the actor ids whos tran amount
    >1500 and the  date when the tran amount
    Has breached
    Ex:
    Actor-id.  Breached-date.    Total
      1.              13-apr-15.            1900
    How can I write a query for above requirement?
    Regards,
    Rajendra

    Your solution (same as Saubhik's) is incorrect. Look at source data - multiple transactions can occur same day. Therefore, your qury will return wrong results if breached amount is 1000:
    with trans as (
    select  1  tran_Id, to_date('10-apr-15', 'dd-mon-yy') tran_date,  100 amount, 1 actorid from dual union all
    select  2,  to_date('11-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
    select  3,  to_date('11-apr-15', 'dd-mon-yy'), 900, 1 from dual union all
    select  4,  to_date('12-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
    select  5,  to_date('13-apr-15', 'dd-mon-yy'), 350, 1 from dual union all
    select  6,  to_date('14-apr-15', 'dd-mon-yy'), 400, 1 from dual union all
    select  7,  to_date('12-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
    select  8,  to_date('13-apr-15', 'dd-mon-yy'), 1200, 2 from dual union all
    select  9,  to_date('14-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
    select  10,  to_date('15-apr-15', 'dd-mon-yy'), 300, 2 from dual
    trans_running_tot as (
    select tran_id, tran_date,
      sum(amount) over (partition by actorid order by tran_date) tot_amt, actorid
    from trans
    trans_ranked as (
    select actorid,tran_id, tran_date,
      rank() over (partition by actorid order by tot_amt) rk
    from trans_running_tot
    where tot_amt > 1000
    select * from trans_ranked where rk=1
       ACTORID    TRAN_ID TRAN_DATE         RK
             1          2 11-APR-15          1 -- here total amount was only 200
             1          3 11-APR-15          1
             2          8 13-APR-15          1
    SQL>
    As you can see, 2 rows were returned for actor 1. Why? Default for analytic ORDER BY is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. Therefore, all rows with same transation_date will fall into same window:
    SQL>  with trans as (
      2   select  1  tran_Id, to_date('10-apr-15', 'dd-mon-yy') tran_date,  100 amount, 1 actorid from dual union all
      3   select  2,  to_date('11-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
      4   select  3,  to_date('11-apr-15', 'dd-mon-yy'), 900, 1 from dual union all
      5   select  4,  to_date('12-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
      6   select  5,  to_date('13-apr-15', 'dd-mon-yy'), 350, 1 from dual union all
      7   select  6,  to_date('14-apr-15', 'dd-mon-yy'), 400, 1 from dual union all
      8   select  7,  to_date('12-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
      9   select  8,  to_date('13-apr-15', 'dd-mon-yy'), 1200, 2 from dual union all
    10   select  9,  to_date('14-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
    11   select  10,  to_date('15-apr-15', 'dd-mon-yy'), 300, 2 from dual
    12   )
    13  select tran_id, tran_date,
    14    sum(amount) over (partition by actorid order by tran_date) tot_amt, actorid
    15  from trans
    16  /
       TRAN_ID TRAN_DATE    TOT_AMT    ACTORID
             1 10-APR-15        100          1
             2 11-APR-15       1100          1
             3 11-APR-15       1100          1
             4 12-APR-15       1200          1
             5 13-APR-15       1550          1
             6 14-APR-15       1950          1
             7 12-APR-15        300          2
             8 13-APR-15       1500          2
             9 14-APR-15       1800          2
            10 15-APR-15       2100          2
    10 rows selected.
    SQL>
    So correct solution is to ORDER BY transation id. Also, just in case if amount can be 0, we should add tranaction id when ordering by sum:
    with trans as (
    select  1  tran_Id, to_date('10-apr-15', 'dd-mon-yy') tran_date,  100 amount, 1 actorid from dual union all
    select  2,  to_date('11-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
    select  3,  to_date('11-apr-15', 'dd-mon-yy'), 900, 1 from dual union all
    select  4,  to_date('12-apr-15', 'dd-mon-yy'), 100, 1 from dual union all
    select  5,  to_date('13-apr-15', 'dd-mon-yy'), 350, 1 from dual union all
    select  6,  to_date('14-apr-15', 'dd-mon-yy'), 400, 1 from dual union all
    select  7,  to_date('12-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
    select  8,  to_date('13-apr-15', 'dd-mon-yy'), 1200, 2 from dual union all
    select  9,  to_date('14-apr-15', 'dd-mon-yy'), 300, 2 from dual union all
    select  10,  to_date('15-apr-15', 'dd-mon-yy'), 300, 2 from dual
    trans_running_tot as (
    select tran_id, tran_date,
      sum(amount) over (partition by actorid order by tran_id) tot_amt, actorid
    from trans
    trans_ranked as (
    select actorid,tran_id, tran_date,
      rank() over (partition by actorid order by tot_amt,tran_id) rk
    from trans_running_tot
    where tot_amt > 1000
    select * from trans_ranked where rk=1
       ACTORID    TRAN_ID TRAN_DATE         RK
             1          3 11-APR-15          1
             2          8 13-APR-15          1
    SQL>
    SY.

  • Could you please help me make this query less complicated

    could you please help me make this query less complicated
    select t1.R_OBJECT_ID
    from dm_relation_sp a, ddt_resolution_sp t1
    where a.parent_id = '0900000283560456' -----------ID
    and a.child_id = t1.R_OBJECT_ID
    union all
    select t3.R_OBJECT_ID
    from ddt_resolution_sp t3
    where t3.R_OBJECT_ID in (select t2.child_id
    from dm_relation_sp t2
    where t2.parent_id in (select a.child_id
    from asud_fsk.dm_relation_sp a, asud_fsk.ddt_resolution_sp t1
    where a.parent_id = '0900000283560456' -----------ID
    and a.child_id = t1.R_OBJECT_ID))
    union all
    select t4.R_OBJECT_ID
    from ddt_resolution_sp t4
    where t4.R_OBJECT_ID in(
    select t3.child_id from dm_relation_sp t3
    where t3.parent_id in (
    select t2.child_id
    from asud_fsk.dm_relation_sp t2
    where t2.parent_id in (select a.child_id
    from asud_fsk.dm_relation_sp a, asud_fsk.ddt_resolution_sp t1
    where a.parent_id = '0900000283560456' -----------ID
    and a.child_id = t1.R_OBJECT_ID))
    and t3.relation_name = 'RESOLUTION_RELATION')
    union all
    select t5.R_OBJECT_ID
    from ddt_resolution_sp t5
    where t5.R_OBJECT_ID in
    (select t4.child_id
    from dm_relation_sp t4
    where t4.parent_id in(
    select t3.child_id from dm_relation_sp t3
    where t3.parent_id in (
    select t2.child_id
    from asud_fsk.dm_relation_sp t2
    where t2.parent_id in (select a.child_id
    from asud_fsk.dm_relation_sp a, asud_fsk.ddt_resolution_sp t1
    where a.parent_id = '0900000283560456' -----------ID
    and a.child_id = t1.R_OBJECT_ID))
    and t3.relation_name = 'RESOLUTION_RELATION')
    and t4.relation_name = 'RESOLUTION_RELATION')
    Edited by: user13025450 on 29.04.2010 16:23

    Hi,
    Welcome to the forum! You'll find that there are many qualified people (such as Tubby) willing to help you. Will you do what you can to help them? In order to simplify the query you posted,someone will first have to understand what it does, what the tables that it uses are like, and what tools you have to work with (that is, what version of Oracle, and any other software you are using). To actually test their ideas, people will need versions of your tables.
    Many people, if they spent enough time, might be able to figure out roughly what you are doing, make some tables themselves and test a solution. You can help with all of that. I assume you already know what the appliction is, and what this particular query is supposed to do: you don't have to figure out any of that, you just have to say it. You know what all your tables are, what the datatypes of all the columns are, and what kinds of data are in the tables: you don't have to guess at any of that.
    Describe what you're doing. Think about it: how do we know that
    SELECT  NULL
    FROM    dual;doesn't do what you want?
    Post CREATE TABLE and INSERT statements for a little sample data.
    Post the results that this query is supposed to produce from those results. (Is it producing the right output now? Then it should be easy to post the correct results.)
    Describe, as well as you can, how the present query is doing it.
    Format your existing code, so it's easy to see what the different branches of the UNION are, and what the main clauses are in each one.
    When posting formatted text (code or results) type these 6 characters
    \(all small letters, inside curly brackets) before and after each formatted section, to keep this site from compressing the spaces.
    Say what versions of Oracle (e.g. 10.2.0.3.0) and any other relevant software you are using.
    I know it's a lot of work, but it really helps. You can do it as well (probably better) than anyone else, and if you're unwilling to do it, why should anyone else be willing?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Help understanding simple query trace with 35,000 "query subcube" events

    No matter WHAT query I issue against my cube, I'm finding thousands of query subcube events against an unexpected , I assume it's a partition.
    Even for the simplest possible query SELECT FROM <CUBE>, which for this cube would return the measure [MRN Distinct Count], a trace reveals 35,000 query subcube events against a partition (?) called "<server>.Medical Home.MedHome.Lab Orders
    With Results" which is NOT the partition that holds that [MRN Distinct Count] measure.
    I can't figure out why this object is being queried so many times for queries completely unrelated to it.  I believe this performance degradation is somewhat new...so I have been trying to think of what may have been added to the cube recently.
    I recently added KPIs and calculated sets & members that DO draw from that Lab Orders With Results object...would calculated members or KPI's cause this kind of problem? I thought that they were all pre-calculated @ processing?
    Thanks for any insight you can provide
    Here's a summary of the profiler trace on the query SELECT FROM <CUBE>
    Event Desc
    # of Events
    ObjectPath
    Duration
    Query Begin
    1
    NULL
    0
    Query Cube Begin
    1
    <server>.Medical Home.MedHome
    0
    Query Subcube Verbose
    11
    <server>.Medical Home.MedHome.Fact Dx 2
    189
    Query Dimension
    7
    <server>.Medical Home.Dim Person
    0
    Query Subcube Verbose
    35650
    <server>.Medical Home.MedHome.Lab Orders With Results
    13856
    Serialize Results Begin
    1
    <server>.Medical Home.MedHome
    0
    Serialize Results Current
    4
    <server>.Medical Home.MedHome
    0
    Query Subcube Verbose
    5
    <server>.Medical Home.MedHome.Dim Person
    0
    Serialize Results Current
    1
    <server>.Medical Home.MedHome
    0
    Serialize Results End
    1
    <server>.Medical Home.MedHome
    0
    Query Cube End
    1
    <server>.Medical Home.MedHome
    40919
    Query End
    1
    NULL
    41059
    Paul

    RedBirdRising - my cube had several dynamic calculated sets in it.  As I recall several of those sets included slices on that one particular dimension that was being queried so many times. For me it was sets that compiled populations of people
    with particular lab result values, so that lab result partition was queried thousands of times for ANY query, even simple ones like SELECT FROM [CUBE].  
    To be precise, I actually had calculated sets interacting with calculated measures - for example, one of the offending calculated sets was really this series of calculations:
    -CALC SET 1: define calculated set of diabetic patients
    -CALC MEASURE 1: define calculated measure for most recent lab test result of a particular lab test - which included a slice on the dimension I found being queried thousands of times.
    -DYNAMIC calculated set: FILTER(CALC SET1, CALC MEASURE 1 <= x)
    So as soon as I switched that dynamic set to a STATIC set my queries began executing in a reasonable amount of time and those query subcube events were no longer issued in the thousands.  I feel like i've heard that FILTER performs poorly but didn't
    know of any alternatives to achieve what we needed.
    hope this helps...I can't say I know for sure what unforseen impact(s) switching to static sets has had yet.  Dynamic seemed like the way to go but the performance hit was just too drastic.  
    Paul

  • Analytic query to select next record

    Hi all,
    I would like to query the below two table match to ouput.
    1.dev_wt 2.dev_map 3.Output Result
    To help more clearance please see this image link : http://lh6.ggpht.com/_xL6eBqjW6Yo/TEqnSvlF_FI/AAAAAAAAB0U/i2sclnnaj6g/Untitled-3.jpg
    1. dev_wt
    PMS_COMP     PMS_I       PMS_PERF_D   PMS_WT   PMS_CREATION_D
    BBOARD     GICEQGROSS    04/01/2001     30     04/05/2001
    BBOARD     GICST_B       04/01/2001     5      04/05/2001
    BBOARD     SBGS_B        04/01/2001     65     04/05/2001
    BBOARD     GICEQGROSS    04/11/2001     30     04/15/2001
    BBOARD     GICST_B       04/11/2001     5      04/15/2001
    BBOARD     SBGS_B        04/11/2001     65     04/15/20012. dev_map
    GS_CODE     GS_I_CODE    GS_I_ID  MD_ID   GS_START_DT    GS_END_DT
    GICEQGROSS   CIWL        304       15     01/04/1998     31/03/2004
    GICEQGROSS   CIWL        304       2     01/04/2004      31/03/9998
    GICST_B      GICST_B     3707      15     01/04/2000     31/12/9998
    SBGS_B       SBGS_B      2231      15     01/04/1992     30/09/2003
    SBGS_B       SBGS_B      564       15     01/10/2003     31/12/9998I would like to match PMS_I = GS_CODE to retrieve GS_I_CODE and using analytic
    query to find next record.
    Because I need to select GICEQGROSS record of PMS_PERF_D date and next
    GICEQGROSS record of PMS_PERF_D from dev_wt table and put GS_WT_FR and GS_WT_TO of Output result.
    ***Date is change to YYYYMMDD format
    ***PMS_WT is devided by 100
    *3. Output Result*
    GS_I_ID    PMS_COMP     GS_I_CODE     GS_WT_FR     GS_WT_TO     GS_I_CALC
    304       BBOARD        CIWL          20010401     20010410     0.3
    3707      BBOARD        GICST_B       20010401     20010410     0.05
    5209      BBOARD        SBGS_B        20010401     20010410     0.65PMS_COMP is from dev_wt table
    GS_I_CODE is from dev_map table join with dev_wt
    GS_WT_FR is from dev_wt table of GS_START_DT
    GS_WT_TO is from dev_wt table of next record GS_START_DT where PMS_I ='GICEQGROSS'
    Now my difficulty is to select next record of PMS_PERF_D using analytic query. Below
    is my query...
    SELECT GS_I_ID, PMS_COMP, GS_I_CODE, GS_WT_FR, GS_WT_TO, GS_I_CALC
    FROM dev_wt (
    SELECT lead(PMS_PERF_D) over(partition by PMS_I order by PMS_PERF_D) as GS_WT_TO       
    FROM dev_wt where PMS_I ='GICEQGROSS')
    left join dev_map on PMS_I = GS_CODE ;Thanks
    Edited by: WinZone on Jul 24, 2010 4:46 PM
    Edited by: WinZone on Jul 24, 2010 4:50 PM

    Hi,
    This query should be fine:
    SELECT DISTINCT t2.gs_i_id, pms_comp, t2.gs_i_code,
                    TO_CHAR
                       (MIN (pms_perf_d) OVER (PARTITION BY pms_comp, pms_i),
                        'yyyymmdd'
                       ) gs_wt_fr,
                    TO_CHAR
                       (MAX (pms_perf_d) OVER (PARTITION BY pms_comp, pms_i) - 1,
                        'yyyymmdd'
                       ) gs_wt_to,
                    pms_wt / 100 gs_i_calc
               FROM dev_wt t1, dev_map t2
              WHERE t2.gs_code = t1.pms_iREM Same remark as odie: should be "2231" instead ...

  • Query Help-2

    Query Help:
    http://forum.java.sun.com/thread.jsp?forum=45&thread=471180&tstart=15&trange=15
    It seems I have confused enough people with my improper presentation of query. Sorry guys. I will restate my question with different table names.
    The above was my previous posting, which was not clear..so Iam restating my problem as follows....
    I have the following tables
    Customer(custID, Name, Address)
    Order(custID, OrderID, orderDate)
    CreditCard(custID, creditCard#, creditCardType)
    Now if I have 3 records in Order with custID 100 and 2 records in CreditCard as
    Order:
    100,A001,11/22/03
    100,A002,11/24/03
    100,A003,12/02/03
    CreditCard:
    100,42323232..., VISA
    100,5234234...., MASTER
    Now how can I get
    custID, Name, Address, OrderID, orderDate, creditCard#, creditCarType
    data in minimum no. of records....
    I think I have made my query clear..
    now please help me guys...
    thanks so much for your help.

    You are right.
    But frankly the actual tables on my database are not customer,orders and creditcards..but I just tried to reproduce the problem with these tables, please ignore that user needs a refund etc situtaion. If the tables were actually order,creditcards etc..it would have been a problem to be considered.
    Can you please help me with the query
    if I have m rows in Order and n rows in CreditCard. I will get m*n records, I looking for max(m,n).
    With the following fields in my query result,
    custID, Name, Address, OrderID, orderDate, creditCard#, creditCarType
    from Customer, Order, CreditCard tables
    Thanks so much for your htlp

  • Query help,  Percentages / ratio to reports / nests

    Hi
    I have a query that returns data like this
    D_NO POINTS COUNT_POINTS ID_COUNT
    4002 L_T_69 12 282
    4219 L_T_69 1 151
    4228 L_T_69 18 193
    4229 L_T_69 7 181
    4230 L_T_69 0 197
    I need to also output a column that works out a percentage of count_points and Id_count. e.g 12/282 * 100 = 4.2
    I had a try with ratio to reports function but no joy for me. I think i need to add in
    another nested select or something but what i was trying wasnt working.
    Can anyone help.
    here is the query so far
    SELECT D_NO,
    GROUPS.POINTS,
    DECODE(GROUPS.POINTS, 'L_T_69' , L_T_69) AS COUNT_POINTS,
    ID_COUNT
    FROM
         (SELECT D_NO,
         Count (CASE WHEN VERBAL <= 69 THEN 1
              END) AS L_T_69,
         COUNT(ID_NUMBER) AS ID_COUNT
         FROM TBL_1
         WHERE VERBAL IS NOT NULL
         group by D_NO)
    TBL_1,
    ( SELECT 'L_T_69' POINTS FROM DUAL )GROUPS
    thank you

    Not sure if this is what you're looking for but it may give you some clues:
    select object_type
          ,has_a_c
          ,type_total
          ,round(100 * (has_a_c / type_total),2) ratio
    from
       select object_type
             ,sum (case when instr(object_name,'C') <> 0 then 1
                        else 0
                   end) has_a_c
             ,count(*) type_total
       from   all_objects
       group by object_type
    OBJECT_TYPE          HAS_A_C   TYPE_TOTAL   RATIO
    CONSUMER GROUP             1            2      50
    EVALUATION CONTEXT         1            1     100
    FUNCTION                  50          113   44.25
    INDEX                      7           20      35
    LIBRARY                    0            2       0
    OPERATOR                   1            2      50
    PACKAGE                  500         1158   43.18
    PACKAGE BODY             487         1126   43.25
    PROCEDURE                 54           86   62.79
    SEQUENCE                  62          116   53.45
    SYNONYM                 1060         2298   46.13
    TABLE                    365          721   50.62
    TABLE PARTITION           15           15     100
    TYPE                     104          272   38.24
    VIEW                     834         1896   43.99
    15 rows selected.

  • Help needed for writing query

    help needed for writing query
    i have the following tables(with data) as mentioned below
    FK*-foregin key (SUBJECTS)
    FK**-foregin key (COMBINATION)
    1)SUBJECTS(table name)     
    SUB_ID(NUMBER) SUB_CODE(VARCHAR2) SUB_NAME (VARCHAR2)
    2           02           Computer Science
    3           03           Physics
    4           04           Chemistry
    5           05           Mathematics
    7           07           Commerce
    8           08           Computer Applications
    9           09           Biology
    2)COMBINATION
    COMB_ID(NUMBER) COMB_NAME(VARCHAR2) SUB_ID1(NUMBER(FK*)) SUB_ID2(NUMBER(FK*)) SUB_ID3(NUMBER(FK*)) SUBJ_ID4(NUMBER(FK*))
    383           S1      9           4           2           3
    384           S2      4           2           5           3
    ---------I actually designed the ABOVE table also like this
    3) a)COMBINATION
    COMB_ID(NUMBER) COMB_NAME(VARCHAR2)
    383           S1
    384           S2
    b)COMBINATION_DET
    COMBDET_ID(NUMBER) COMB_ID(FK**) SUB_ID(FK*)
    1               383          9
    2               383          4
    3               383          2
    4               383          3
    5               384          4
    6               384          2          
    7               384          5
    8               384          3
    Business rule: a combination consists of a maximum of 4 subjects (must contain)
    and the user is less relevant to a COMB_NAME(name of combinations) but user need
    the subjects contained in combinations
    i need the following output
    COMB_ID COMB_NAME SUBJECT1 SUBJECT2      SUBJECT3      SUBJECT4
    383     S1     Biology Chemistry      Computer Science Physics
    384     S2     Chemistry Computer Science Mathematics Physics
    or even this is enough(what i actually needed)
    COMB_ID     subjects
    383           Biology,Chemistry,Computer Science,Physics
    384           Chemistry,Computer Science,Mathematics,Physics
    you can use any of the COMBINATION table(either (2) or (3))
    and i want to know
    1)which design is good in this case
    (i think SUB_ID1,SUB_ID2,SUB_ID3,SUB_ID4 is not a
    good method to link with same table but if 4 subjects only(and must) comes
    detail table is not neccessary )
    now i am achieving the result by program-coding in C# after getting the rows from oracle
    i am using oracle 9i (also ODP.NET)
    i want to know how can i get the result in the stored procedure itsef.
    2)how it could be designed in any other way.
    any help/suggestion is welcome
    thanks for your time --Pradeesh

    Well I forgot the table-alias, here now with:
    SELECT C.COMB_ID
    , C.COMB_NAME
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID1) AS SUBJECT_NAME1
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID2) AS SUBJECT_NAME2
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID3) AS SUBJECT_NAME3
    , (SELECT SUB_NAME
    FROM SUBJECTS
    WHERE SUB_ID = C.SUB_ID4) AS SUBJECT_NAME4
    FROM COMBINATION C;
    As you need exactly 4 subjects, the columns-solution is just fine I would say.

  • HELP TO TUNE THIS QUERY

    Hi,
    I'm using below query in procedure.It's taking more more time can some one help to tune this query or advice to rewrite the query.
    Databse :10.1
    SELECT   'Reading Comprehension' TEST_NAME,T.TEST_END_DATE TEST_SESSION_DATE,
            C.POOL_VERSION_ID, I.CREATED_ON POOL_CREATED_DT,
            C.ITEM_ID, C.ITEM_RESPONSE_ID, S.STUDENT_ID_PK, C.RESPONSE_KEY, C.IS_CORRECT RESPONSE_IS_CORRECT,
            T.SCORE SCALE_SCORE, C.RESPONSE_DURATION, P.ITEM_KEY,
            T.TEST_SESSION_DETAIL_ID, SYSDATE CREATED_ON
           -- BULK COLLECT INTO TV_PSYCHO_DET
            FROM
            CAT_ITEM_PARAMETER P, CAT_ITEM_USER_RESPONSE C, TEST_SESSION_DETAIL T,
            TEST_SESSION S, ITEM_POOL_VERSION I, TEST_DETAIL D
            ,INSTITUTION E
            WHERE  TRUNC(T.TEST_END_DATE) BETWEEN TO_DATE('01-11-09','dd-mm-yy') AND TO_DATE('30-11-09','dd-mm-yy')
            AND D.TEST_NAME =  'Reading Comprehension'
            AND T.TEST_SESSION_STATUS_ID = 3
            AND I.POOL_AVAILABILITY='Y'
            AND P.PRETEST=0 AND C.RESTART_FLAG=0
            AND T.TEST_DETAIL_ID = D.TEST_DETAIL_ID
            AND S.TEST_SESSION_ID = T.TEST_SESSION_ID
            AND C.TEST_SESSION_DETAIL_ID = T.TEST_SESSION_DETAIL_ID
            AND S.INSTITUTION_ID=E.INSTITUTION_ID
            AND SUBSTR(E.INSTITUTION_ID_DISPLAY,8,3) <> '000'
            AND I.ITEM_ID = C.ITEM_ID
            AND P.ITEM_ID = I.ITEM_ID;expln plan
    Plan hash value: 3712814491                                                                                                      
    | Id  | Operation                                 | Name                        | Rows  | Bytes | Cost (%CPU)| Time     | Pstart|
    Pstop |                                                                                                                          
    |   0 | SELECT STATEMENT                          |                             | 50857 |  7151K| 93382   (1)| 00:18:41 |       |
          |                                                                                                                          
    |*  1 |  FILTER                                   |                             |       |       |            |          |       |
          |                                                                                                                          
    |*  2 |   HASH JOIN                               |                             | 50857 |  7151K| 93382   (1)| 00:18:41 |       |
          |                                                                                                                          
    |   3 |    PARTITION HASH ALL                     |                             |  2312 | 23120 |    25   (0)| 00:00:01 |     1 |
        5 |                                                                                                                          
    |*  4 |     TABLE ACCESS FULL                     | CAT_ITEM_PARAMETER          |  2312 | 23120 |    25   (0)| 00:00:01 |     1 |
        5 |                                                                                                                          
    |*  5 |    HASH JOIN                              |                             | 94938 |    12M| 93356   (1)| 00:18:41 |       |
          |                                                                                                                          
    |*  6 |     TABLE ACCESS FULL                     | ITEM_POOL_VERSION           |  9036 |   132K|    30   (0)| 00:00:01 |       |
          |                                                                                                                          
    |*  7 |     TABLE ACCESS BY GLOBAL INDEX ROWID    | CAT_ITEM_USER_RESPONSE      |     9 |   279 |    18   (0)| 00:00:01 | ROWID |
    ROWID |                                                                                                                          
    |   8 |      NESTED LOOPS                         |                             | 45349 |  5270K| 93325   (1)| 00:18:40 |       |
          |                                                                                                                          
    |*  9 |       HASH JOIN                           |                             |  4923 |   423K| 11377   (1)| 00:02:17 |       |
          |                                                                                                                          
    |* 10 |        INDEX FAST FULL SCAN               | INSTI_ID_NAME_COUN_DISP_IDX |  8165 |   111K|    18   (0)| 00:00:01 |       |
          |                                                                                                                          
    |* 11 |        HASH JOIN                          |                             |  4923 |   355K| 11359   (1)| 00:02:17 |       |
          |                                                                                                                          
    |* 12 |         TABLE ACCESS BY GLOBAL INDEX ROWID| TEST_SESSION_DETAIL         |  4107 |   148K|  6804   (1)| 00:01:22 | ROWID |
    ROWID |                                                                                                                          
    |  13 |          NESTED LOOPS                     |                             |  4923 |   278K|  6806   (1)| 00:01:22 |       |
          |                                                                                                                          
    |* 14 |           INDEX RANGE SCAN                | TEST_DETAIL_AK_1            |     1 |    21 |     2   (0)| 00:00:01 |       |
          |                                                                                                                          
    |* 15 |           INDEX RANGE SCAN                | TEST_SESSION_DETAIL_FK2_I   | 39737 |       |   102   (0)| 00:00:02 |       |
          |                                                                                                                          
    |  16 |         PARTITION HASH ALL                |                             |  1672K|    25M|  4546   (1)| 00:00:55 |     1 |
        5 |                                                                                                                          
    |  17 |          TABLE ACCESS FULL                | TEST_SESSION                |  1672K|    25M|  4546   (1)| 00:00:55 |     1 |
        5 |                                                                                                                          
    |* 18 |       INDEX RANGE SCAN                    | CAT_ITEM_USER_RESP_IDX1     |    18 |       |     3   (0)| 00:00:01 |       |
          |                                                                                                                          
    Predicate Information (identified by operation id):                                                                              
       1 - filter(TO_DATE('01-11-09','dd-mm-yy')<=TO_DATE('30-11-09','dd-mm-yy'))                                                    
       2 - access("P"."ITEM_ID"="I"."ITEM_ID")                                                                                       
       4 - filter("P"."PRETEST"=0)                                                                                                   
       5 - access("I"."ITEM_ID"="C"."ITEM_ID")                                                                                       
       6 - filter("I"."POOL_AVAILABILITY"='Y')                                                                                       
       7 - filter(TO_NUMBER("C"."RESTART_FLAG")=0)                                                                                   
       9 - access("S"."INSTITUTION_ID"="E"."INSTITUTION_ID")                                                                         
      10 - filter(SUBSTR("E"."INSTITUTION_ID_DISPLAY",8,3)<>'000')                                                                   
      11 - access("S"."TEST_SESSION_ID"="T"."TEST_SESSION_ID")                                                                       
      12 - filter(TRUNC(INTERNAL_FUNCTION("T"."TEST_END_DATE"))>=TO_DATE('01-11-09','dd-mm-yy') AND "T"."TEST_SESSION_STATUS_ID"=3   
                  AND TRUNC(INTERNAL_FUNCTION("T"."TEST_END_DATE"))<=TO_DATE('30-11-09','dd-mm-yy'))                                 
      14 - access("D"."TEST_NAME"='Reading Comprehension')                                                                           
      15 - access("T"."TEST_DETAIL_ID"="D"."TEST_DETAIL_ID")                                                                         
      18 - access("C"."TEST_SESSION_DETAIL_ID"="T"."TEST_SESSION_DETAIL_ID")                                                         
    43 rows selected.Edited by: user575115 on Dec 18, 2009 12:31 AM

    When you see something like ...
       7 - filter(TO_NUMBER("C"."RESTART_FLAG")=0)                                                                                    It means that Oracle had to do a conversion for you since you aren't using the proper data type in your query.
    That would mean IF there is an index on that column, it won't be useable...

Maybe you are looking for