Running Total Variation Query Time Optimization

Hi all, 
I've been struggling with this query for a while. I need to set a customer specific running total for 10 million rows (reset for every customer). But every time the number goes negative, I need to set it as zero.
For example,
member no              amount            wallet
member1                 400                      400
member1                 -500                     0
member1                  200                    200
member2                  700                    700
member2                 -200                    500
Query:
DECLARE @member float
DECLARE @prev_member float
DECLARE @amount float
DECLARE @wallet float
DECLARE db_cursor CURSOR FOR  
SELECT [Member no], [Transaction Amount] 
FROM [wallet_master_3]
ORDER BY [Member No], [rownum]
FOR UPDATE
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @member, @amount
SET @prev_member = @member
set @wallet=0
WHILE @@FETCH_STATUS = 0
BEGIN   
       IF @prev_member <> @member set @wallet=0
       SET @wallet = @wallet + @amount
       IF @wallet < 0 SET @wallet = 0
       UPDATE [wallet_master_3] SET walletsize = @wallet
       WHERE CURRENT OF db_cursor
       set @prev_member=@member
       FETCH NEXT FROM db_cursor INTO @member, @amount
END   
CLOSE db_cursor   
DEALLOCATE db_cursor
I've tried using a cursor. In five minutes, it ran 17,000 rows but after running it for 15 hours, the code only manages to set the running total for 175,000 rows. I'm not exactly sure why. Is there a faster approach I can use? 
Thanks!

As an exercise a 'Quirky Update' may help you in this scenario. Try the below trick!
DECLARE @Wallet AS TABLE
MemberNo VARCHAR(10),
RowNum INT,
Amount INT,
Wallet INT
INSERT INTO @Wallet (MemberNo, RowNum, Amount) VALUES
('member1',1, 400),
('member1',2, -500),
('member1',3, 200),
('member2',1, 700),
('member2',2, -200)
DECLARE @RunTotal AS INT
UPDATE W1
SET
@RunTotal = W1.Wallet =
CASE
WHEN W1.RowNum = 1 THEN W1.Amount
WHEN @RunTotal + COALESCE(W1.Amount, W2.Amount) < 0 THEN 0
ELSE @RunTotal + COALESCE(W1.Amount, W2.Amount)
END
FROM @Wallet W1
LEFT OUTER JOIN @Wallet W2
ON W1.MemberNo = W2.MemberNo AND W2.RowNum = W1.RowNum - 1;
SELECT * FROM @Wallet;
RESULT
MemberNo RowNum Amount Wallet
member1 1 400 400
member1 2 -500 0
member1 3 200 200
member2 1 700 700
member2 2 -200 500
You can read more on 'Quirky Update' in below articles
Solving the Running Total and Ordinal Rank Problems - Jeff Moden
Robyn Page's SQL Server Cursor Workbench
NOTE: Please test it thoroughly before using in a production environment!
Krishnakumar S

Similar Messages

  • Needed a running total in query

    Dear Gurus...Can I've a running total in query like this:
    Item_ Open_Qty_ Rec_Qty_ Iss_Qty_ Bal_
    A 10 5 2 13
    A 0 4 5 12
    A 0 0 6 6
    In this query column Bal is required whose value should be calculated as:
    Bal = ((Open_Qty + Rec_Qty) - Iss_Qty)
    Thanx in advance.

    389 posts and still you seem unaware of:
    - how important it is to mention your database version.
    - the tag, which implicates that you haven't read the FAQ for a while.
    - the 'search' option you have on the right side of the screen.
    You can query running totals easily by using analytic function SUM.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Query is running from a long time

    it just keeps running for a long time without returning any results.
    I am pasting here the execution plan for this query,please let me know which part could tune ,
    | Id  | Operation                                  | Name                     | Rows  | Bytes | Cost (%CPU)| Time     |
    |   0 | SELECT STATEMENT                           |                          |    27 |  9720 |   182K  (1)| 00:42:31 |
    |   1 |  SORT GROUP BY                             |                          |    27 |  9720 |   182K  (1)| 00:42:31 |
    |   2 |   VIEW                                     |                          |    27 |  9720 |   182K  (1)| 00:42:31 |
    |   3 |    HASH GROUP BY                           |                          |    27 | 14283 |   182K  (1)| 00:42:31 |
    |*  4 |     HASH JOIN                              |                          |    27 | 14283 |   182K  (1)| 00:42:31 |
    |*  5 |      HASH JOIN                             |                          |    26 |  9568 |   166K  (1)| 00:38:56 |
    |   6 |       NESTED LOOPS                         |                          |    26 |  9048 |   160K  (1)| 00:37:29 |
    |   7 |        NESTED LOOPS                        |                          |    25 |  7950 |   160K  (1)| 00:37:28 |
    |*  8 |         HASH JOIN                          |                          |    25 |  7800 |   160K  (1)| 00:37:28 |
    |*  9 |          HASH JOIN                         |                          |    25 |  7050 |   134K  (1)| 00:31:18 |
    |* 10 |           HASH JOIN                        |                          |    25 |  6600 |   107K  (1)| 00:25:08 |
    |* 11 |            TABLE ACCESS FULL               | WC_WRK_SPEC_F            |  2288 | 84656 |  4405   (2)| 00:01:02 |
    |  12 |            NESTED LOOPS                    |                          | 16275 |  3607K|   103K  (1)| 00:24:06 |
    |  13 |             NESTED LOOPS                   |                          |   592 |   121K|   102K  (1)| 00:23:49 |
    |* 14 |              HASH JOIN                     |                          |   592 | 47952 | 99684   (1)| 00:23:16 |
    |* 15 |               TABLE ACCESS BY INDEX ROWID  | W_PERSON_D               | 14057 |   700K| 90700   (1)| 00:21:10 |
    |  16 |                BITMAP CONVERSION TO ROWIDS |                          |       |       |            |          |
    |  17 |                 BITMAP AND                 |                          |       |       |            |          |
    |  18 |                  BITMAP OR                 |                          |       |       |            |          |
    |* 19 |                   BITMAP INDEX SINGLE VALUE| IXDIM_W_PERSON_D_F27     |       |       |            |          |
    |* 20 |                   BITMAP INDEX SINGLE VALUE| IXDIM_W_PERSON_D_F27     |       |       |            |          |
    |* 21 |                   BITMAP INDEX SINGLE VALUE| IXDIM_W_PERSON_D_F27     |       |       |            |          |
    |* 22 |                   BITMAP INDEX SINGLE VALUE| IXDIM_W_PERSON_D_F27     |       |       |            |          |
    |* 23 |                   BITMAP INDEX SINGLE VALUE| IXDIM_W_PERSON_D_F27     |       |       |            |          |
    |* 24 |                  BITMAP INDEX SINGLE VALUE | W_PERSON_D_M14           |       |       |            |          |
    |  25 |               NESTED LOOPS                 |                          | 62915 |  1843K|  8983   (1)| 00:02:06 |
    |* 26 |                TABLE ACCESS FULL           | WC_WRK_SPEC_F            |  2288 | 57200 |  4405   (2)| 00:01:02 |
    |  27 |                BITMAP CONVERSION TO ROWIDS |                          |    27 |   135 |  8983   (1)| 00:02:06 |
    |* 28 |                 BITMAP INDEX SINGLE VALUE  | W_PER_RANK_F_F7          |       |       |            |          |
    |* 29 |              TABLE ACCESS BY INDEX ROWID   | W_PERSON_D               |     1 |   129 |     4   (0)| 00:00:01 |
    |* 30 |               INDEX RANGE SCAN             | W_PERSON_D_U1            |     1 |       |     2   (0)| 00:00:01 |
    |  31 |             BITMAP CONVERSION TO ROWIDS    |                          |    27 |   459 |   103K  (1)| 00:24:06 |
    |* 32 |              BITMAP INDEX SINGLE VALUE     | W_PER_RANK_F_F7          |       |       |            |          |
    |* 33 |           TABLE ACCESS FULL                | WC_CUST_INST_F           | 33386 |   586K| 26431   (2)| 00:06:11 |
    |* 34 |          TABLE ACCESS FULL                 | WC_CUST_INST_F           | 33386 |   978K| 26431   (2)| 00:06:11 |
    |* 35 |         INDEX UNIQUE SCAN                  | W_PERSON_D_P1            |     1 |     6 |     1   (0)| 00:00:01 |
    |  36 |        TABLE ACCESS BY INDEX ROWID         | W_PERSON_D               |     1 |    30 |     2   (0)| 00:00:01 |
    |* 37 |         INDEX UNIQUE SCAN                  | W_PERSON_D_P1            |     1 |       |     1   (0)| 00:00:01 |
    |* 38 |       VIEW                                 | index$_join$_003         |   287K|  5616K|  6215   (3)| 00:01:28 |
    |* 39 |        HASH JOIN                           |                          |       |       |            |          |
    |* 40 |         HASH JOIN                          |                          |       |       |            |          |
    |  41 |          BITMAP CONVERSION TO ROWIDS       |                          |   287K|  5616K|    47   (0)| 00:00:01 |
    |* 42 |           BITMAP INDEX SINGLE VALUE        | IXDIM_WC_CUST_ADDR_D_F07 |       |       |            |          |
    |  43 |          BITMAP CONVERSION TO ROWIDS       |                          |   287K|  5616K|    85   (0)| 00:00:02 |
    |* 44 |           BITMAP INDEX SINGLE VALUE        | IXDIM_WC_CUST_ADDR_D_F04 |       |       |            |          |
    |  45 |         BITMAP CONVERSION TO ROWIDS        |                          |   287K|  5616K|  5895   (1)| 00:01:23 |
    |  46 |          BITMAP INDEX FULL SCAN            | IXDIM_WC_CUST_ADDR_D_F02 |       |       |            |          |
    |  47 |      TABLE ACCESS BY INDEX ROWID           | WC_CUST_ADDR_D           |   152K|    23M| 15400   (1)| 00:03:36 |
    |  48 |       BITMAP CONVERSION TO ROWIDS          |                          |       |       |            |          |
    |  49 |        BITMAP AND                          |                          |       |       |            |          |
    |* 50 |         BITMAP INDEX SINGLE VALUE          | IXDIM_WC_CUST_ADDR_D_F07 |       |       |            |          |
    |* 51 |         BITMAP INDEX SINGLE VALUE          | IXDIM_WC_CUST_ADDR_D_F06 |       |       |            |          |
    |* 52 |         BITMAP INDEX SINGLE VALUE          | IXDIM_WC_CUST_ADDR_D_F05 |       |       |            |          |
    Predicate Information (identified by operation id):
       4 - access("T489839"."ROW_WID"="T555160"."CONTACT_WID")
       5 - access("T489839"."ROW_WID"="T555160"."CONTACT_WID")
       8 - access("T489839"."ROW_WID"="T565669"."CONTACT_WID")
       9 - access("T489839"."ROW_WID"="T565669"."CONTACT_WID")
      10 - access("T445248"."CONTACT_WID"="T565813"."CONTACT_WID")
      11 - filter("T565813"."WRK_SPECIALITY"='01 Allm??Allm??edicin')
      14 - access("T445248"."CONTACT_WID"="T489839"."ROW_WID")
      15 - filter("T489839"."X_BU_NAME"='NNEU Sweden' AND ("T489839"."VIS_PR_BU_ID"='1-A30BR' OR
                  "T489839"."VIS_PR_BU_ID"='1-A37AL' OR "T489839"."VIS_PR_BU_ID"='1-B67SM' OR "T489839"."VIS_PR_BU_ID"='1-DEN82'
                  OR "T489839"."VIS_PR_BU_ID"='Unspecified') AND ("T489839"."X_CUST_STAT_CD"='Active' OR
                  "T489839"."X_CUST_STAT_CD"='New' OR "T489839"."X_CUST_STAT_CD"='Pending Validation' OR
                  "T489839"."X_CUST_STAT_CD"='Unspecified'))
      19 - access("T489839"."VIS_PR_BU_ID"='1-A30BR')
      20 - access("T489839"."VIS_PR_BU_ID"='1-A37AL')
      21 - access("T489839"."VIS_PR_BU_ID"='1-B67SM')
      22 - access("T489839"."VIS_PR_BU_ID"='1-DEN82')
      23 - access("T489839"."VIS_PR_BU_ID"='Unspecified')
      24 - access("T489839"."EMP_FLG"='N')
      26 - filter("T565813"."WRK_SPECIALITY"='01 Allm??Allm??edicin')
      28 - access("T445248"."CONTACT_WID"="T565813"."CONTACT_WID")
      29 - filter("T489839"."X_BU_NAME"='NNEU Sweden' AND ("T489839"."VIS_PR_BU_ID"='1-A30BR' OR
                  "T489839"."VIS_PR_BU_ID"='1-A37AL' OR "T489839"."VIS_PR_BU_ID"='1-B67SM' OR "T489839"."VIS_PR_BU_ID"='1-DEN82'
                  OR "T489839"."VIS_PR_BU_ID"='Unspecified') AND "T489839"."EMP_FLG"='N')
      30 - access("T489839"."INTEGRATION_ID"="T489839"."INTEGRATION_ID")
      32 - access("T445248"."CONTACT_WID"="T489839"."ROW_WID")
      33 - filter("T565669"."X_REL_TYPE_CD"='Ass Spe' OR "T565669"."X_REL_TYPE_CD"='AssDist Health C' OR
                  "T565669"."X_REL_TYPE_CD"='Dis Health C Ctr' OR "T565669"."X_REL_TYPE_CD"='DisHealth C Ctr' OR
                  "T565669"."X_REL_TYPE_CD"='DistHealth C' OR "T565669"."X_REL_TYPE_CD"='Distr Healt Ch' OR
                  "T565669"."X_REL_TYPE_CD"='District Health' OR "T565669"."X_REL_TYPE_CD"='Graduate physician' OR
                  "T565669"."X_REL_TYPE_CD"='Home Health Care Physician' OR "T565669"."X_REL_TYPE_CD"='Spe RecSenior' OR
                  "T565669"."X_REL_TYPE_CD"='SpeRecSen' OR "T565669"."X_REL_TYPE_CD"='SpeRecSpec' OR
                  "T565669"."X_REL_TYPE_CD"='SpeRecep')
      34 - filter("T565669"."X_REL_TYPE_CD"='Ass Spe' OR "T565669"."X_REL_TYPE_CD"='AssDist Health C' OR
                  "T565669"."X_REL_TYPE_CD"='Dis Health C Ctr' OR "T565669"."X_REL_TYPE_CD"='DisHealth C Ctr' OR
                  "T565669"."X_REL_TYPE_CD"='DistHealth C' OR "T565669"."X_REL_TYPE_CD"='Distr Healt Ch' OR
                  "T565669"."X_REL_TYPE_CD"='District Health' OR "T565669"."X_REL_TYPE_CD"='Graduate physician' OR
                  "T565669"."X_REL_TYPE_CD"='Home Health Care Physician' OR "T565669"."X_REL_TYPE_CD"='Spe RecSenior' OR
                  "T565669"."X_REL_TYPE_CD"='SpeRecSen' OR "T565669"."X_REL_TYPE_CD"='SpeRecSpec' OR
                  "T565669"."X_REL_TYPE_CD"='SpeRecep')
      35 - access("T36044"."ROW_WID"="T445248"."CONTACT_WID")
      37 - access("T36044"."ROW_WID"="T445248"."CONTACT_WID")
      38 - filter("T555160"."COUNTRY"='Sweden' AND "T555160"."NN_PRIMARY"='Y')
      39 - access(ROWID=ROWID)
      40 - access(ROWID=ROWID)
      42 - access("T555160"."COUNTRY"='Sweden')
      44 - access("T555160"."NN_PRIMARY"='Y')
      50 - access("T555160"."COUNTRY"='Sweden')
      51 - access("T555160"."MAIN_ADDR_FLG"='Y')
      52 - access("T555160"."ACTIVE_FLG"='Y')

    Here I am pasting the tkprof's output:
    tkprof p002_ora_32614_MY_TEST_SESSION02.trc abc.txt sys=no sort=prsela exeela fchela
    Please suggest:
    TKPROF: Release 10.2.0.5.0 - Production on Wed Sep 28 14:21:48 2011
    Copyright (c) 1982, 2007, Oracle.  All rights reserved.
    Trace file: p002_ora_32614_MY_TEST_SESSION02.trc
    Sort options: prsela  exeela  fchela 
    count    = number of times OCI procedure was executed
    cpu      = cpu time in seconds executing
    elapsed  = elapsed time in seconds executing
    disk     = number of physical reads of buffers from disk
    query    = number of buffers gotten for consistent read
    current  = number of buffers gotten in current mode (usually for update)
    rows     = number of rows processed by the fetch or execute call
    WITH SASUBWITH0 AS
      (SELECT D1.c1 AS c1
       FROM
         (SELECT T489839.INTEGRATION_ID AS c1,
                         count(DISTINCT T36044.INTEGRATION_ID) AS c2
          FROM WC_WRK_SPEC_F T565813,
                             WC_CUST_ADDR_D T555160,
                                            W_PERSON_D T489839 /* Custom Contact Dim_W_PERSON_D_Contact */, WC_CUST_INST_F T565669,
                                                                                                                           W_PERSON_D T36044,
                                                                                                                                      W_PER_RANK_F T445248 /* Fact_W_PER_RANK_F */
          WHERE (T445248.CONTACT_WID = T565813.CONTACT_WID
                 AND T36044.ROW_WID = T445248.CONTACT_WID
                 AND T445248.CONTACT_WID = T489839.ROW_WID
                 AND T489839.ROW_WID = T555160.CONTACT_WID
                 AND T489839.ROW_WID = T565669.CONTACT_WID
                 AND T489839.EMP_FLG = 'N'
                 AND T489839.X_BU_NAME = 'NNEU Sweden'
                 AND T555160.NN_PRIMARY = 'Y'
                 AND T555160.COUNTRY = 'Sweden'
                 AND T565813.WRK_SPECIALITY = '01 Allm��Allm��edicin'
                 AND (T489839.VIS_PR_BU_ID IN ('1-A30BR',
                                               '1-A37AL',
                                               '1-B67SM',
                                               '1-DEN82',
                                               'Unspecified'))
                 AND (T489839.X_CUST_STAT_CD IN ('Active',
                                                 'New',
                                                 'Pending Validation',
                                                 'Unspecified'))
                 AND (T565669.X_REL_TYPE_CD IN ('Ass Spe',
                                                'AssDist Health C',
                                                'Dis Health C Ctr',
                                                'DisHealth C Ctr',
                                                'DistHealth C',
                                                'Distr Healt Ch',
                                                'District Health',
                                                'Graduate physician',
                                                'Home Health Care Physician',
                                                'Spe RecSenior',
                                                'SpeRecep',
                                                'SpeRecSen',
                                                'SpeRecSpec')))
          GROUP BY T489839.INTEGRATION_ID ) D1)
    SELECT T489839.SUPPRESS_MAIL_FLG AS c1,
                   CASE
                       WHEN T489839.JOB_TITLE = 'Unspecified' THEN ''
                       ELSE T489839.JOB_TITLE
                   END AS c2,
                   CASE
                       WHEN T489839.X_REFERENCE_TYPE_CD IN ('Unknown',
                                                            'Unspecified') THEN ''
                       ELSE T489839.X_REFERENCE_TYPE_CD
                   END AS c3,
                   T489839.FST_NAME AS c4,
                           T489839.MID_NAME AS c5,
                                   T489839.LAST_NAME AS c6,
                                           CASE
                                               WHEN CASE
                                                        WHEN NOT substr(T555160.INSTITUTION_FULL_NAME , 1, 3) IN ('WDK',
                                                                                                                  'WFI',
                                                                                                                  'WNO',
                                                                                                                  'WSE') THEN T555160.INSTITUTION_FULL_NAME
                                                        ELSE ''
                                                    END = 'M Hemadress' THEN ''
                                               ELSE CASE
                                                        WHEN NOT substr(T555160.INSTITUTION_FULL_NAME , 1, 3) IN ('WDK',
                                                                                                                  'WFI',
                                                                                                                  'WNO',
                                                                                                                  'WSE') THEN T555160.INSTITUTION_FULL_NAME
                                                        ELSE ''
                                                    END
                                           END AS c7,
                                           CASE
                                               WHEN CASE
                                                        WHEN NOT substr(T555160.INSTITUTION_NAME , 1, 3) IN ('WDK',
                                                                                                             'WFI',
                                                                                                             'WNO',
                                                                                                             'WSE') THEN T555160.INSTITUTION_NAME
                                                        ELSE ''
                                                    END = 'M Hemadress' THEN ''
                                               ELSE CASE
                                                        WHEN NOT substr(T555160.INSTITUTION_NAME , 1, 3) IN ('WDK',
                                                                                                             'WFI',
                                                                                                             'WNO',
                                                                                                             'WSE') THEN T555160.INSTITUTION_NAME
                                                        ELSE ''
                                                    END
                                           END AS c8,
                                           T555160.ADDR AS c9,
                                                   T555160.ZIPCODE AS c10,
                                                           T555160.CITY AS c11,
                                                                   T555160.NAME AS c12,
                                                                           T555160.PH_NUM AS c13,
                                                                                   CASE
                                                                                       WHEN T489839.X_COUNTY = 'Unspecified' THEN ''
                                                                                       ELSE T489839.X_COUNTY
                                                                                   END AS c14,
                                                                                   T555160.NN_PRIMARY AS c15,
                                                                                           T489839.X_External_ID AS c16,
                                                                                                   count(DISTINCT T36044.INTEGRATION_ID) AS c17
    FROM WC_WRK_SPEC_F T565813,
         WC_CUST_ADDR_D T555160,
         W_PERSON_D T489839 /* Custom Contact Dim_W_PERSON_D_Contact */, WC_CUST_INST_F T565669,
                                                                         W_PERSON_D T36044,
                                                                         W_PER_RANK_F T445248 /* Fact_W_PER_RANK_F */
    WHERE (T445248.CONTACT_WID = T565813.CONTACT_WID
           AND T36044.ROW_WID = T445248.CONTACT_WID
           AND T445248.CONTACT_WID = T489839.ROW_WID
           AND T489839.ROW_WID = T555160.CONTACT_WID
           AND T489839.ROW_WID = T565669.CONTACT_WID
           AND T489839.EMP_FLG = 'N'
           AND T489839.X_BU_NAME = 'NNEU Sweden'
           AND T555160.ACTIVE_FLG = 'Y'
           AND T555160.MAIN_ADDR_FLG = 'Y'
           AND T555160.COUNTRY = 'Sweden'
           AND T565813.WRK_SPECIALITY = '01 Allm��Allm��edicin'
           AND (T565669.X_REL_TYPE_CD IN ('Ass Spe',
                                          'AssDist Health C',
                                          'Dis Health C Ctr',
                                          'DisHealth C Ctr',
                                          'DistHealth C',
                                          'Distr Healt Ch',
                                          'District Health',
                                          'Graduate physician',
                                          'Home Health Care Physician',
                                          'Spe RecSenior',
                                          'SpeRecep',
                                          'SpeRecSen',
                                          'SpeRecSpec'))
           AND (T489839.VIS_PR_BU_ID IN ('1-A30BR',
                                         '1-A37AL',
                                         '1-B67SM',
                                         '1-DEN82',
                                         'Unspecified'))
           AND T489839.INTEGRATION_ID IN
             (SELECT DISTINCT SASUBWITH0.c1 AS c1
              FROM SASUBWITH0))
    GROUP BY T489839.FST_NAME, T489839.MID_NAME, T489839.LAST_NAME, T489839.SUPPRESS_MAIL_FLG, T489839.X_External_ID , T555160.NN_PRIMARY, T555160.NAME, T555160.ZIPCODE, T555160.ADDR, T555160.CITY, T555160.PH_NUM, CASE
                                                                                                                                                                                                                          WHEN T489839.JOB_TITLE = 'Unspecified' THEN ''
                                                                                                                                                                                                                          ELSE T489839.JOB_TITLE
                                                                                                                                                                                                                      END , CASE
                                                                                                                                                                                                                                WHEN T489839.X_REFERENCE_TYPE_CD IN ('Unknown',
                                                                                                                                                                                                                                                                     'Unspecified') THEN ''
                                                                                                                                                                                                                                ELSE T489839.X_REFERENCE_TYPE_CD
                                                                                                                                                                                                                            END , CASE
                                                                                                                                                                                                                                      WHEN T489839.X_COUNTY = 'Unspecified' THEN ''
                                                                                                                                                                                                                                      ELSE T489839.X_COUNTY
                                                                                                                                                                                                                                  END , CASE
                                                                                                                                                                                                                                            WHEN CASE
                                                                                                                                                                                                                                                     WHEN NOT substr(T555160.INSTITUTION_NAME , 1, 3) IN ('WDK',
                                                                                                                                                                                                                                                                                                          'WFI',
                                                                                                                                                                                                                                                                                                          'WNO',
                                                                                                                                                                                                                                                                                                          'WSE') THEN T555160.INSTITUTION_NAME
                                                                                                                                                                                                                                                     ELSE ''
                                                                                                                                                                                                                                                 END = 'M Hemadress' THEN ''
                                                                                                                                                                                                                                            ELSE CASE
                                                                                                                                                                                                                                                     WHEN NOT substr(T555160.INSTITUTION_NAME , 1, 3) IN ('WDK',
                                                                                                                                                                                                                                                                                                          'WFI',
                                                                                                                                                                                                                                                                                                          'WNO',
                                                                                                                                                                                                                                                                                                          'WSE') THEN T555160.INSTITUTION_NAME
                                                                                                                                                                                                                                                     ELSE ''
                                                                                                                                                                                                                                                 END
                                                                                                                                                                                                                                        END , CASE
                                                                                                                                                                                                                                                  WHEN CASE
                                                                                                                                                                                                                                                           WHEN NOT substr(T555160.INSTITUTION_FULL_NAME , 1, 3) IN ('WDK',
                                                                                                                                                                                                                                                                                                                     'WFI',
                                                                                                                                                                                                                                                                                                                     'WNO',
                                                                                                                                                                                                                                                                                                                     'WSE') THEN T555160.INSTITUTION_FULL_NAME
                                                                                                                                                                                                                                                           ELSE ''
                                                                                                                                                                                                                                                       END = 'M Hemadress' THEN ''
                                                                                                                                                                                                                                                  ELSE CASE
                                                                                                                                                                                                                                                           WHEN NOT substr(T555160.INSTITUTION_FULL_NAME , 1, 3) IN ('WDK',
                                                                                                                                                                                                                                                                                                                     'WFI',
                                                                                                                                                                                                                                                                                                                     'WNO',
                                                                                                                                                                                                                                                                                                                     'WSE') THEN T555160.INSTITUTION_FULL_NAME
                                                                                                                                                                                                                                                           ELSE ''
                                                                                                                                                                                                                                                       END
                                                                                                                                                                                                                                              END
    ORDER BY c1,
             c2,
             c3,
             c4,
             c5,
             c6,
             c7,
             c8,
             c9,
             c10,
             c11,
             c12,
             c13,
             c14,
             c15,
             c16
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.83       0.82          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        1      0.77       5.33      11613      11625          0           0
    total        3      1.60       6.16      11613      11625          0           0
    Misses in library cache during parse: 1
    Optimizer mode: ALL_ROWS
    Parsing user id: 52 
    Rows     Row Source Operation
          0  SORT GROUP BY (cr=11625 pr=11613 pw=0 time=5339293 us)
          0   VIEW  (cr=11625 pr=11613 pw=0 time=5339234 us)
          0    HASH GROUP BY (cr=11625 pr=11613 pw=0 time=5339226 us)
          0     HASH JOIN  (cr=11625 pr=11613 pw=0 time=5339089 us)
          0      NESTED LOOPS  (cr=11625 pr=11613 pw=0 time=5337767 us)
          0       HASH JOIN  (cr=11625 pr=11613 pw=0 time=5337762 us)
          0        NESTED LOOPS  (cr=11625 pr=11613 pw=0 time=5337565 us)
          0         HASH JOIN  (cr=11625 pr=11613 pw=0 time=5337561 us)
          0          HASH JOIN  (cr=11625 pr=11613 pw=0 time=5337376 us)
          0           HASH JOIN  (cr=11625 pr=11613 pw=0 time=5337192 us)
          0            TABLE ACCESS FULL WC_WRK_SPEC_F (cr=11625 pr=11613 pw=0 time=5336958 us)
          0            NESTED LOOPS  (cr=0 pr=0 pw=0 time=0 us)
          0             NESTED LOOPS  (cr=0 pr=0 pw=0 time=0 us)
          0              HASH JOIN  (cr=0 pr=0 pw=0 time=0 us)
          0               TABLE ACCESS BY INDEX ROWID W_PERSON_D (cr=0 pr=0 pw=0 time=0 us)
          0                BITMAP CONVERSION TO ROWIDS (cr=0 pr=0 pw=0 time=0 us)
          0                 BITMAP AND  (cr=0 pr=0 pw=0 time=0 us)
          0                  BITMAP OR  (cr=0 pr=0 pw=0 time=0 us)
          0                   BITMAP INDEX SINGLE VALUE IXDIM_W_PERSON_D_F27 (cr=0 pr=0 pw=0 time=0 us)(object id 348198)
          0                   BITMAP INDEX SINGLE VALUE IXDIM_W_PERSON_D_F27 (cr=0 pr=0 pw=0 time=0 us)(object id 348198)
          0                   BITMAP INDEX SINGLE VALUE IXDIM_W_PERSON_D_F27 (cr=0 pr=0 pw=0 time=0 us)(object id 348198)
          0                   BITMAP INDEX SINGLE VALUE IXDIM_W_PERSON_D_F27 (cr=0 pr=0 pw=0 time=0 us)(object id 348198)
          0                   BITMAP INDEX SINGLE VALUE IXDIM_W_PERSON_D_F27 (cr=0 pr=0 pw=0 time=0 us)(object id 348198)
          0                  BITMAP INDEX SINGLE VALUE W_PERSON_D_M14 (cr=0 pr=0 pw=0 time=0 us)(object id 250911)
          0               NESTED LOOPS  (cr=0 pr=0 pw=0 time=0 us)
          0                TABLE ACCESS FULL WC_WRK_SPEC_F (cr=0 pr=0 pw=0 time=0 us)
          0                BITMAP CONVERSION TO ROWIDS (cr=0 pr=0 pw=0 time=0 us)
          0                 BITMAP INDEX SINGLE VALUE W_PER_RANK_F_F7 (cr=0 pr=0 pw=0 time=0 us)(object id 642169)
          0              TABLE ACCESS BY INDEX ROWID W_PERSON_D (cr=0 pr=0 pw=0 time=0 us)
          0               INDEX RANGE SCAN W_PERSON_D_U1 (cr=0 pr=0 pw=0 time=0 us)(object id 250907)
          0             BITMAP CONVERSION TO ROWIDS (cr=0 pr=0 pw=0 time=0 us)
          0              BITMAP INDEX SINGLE VALUE W_PER_RANK_F_F7 (cr=0 pr=0 pw=0 time=0 us)(object id 642169)
          0           TABLE ACCESS FULL WC_CUST_INST_F (cr=0 pr=0 pw=0 time=0 us)
          0          TABLE ACCESS FULL WC_CUST_INST_F (cr=0 pr=0 pw=0 time=0 us)
          0         INDEX UNIQUE SCAN W_PERSON_D_P1 (cr=0 pr=0 pw=0 time=0 us)(object id 250906)
          0        TABLE ACCESS BY INDEX ROWID WC_CUST_ADDR_D (cr=0 pr=0 pw=0 time=0 us)
          0         BITMAP CONVERSION TO ROWIDS (cr=0 pr=0 pw=0 time=0 us)
          0          BITMAP AND  (cr=0 pr=0 pw=0 time=0 us)
          0           BITMAP INDEX SINGLE VALUE IXDIM_WC_CUST_ADDR_D_F07 (cr=0 pr=0 pw=0 time=0 us)(object id 251281)
          0           BITMAP INDEX SINGLE VALUE IXDIM_WC_CUST_ADDR_D_F06 (cr=0 pr=0 pw=0 time=0 us)(object id 251285)
          0           BITMAP INDEX SINGLE VALUE IXDIM_WC_CUST_ADDR_D_F05 (cr=0 pr=0 pw=0 time=0 us)(object id 251287)
          0       TABLE ACCESS BY INDEX ROWID W_PERSON_D (cr=0 pr=0 pw=0 time=0 us)
          0        INDEX UNIQUE SCAN W_PERSON_D_P1 (cr=0 pr=0 pw=0 time=0 us)(object id 250906)
          0      VIEW  index$_join$_003 (cr=0 pr=0 pw=0 time=0 us)
          0       HASH JOIN  (cr=0 pr=0 pw=0 time=0 us)
          0        HASH JOIN  (cr=0 pr=0 pw=0 time=0 us)
          0         BITMAP CONVERSION TO ROWIDS (cr=0 pr=0 pw=0 time=0 us)
          0          BITMAP INDEX SINGLE VALUE IXDIM_WC_CUST_ADDR_D_F07 (cr=0 pr=0 pw=0 time=0 us)(object id 251281)
          0         BITMAP CONVERSION TO ROWIDS (cr=0 pr=0 pw=0 time=0 us)
          0          BITMAP INDEX SINGLE VALUE IXDIM_WC_CUST_ADDR_D_F04 (cr=0 pr=0 pw=0 time=0 us)(object id 251288)
          0        BITMAP CONVERSION TO ROWIDS (cr=0 pr=0 pw=0 time=0 us)
          0         BITMAP INDEX FULL SCAN IXDIM_WC_CUST_ADDR_D_F02 (cr=0 pr=0 pw=0 time=0 us)(object id 251276)
    OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        1      0.83       0.82          0          0          0           0
    Execute      1      0.00       0.00          0          0          0           0
    Fetch        1      0.77       5.33      11613      11625          0           0
    total        3      1.60       6.16      11613      11625          0           0
    Misses in library cache during parse: 1
    OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse       11      0.00       0.00          0          0          0           0
    Execute    113      0.02       0.03          0          0          0           0
    Fetch      253      0.01       0.08         15        411          0         999
    total      377      0.05       0.13         15        411          0         999
    Misses in library cache during parse: 8
    Misses in library cache during execute: 8
        1  user  SQL statements in session.
      112  internal SQL statements in session.
      113  SQL statements in session.
    Trace file: p002_ora_32614_MY_TEST_SESSION02.trc
    Trace file compatibility: 10.01.00
    Sort options: prsela  exeela  fchela 
           0  session in tracefile.
           1  user  SQL statements in trace file.
         112  internal SQL statements in trace file.
         113  SQL statements in trace file.
          12  unique SQL statements in trace file.
        1292  lines in trace file.
          12  elapsed seconds in trace file.

  • Query running for a long time

    Can some help me please below query has been running for a long time I am unable to fix it
    SELECT lmt.tab, pr.tab6, pr.tab7,
               pr.activity_id, pr.resource_id, lmt.tab2,
                lmt.tab5, pr.txn_lmt_trans_id, pr.analysis_type,
                pr.resource_type, pr.resource_category, pr.resource_sub_cat,
                pr.trans_dt, cd.start_dt, cd.end_dt, pr.currency_cd, pr.rt_type,
                pr.resource_quantity, pr.resource_amount, pr.foreign_amount,
                pr.foreign_currency, 2, 'S', pr.resource_id_from,
                hdr.contract_sign_dt, cd.retainage_id, cd.discount_id, 0, 0,
           cd.limit_bi_cntrl_flg, hdr.ca_rqst_src, 0, hdr.currency_cd,
           hdr.rt_type, 0, 0, pr.bi_distrib_status, pr.gl_distrib_status
         FROM ps_c1 lmt, ps_c2 hdr,  ps_c3 cd,
                ps_c4 pr
             WHERE lmt.tab = 836626
               AND hdr.tab2 = lmt.tab2
               AND cd.tab2 = lmt.tab2
               AND cd.tab5 = lmt.tab5
               AND cd.pricing_structure = 'RATE'
               AND pr.tab6 = cd.tab6_pc
               AND pr.tab2 = cd.tab2
               AND pr.tab5 = cd.tab5
               AND (pr.analysis_type IN ('BIL', 'OLT', 'PMR', 'ROL')
               AND pr.bi_distrib_status <> 'I'
               OR  pr.analysis_type = 'REV'
               AND pr.gl_distrib_status IN ('C', 'N'))
               AND NOT pr.ca_fee_status IN ('2', '3', '4', '5')
               AND 0 >= (SELECT count(*)
                        FROM ps_ca_lmt4_tao4 lmt4
                        WHERE lmt4.tab = 836626
                          AND lmt4.tab6 = pr.tab6
                          AND lmt4.tab7 = pr.tab7
                          AND lmt4.activity_id = pr.activity_id
                          AND lmt4.resource_id = pr.resource_id))
    Below is the execution plan
    ID    PID    Operation    Name    Rows    Bytes    Cost    CPU Cost    IO Cost    Temp space    IN-OUT    PQ Dist    PStart    PStop
    0        SELECT STATEMENT        2     598     99885     2G    99823                          
    1    0      NESTED LOOPS                                                       
    2    1        NESTED LOOPS        1     299     99885     2G    99823                          
    3    2          NESTED LOOPS        1     264     99883     2G    99821                          
    4    3            NESTED LOOPS        3     273     8     114779     8                          
    5    4              INDEX RANGE SCAN     ps_c1    6     138     1     8321     1                          
    6    4              TABLE ACCESS BY INDEX ROWID    ps_c3    1     68     2     17743     2                          
    7    6                INDEX UNIQUE SCAN    ps_c3    1          1     9021     1                          
    8    3            TABLE ACCESS BY INDEX ROWID    ps_c4    1     173     33292     671M    33271                          
    9    8              INDEX RANGE SCAN    ps_c4    55808          10139     320M    10129                          
    10    9                INDEX UNIQUE SCAN    PS_CA_LMT4_TAO4    1     46     0     1050     0                          
    11    2          INDEX UNIQUE SCAN    ps_c2    1          1     9021     1                          
    12    1        TABLE ACCESS BY INDEX ROWID    ps_c2    1     35     2     17413     2
    Can someone please help its really kind of urgent

    check the cardinalities: the CBO expects to access only a few rows and uses a lot of NL joins - and that's probably not the best solution. You could start with a llok at the cardinality of step 5, the index range scan on ps_c1. The CBO expects only 6 rows - is this a good guess? Of course you could just create a plan with rowsource statistics (gather_plan_statistics hint) and compare E- and A-rows.

  • How to do running totals in a query

    Hi Guys, I have following data sample and desired output and I need a running total or subtotals on each row for two quntity fields on same row.
    Current data:
    ID - ---Day----- Qty1 Qty2
    Abc 04/01/2009 100 50
    abc 04/02/2009 70 20
    def 04/01/2009 10 30...
    Desired outPut:
    ID - ---Day----- Qty1 Subtoal Qty1 Qty2 SubtotalQty2
    Abc 04/01/2009 100 100 50 50
    abc 04/02/2009 70 170 20 70
    def 04/01/2009 10 180 30 30

    Hi,
    There's one thing in your example, regarding the last line:
    def 04/01/2009 10 180 30 30I assumed you meant:
    def 04/01/2009 10 180 30 100 <<== running total should be 100Am I correct?
    Then try:
    SQL> with t as (
      2  select  'Abc' col1, to_date('04/01/2009', 'dd/mm/yyyy') col2, 100 col3, 50 col4 from dual union all
      3  select'abc', to_date('04/01/2009', 'dd/mm/yyyy'), 70, 20 from dual union all
      4  select 'def', to_date('04/01/2009', 'dd/mm/yyyy'), 10, 30 from dual
      5  ) --Actual query starts here:
      6  select col1
      7  ,      col2
      8  ,      col3
      9  ,      sum(col3) over (order by rownum) rt_col3
    10  ,      col4
    11  ,      sum(col4) over (order by rownum) rt_col4
    12  from t;
    COL COL2             COL3    RT_COL3       COL4    RT_COL4
    Abc 04-01-2009        100        100         50         50
    abc 04-01-2009         70        170         20         70
    def 04-01-2009         10        180         30        100Edited by: hoek on Jun 24, 2009 5:19 PM added question

  • Need modify Running total query

    CREATE TABLE DET
    (DDT DATE,
    DNO NUMBER,
    DCODE VARCHAR2(6),
    DAMOUNT NUMBER,
    DINST NUMBER);
    INSERT INTO DET VALUES (TO_DATE('01012010','DD/MM/YYYY'),7,1946,19674.5,NULL);
    INSERT INTO DET VALUES (TO_DATE('08012010','DD/MM/YYYY'),8,1574,4800,NULL);
    INSERT INTO DET VALUES (TO_DATE('08012010','DD/MM/YYYY'),8,1574,50000,208.333);
    INSERT INTO DET VALUES (TO_DATE('08012010','DD/MM/YYYY'),9,1835,31600,208.333);
    INSERT INTO DET VALUES (TO_DATE('08012010','DD/MM/YYYY'),6,1924,50000,232.558);
    INSERT INTO DET VALUES (TO_DATE('02022010','DD/MM/YYYY'),9,1835,5000,NULL);
    INSERT INTO DET VALUES (TO_DATE('02022010','DD/MM/YYYY'),9,1835,31600,131.667);
    INSERT INTO DET VALUES (TO_DATE('05022010','DD/MM/YYYY'),8,1574,50000,208.333);
    INSERT INTO DET VALUES (TO_DATE('10022010','DD/MM/YYYY'),6,1924,50000,232.558);
    INSERT INTO DET VALUES (TO_DATE('01032010','DD/MM/YYYY'),8,1574,50000,208.333);
    INSERT INTO DET VALUES (TO_DATE('01032010','DD/MM/YYYY'),6,1924,50000,232.558);
    i have this query ;
    SCOTT@orcl>select ddt,
      2  dno,
      3  dcode,
      4  newdamount damount,
      5  dinst,
      6  newdamount-nvl(dinst,0) run_tot
      7  from(
      8  select ddt,
      9  dno,
    10  dcode,
    11  damount,
    12  dinst,
    13  decode(nvl(to_number(to_char(ddt,'MM'))-1,0),0,damount,
    14  damount-nvl(dinst,0)*(to_number(to_char(ddt,'MM'))-1)) newdamount
    15  from det
    16  order by ddt)
    17  where dcode = '1835'
    18  order by ddt,dno,damount,dinst nulls first;
    it returns this result;
    DDT               DNO DCODE     DAMOUNT      DINST    RUN_TOT
    08/01/2010          9 1835        31600    208.333  31391.667
    02/02/2010          9 1835         5000                  5000
    02/02/2010          9 1835    31468.333    131.667  31336.666
    The results last row should appear like this;
    02/02/2010          9 1835    31391.667    131.667  31260 DAMOUNT for the row 02/02/2010 (where DINST > 0 ) should be 31391.667,(closing RUN_TOT of january)
    and NOT 31468.333.
    The above query logic could be enitrely wrong, because the criteria laid assuming that the instalment remains same every month.
    Please someone who is well-verse with Lag or Sum ..Over analytic function, correct my code.
    TY.

    user613563 wrote:
    Your query entirely missed one row with DAMOUNT 5000.
    i expect the result to be;
    DDT               DNO DCODE     DAMOUNT      DINST    RUN_TOT
    08/01/2010          9 1835        31600    208.333  31391.667
    02/02/2010          9 1835         5000                  5000
    02/02/2010          9 1835    31391.667    131.667  31260
    Well, i for one am not following the logic here, but this will give you the results you've posted (why doesn't the row with damount = 5000 factor in to the 'running total' in any way shape or form?)
    select
      ddt, dno, dcode, damount, dinst, resolved
    from
      select
        ddt, dno, dcode, damount, dinst,
        case when lag(trunc(ddt, 'MM')) over (order by ddt asc) != trunc(ddt, 'MM')
        then
          lag(damount-dinst) over (order by ddt asc) - dinst
        else
          damount-dinst
        end as resolved
      from det
      where dcode = '1835'
      and dinst is not null
        union all
      select
        ddt, dno, dcode, damount, dinst,
        damount as resolved
      from det
      where dcode = '1835'
      and dinst is null
      order by ddt asc, dinst nulls first
    )Edited by: Tubby on Jan 11, 2010 2:47 PM
    fixed the not equals sign since the forum eats less than / greater than symbols

  • When I run the same query for the second time it's faster, I want to reset this behavior

    I am running a query in oracle 11g select A from B where C = ':D' B has millions of records.
    The first time i run it it takes about 30 seconds, the second time i run the query it takes about 1 second.
    Obviously it's caching something and i want it to stop that, each time i run the query i want it to take 30s - just like it was running for the first time.
    The reason I want to reset this is because for testing purposes I want to measure this query at the very first time.
    Please help.
    Thanks & Best Regards,
    Dark

    user9359353 wrote:
    I am running a query in oracle 11g select A from B where C = ':D' B has millions of records.
    The first time i run it it takes about 30 seconds, the second time i run the query it takes about 1 second.
    Obviously it's caching something and i want it to stop that, each time i run the query i want it to take 30s - just like it was running for the first time.
    The reason I want to reset this is because for testing purposes I want to measure this query at the very first time.
    Please help.
    Thanks & Best Regards,
    Dark
    No - you do NOT want to do that. Not if you want to get results that really represent how that query will work in reality.
    See these two AskTom blogs where he discusses the reasons for NOT doing this in detail.
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:7413988573867
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:311990400346061304

  • Query with running Total

    Hello,
    I have a insert from query:
         insert into tablexxxx
         SELECT ALL      G.c1, G.c2,
                         CASE
                         WHEN G.colt1  NOT IN ( 'A', 'B', 'C') THEN G.DATA1
                         WHEN G.colt1      IN ( 'B', 'C')      THEN &DATASF
                         WHEN G.colt1      =   'A'             THEN &DATASI
                         END colt3,
                     CASE G.colt2 WHEN 'D' THEN  G.IMP1  else 0   END    IMPD,
                    CASE G.colt2 WHEN 'A' THEN  G.IMP1  else 0   END    IMPA,
                         CASE
                         WHEN G.colt1  NOT IN ( 'B', 'C')
                         THEN (&VALU1 -1)
                         ELSE COLX
                         END      COLX,
         FROM table1 G, table2 S, table3 C
         WHERE  G.colt4    = S.colt4      
         AND (     (G.colt1  = 'M' AND  G.DATA1 BETWEEN &DATA_IP AND &DATA_FP AND G.TCOL6 = EXTRACT(YEAR FROM &DATA_IP) - 1 )
                OR (G.colt1 <> 'M' AND  G.DATA1 BETWEEN &DATA_IP AND &DATA_FP)
                OR (G.colt1  = 'A'  AND (&DATASI BETWEEN &DATA_IP AND &DATA_FP AND G.DATA1 = &DATAIE) )
                OR (G.colt1  = 'B'  AND (&DATASF BETWEEN &DATA_IP AND &DATA_FP AND G.DATA1 = &DATAFE) )
                OR (G.colt1  = 'C'  AND (&DATASF BETWEEN &DATA_IP AND &DATA_FP AND G.DATA1 = &DATAFE) )  )
         AND  G.colt5  = C.colt5(+)
         ORDER BY 3,4,5 ; 
         NOW I HAVE TO INSERT in previous query 2 RUNNING TOTALE e.g:
               sum(CASE G.colt2 WHEN 'D' THEN  G.IMP1 + &myvalue1  else 0   END    IMPD) over (order by CASE
                         WHEN G.colt1  NOT IN ( 'A', 'B', 'C') THEN G.DATA1
                         WHEN G.colt1      IN ( 'B', 'C')      THEN &DATASF
                         WHEN G.colt1      =   'A'             THEN &DATASI
                         END) running_sum,
             sum(CASE G.colt2 WHEN 'A' THEN  G.IMP1 + &myvalue2  else 0   END    IMPA) over ((order by CASE
                         WHEN G.colt1  NOT IN ( 'A', 'B', 'C') THEN G.DATA1
                         WHEN G.colt1      IN ( 'B', 'C')      THEN &DATASF
                         WHEN G.colt1      =   'A'             THEN &DATASI
                         END) running_sum1    Could anyone help me to write it? ( maybe is better write it with SELECT-UNION?)
    Thanks in advance

    Hi IceMan,
    I think about having resolved in this way, I don't sincerely believe that is the optimal solution,
    but of sure it is that simpler,anyway, one opinion of yours would be very pleasant:
    1) I have made an Insert-Select with the 2 runnings total = 0
    INSERT INTO  TABLEX
    SELECT ALL G.c1, G.D2,
    CASE
      WHEN G.colt1 NOT IN ( 'A', 'B', 'C') THEN G.DATA1
      WHEN G.colt1 IN ( 'B', 'C') THEN &DATASF
      WHEN G.colt1 = 'A' THEN &DATASI
    END D1,
    CASE G.colt2 WHEN 'D' THEN G.IMP1 else 0 END IMPD,
    CASE G.colt2 WHEN 'A' THEN G.IMP1 else 0 END IMPA,
    CASE
    WHEN G.colt1 NOT IN ( 'B', 'C')
    THEN (&VALU1 -1)
    ELSE COLX
    END COLX, 0 RUNIMP_D, 0 RUNIMP_A
    FROM table1 G, table2 S, table3 C
    WHERE ........
    ORDER BY 3,4,5 ;2) in the populated table(step 1) I have made an UPDATE-SELECT getting the 2 runnings total
    UPDATE TABLEX A SET  (RUNIMP_D, RUNIMP_A ) =(
    SELECT  --G.D1, G.D2, G.IMPD,G.IMPA,
             SUM(G.IMPD) OVER (ORDER BY G.D1, G.D2,G.IMPD) RS1  ,
           SUM(G.IMPA) OVER (ORDER BY  G.D1, G.D2,G.IMPA) RS2
      FROM TABLEX G
    WHERE  G.ROWID = A.ROWID     
    --ORDER BY 1,2
    where G.D1 is that I get from
    CASE
    WHEN G.colt1 NOT IN ( 'A', 'B', 'C') THEN G.DATA1
    WHEN G.colt1 IN ( 'B', 'C') THEN &DATASF
    WHEN G.colt1 = 'A' THEN &DATASI
    END D1
    and
    G.IMPD is:
    CASE G.colt2 WHEN 'D' THEN G.IMP1 else 0 END IMPDThanks again for your reply

  • Running Total & Distinct Count Query

    Crystal 10.0.0.533 CR Professional
    Hope you can assist.
    I have a report listing deals signed, each deal has a corresponding category i.e. industry type, Accountant, Chiropractor, Financial Services, etc.
    I am undertaking a Distinct Count on these categories so that in the group footer it is showing number of different categories signed in a period (the report is grouped into different date periods). This is working OK.
    However, I need to EXCLUDE the category from this distinct count if the deal value is zero.
    I have tried using a formula to show a blank field if the value is zero, however it appears to be distinctly counting the blank as a category?
    Any assistance would be much appreciated.
    Tracy

    Deffinetly it will be less than what you expect for grand total. It is because when you are calculating distinct count for each group suppose
    Group A has categories A,B,C,D-->distinct count (4)
    Group B has C,D,E,F-->distinct count (4)
    but while calculating grand total then the distinct count will be
    A,B,C,D,E,F --> grand total (6) but not (8).
    In this case you need to use mannual running total like this
    whileprintingrecords;
    numbervar i;
    i:=i+{running total};
    place this in group footer and create another fomula like this
    whileprintingrecords;
    numbervar i;
    place this in report footer to get the correct grand total.
    Regards,
    Raghavendra

  • SQL with connect by prior running for a long time

    Hi,
    We are using Oracle 10g. Below is a cursor sql which is having performance issues. The pAccountid is being passed from the output of a different cursor. But this cursor sql is running for a long time. Could you please help me in tuning this sql. I believe the subquery with connect by prior is causing the trouble.
    The TRXNS is a huge table which is not partitioned. The query is forced to use the index on the accountid of the TRXNS table.
    The accountlink table has 20,000 records and the TRXNStrack table has 10,000 records in total.
    This sql executes for 200,000 pAccountids and runs for more than 8 hours.
    SELECT /*+ INDEX(T TRXNS_ACCOUNTID_NIDX) */ AL.FROMACCOUNTID oldaccountid ,
                                    A.ACCOUNTNUM  oldaccountnum,
                                   T.TRXNSID,
                                   T.TRXNSTYPEID,
                                   T.DESCRIPTION ,
                                   T.postdt,
                                   T.TRXNSAMT
                        FROM
                        ACCOUNTLINK AL,
                        TRXNS T,
                        ACCOUNT A
                       WHERE AL.TOACCOUNTID IN
                                                             (SELECT TOACCOUNTID FROM ACCOUNTLINK START WITH TOACCOUNTID = pAccountid
                                                                                                                         CONNECT BY PRIOR FROMACCOUNTID  = TOACCOUNTID)
                            AND AL.FROMACCOUNTID = T.ACCOUNTID
                            AND A.ACCOUNTID = AL.FROMACCOUNTID
    AND NOT EXISTS (select 1 from TRXNStrack trck where trck.TRXNSid = t.TRXNSid AND TRXNSTrackReasonid = 1)
                            AND T.postdt > A.CLOSEDATE
                            AND T.postdt >= sysdate-2
                            AND T.postdt <= sysdate;
    Create script for trxn table:
    CREATE TABLE SP.TRXNS
      TRXNSID      NUMBER(15) CONSTRAINT "BIN$rpIQEeyLDfbgRAAUT4DEnQ==$0" NOT NULL,
      ACCOUNTID    NUMBER(15) CONSTRAINT "BIN$rpIQEeyMDfbgRAAUT4DEnQ==$0" NOT NULL,
      STATEMENTID  NUMBER(15),
      TRXNSTYPEID  NUMBER(15),
      DESCRIPTION  VARCHAR2(80 BYTE),
      postdt     DATE,
      TRXNSAMT     NUMBER(12,2),
      TRXNSREQID   NUMBER(15),
      LASTUPDATE   DATE,
      SOURCEID     NUMBER(15),
      HIDE         VARCHAR2(1 BYTE)
    TABLESPACE SO_TRXN_DATA
    RESULT_CACHE (MODE DEFAULT)
    PCTUSED    40
    PCTFREE    10
    INITRXNS   2
    MAXTRXNS   255
    STORAGE    (
                INITIAL          50M
                NEXT             1M
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                FREELISTS        8
                FREELIST GROUPS  1
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
    LOGGING
    NOCOMPRESS
    NOCACHE
    NOPARALLEL
    MONITORING;
    CREATE INDEX SP.TRXNS_ACCOUNTID_NIDX ON SP.TRXNS
    (ACCOUNTID, postdt)
    LOGGING
    TABLESPACE SO_TRXN_INDEX
    PCTFREE    10
    INITRXNS   2
    MAXTRXNS   255
    STORAGE    (
                INITIAL          64K
                NEXT             1M
                MINEXTENTS       1
                MAXEXTENTS       UNLIMITED
                PCTINCREASE      0
                FREELISTS        1
                FREELIST GROUPS  1
                BUFFER_POOL      DEFAULT
                FLASH_CACHE      DEFAULT
                CELL_FLASH_CACHE DEFAULT
    NOPARALLEL;
    below is the executing plan for this sql taken from toad :
    PLAN_ID
    TIMESTAMP
    OPERATION
    OPTIONS
    OBJECT_OWNER
    OBJECT_NAME
    OBJECT_ALIAS
    OBJECT_INSTANCE
    OBJECT_TYPE
    OPTIMIZER
    SEARCH_COLUMNS
    ID
    PARENT_ID
    DEPTH
    POSITION
    COST
    CARDINALITY
    BYTES
    CPU_COST
    IO_COST
    TEMP_SPACE
    ACCESS_PREDICATES
    FILTER_PREDICATES
    PROJECTION
    TIME
    QBLOCK_NAME
    1121
    9/10/2013 3:30
    FILTER
    1
    0
    1
    1
    NOT EXISTS (SELECT 0 FROM "TRXNSTRACK" "TRCK" WHERE "TRXNSTRACKREASONID"=1 AND "TRCK"."TRXNSID"=:B1)
    AL."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A"."ACCOUNTNUM"[VARCHAR2,19]
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    FILTER
    2
    1
    2
    1
    SYSDATE@!-2<=SYSDATE@!
    AL."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A"."ACCOUNTNUM"[VARCHAR2,19]
    1121
    9/10/2013 3:30
    NESTED LOOPS
    3
    2
    3
    1
    (#keys=0) "AL"."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A"."ACCOUNTNUM"[VARCHAR2,19]
    1121
    9/10/2013 3:30
    NESTED LOOPS
    4
    3
    4
    1
    5
    1
    119
    3989858
    4
    (#keys=0) "AL"."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22], "A".ROWID[ROWID,10]
    1
    1121
    9/10/2013 3:30
    NESTED LOOPS
    5
    4
    5
    1
    4
    1
    90
    3989690
    3
    (#keys=0) "AL"."FROMACCOUNTID"[NUMBER,22], "T"."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22]
    1
    1121
    9/10/2013 3:30
    HASH JOIN
    SEMI
    6
    5
    6
    1
    3
    2
    54
    3989094
    2
    AL."TOACCOUNTID"="TOACCOUNTID"
    (#keys=1) "AL"."FROMACCOUNTID"[NUMBER,22]
    1
    1121
    9/10/2013 3:30
    INDEX
    FULL SCAN
    SP
    ACCOUNTLINK_AK1
    AL@SEL$1
    INDEX (UNIQUE)
    ANALYZED
    7
    6
    7
    1
    1
    18
    252
    107
    1
    AL."FROMACCOUNTID"[NUMBER,22], "AL"."TOACCOUNTID"[NUMBER,22]
    1
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    VIEW
    SYS
    VW_NSO_1
    VW_NSO_1@SEL$5DA710D3
    11
    VIEW
    8
    6
    7
    2
    2
    18
    234
    107
    1
    TOACCOUNTID[NUMBER,22]
    1
    SEL$683B0107
    1121
    9/10/2013 3:30
    CONNECT BY
    NO FILTERING WITH START-WITH
    9
    8
    8
    1
    TOACCOUNTID=PRIOR "FROMACCOUNTID"
    TOACCOUNTID=56354162
    TOACCOUNTID[NUMBER,22], "FROMACCOUNTID"[NUMBER,22], PRIOR NULL[22], LEVEL[4]
    SEL$683B0107
    1121
    9/10/2013 3:30
    INDEX
    FULL SCAN
    SP
    ACCOUNTLINK_AK1
    ACCOUNTLINK@SEL$3
    INDEX (UNIQUE)
    ANALYZED
    10
    9
    9
    1
    1
    18
    252
    107
    1
    ACCOUNTLINK.ROWID[ROWID,10], "FROMACCOUNTID"[NUMBER,22], "TOACCOUNTID"[NUMBER,22]
    1
    SEL$3
    1121
    9/10/2013 3:30
    TABLE ACCESS
    BY INDEX ROWID
    SP
    TRXNS
    T@SEL$1
    2
    TABLE
    ANALYZED
    11
    5
    6
    2
    1
    1
    63
    298
    1
    T."TRXNSID"[NUMBER,22], "T"."TRXNSTYPEID"[NUMBER,22], "T"."DESCRIPTION"[VARCHAR2,80], "T"."POSTDT"[DATE,7], "T"."TRXNSAMT"[NUMBER,22]
    1
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    INDEX
    RANGE SCAN
    SP
    TRXNS_ACCOUNTID_NIDX
    T@SEL$1
    INDEX
    ANALYZED
    2
    12
    11
    7
    1
    1
    1
    224
    1
    AL."FROMACCOUNTID"="T"."ACCOUNTID" AND "T"."POSTDT">=SYSDATE@!-2 AND "T"."POSTDT"<=SYSDATE@!
    T.ROWID[ROWID,10], "T"."POSTDT"[DATE,7]
    1
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    INDEX
    UNIQUE SCAN
    SP
    ACCOUNT_PK
    A@SEL$1
    INDEX (UNIQUE)
    ANALYZED
    1
    13
    4
    5
    2
    1
    1
    90
    1
    A."ACCOUNTID"="AL"."FROMACCOUNTID"
    A.ROWID[ROWID,10]
    1
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    TABLE ACCESS
    BY INDEX ROWID
    SP
    ACCOUNT
    A@SEL$1
    3
    TABLE
    ANALYZED
    14
    3
    4
    2
    1
    1
    29
    168
    1
    A."CLOSEDATE"<SYSDATE@! AND "T"."POSTDT">"A"."CLOSEDATE"
    A."ACCOUNTNUM"[VARCHAR2,19]
    1
    SEL$5DA710D3
    1121
    9/10/2013 3:30
    INDEX
    RANGE SCAN
    SP
    TRXNSTRACK_TRXNSID_NIDX
    TRCK@SEL$6
    INDEX
    ANALYZED
    2
    15
    1
    2
    2
    1
    1
    10
    73
    1
    TRCK."TRXNSID"=:B1 AND "TRXNSTRACKREASONID"=1
    TRCK."TRXNSID"[NUMBER,22], "TRXNSTRACKREASONID"[NUMBER,22]
    1
    SEL$6
    Please help me in debugging this thanks!

    Hi,
    Thanks for your thought on this subject. Below is the trace info that I got from the DBA
    SQL ID: d0x879qx2zgtz Plan Hash: 4036333519
    SELECT /*+ INDEX(T TRXNS_ACCOUNTID_NIDX) */ AL.FROMACCOUNTID OLDACCOUNTID ,
      A.ACCOUNTNUM OLDACCOUNTNUM, T.TRXNSID, T.TRXNSTYPEID, T.DESCRIPTION ,
      T.POSTDT, T.TRXNSAMT
    FROM
    ACCOUNTLINK AL, TRXNS T, ACCOUNT A WHERE AL.TOACCOUNTID IN (SELECT
      TOACCOUNTID FROM ACCOUNTLINK START WITH TOACCOUNTID = :B3 CONNECT BY PRIOR
      FROMACCOUNTID = TOACCOUNTID) AND AL.FROMACCOUNTID = T.ACCOUNTID AND
      A.ACCOUNTID = AL.FROMACCOUNTID AND NOT EXISTS (SELECT 1 FROM TRXNSTRACK
      TRCK WHERE TRCK.TRXNSID = T.TRXNSID AND TRXNSTRACKREASONID = :B4 ) AND
      T.POSTDT > A.CLOSEDATE AND T.POSTDT >= :B2 AND T.POSTDT <= :B1
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        0      0.00       0.00          0          0          0           0
    Execute  17160      2.10       1.87          0          0          0           0
    Fetch    17160   7354.61    7390.86     169408    5569856  883366791           0
    total    34320   7356.71    7392.74     169408    5569856  883366791           0
    Misses in library cache during parse: 0
    Optimizer mode: CHOOSE
    Parsing user id: 38     (recursive depth: 1)
    SQL ID: gs89hpavb4cts Plan Hash: 3415795327
    SELECT A.ACCOUNTID, C.MEMBERID, A.PROGRAMID, A.ACCOUNTNUM
    FROM
    CUSTOMER C, CUSTOMERACCOUNT CA, ACCOUNT A, PROGRAMPARAMVALUE PPV,
      BATCHPROCESSPROGRAM BP WHERE A.PROGRAMID = BP.PROGRAMID AND A.PROGRAMID =
      PPV.PROGRAMID AND A.ACCOUNTID = CA.ACCOUNTID AND CA.PERSONID = C.PERSONID
      AND PPV.PARAMID = :B2 AND PPV.VALUE = 'Y' AND BP.PROCESSID = :B1 AND BP.RUN
      = 'Y' AND A.ACCOUNTTYPEID = 4 AND A.ACCOUNTSTATUSID = 1 AND C.MEMBERID IS
      NOT NULL
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        0      0.00       0.00          0          0          0           0
    Execute      0      0.00       0.00          0          0          0           0
    Fetch      172     13.14     115.34      80826     278650          0       17200
    total      172     13.14     115.34      80826     278650          0       17200
    Misses in library cache during parse: 0
    Parsing user id: 38     (recursive depth: 1)
    OVERALL TOTALS FOR ALL NON-RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        0      0.00       0.00          0          0          0           0
    Execute      0      0.00       0.00          0          0          0           0
    Fetch        0      0.00       0.00          0          0          0           0
    total        0      0.00       0.00          0          0          0           0
    Misses in library cache during parse: 0
    OVERALL TOTALS FOR ALL RECURSIVE STATEMENTS
    call     count       cpu    elapsed       disk      query    current        rows
    Parse        0      0.00       0.00          0          0          0           0
    Execute  17160      2.10       1.87          0          0          0           0
    Fetch    17332   7367.75    7506.21     250234    5848506  883366791       17200
    total    34492   7369.85    7508.09     250234    5848506  883366791       17200
    Misses in library cache during parse: 0
        2  user  SQL statements in session.
        0  internal SQL statements in session.
        2  SQL statements in session.
    Trace file: svoprod_ora_12346.trc
    Trace file compatibility: 11.1.0.7
    Sort options: default
           1  session in tracefile.
           2  user  SQL statements in trace file.
           0  internal SQL statements in trace file.
           2  SQL statements in trace file.
           2  unique SQL statements in trace file.
       66499  lines in trace file.
        7516  elapsed seconds in trace file.

  • Running Total in forms 4.5

    Hi,
    I have a forms 4.5 with a master and detail block.
    The detail block displays 15 record at a time. There is a column in the detail field whose total is to be displayed at the bottom.
    I am using post-query to sum this item and display it at the bottom.
    The solution works fine fine if the detail block has 15 or less records and the running total is displayed correctly.
    But If I query a master records where there are 20 detail records then the total sum at the bottom only show sum of the 15 detail records that are displayed and not 20.
    For that I have to navigate to record 15, key next , 16 and so on then the post-query trigger fires and add the rest of the records and displays the correct total.
    I have tried to change the record buffered/fetched in the detail block query but it does not help.
    Can anyone suggest a solution for this problem in forms 4.5?
    Thanks
    Jakh

    Try Last_record instead of next_record.
    Any other suggestion is that.
    You must have a primary key and foreign key in your master detail form. Then why dont you write a select statement on POST-QUERY:
    You have a master block (master) having columns invoice_id, date, cust_id and have a detailed block (detail) having columns invoice_id (using as foreign key from master table block) and item_id, item_description, quantity, amount
    Write query on POST-QUERY on detail block as
    select sum(amount) into :control_block.total from detail where detail.invoice_id = :master.invoice_id
    or select sum(amount) into :control_block.total from detail where detail.invoice_id = :detail1.invoice_id;
    it should give you the required result. I have used it and its working fine for me.
    Thanks.
    Hassan

  • MDX - Running total

    I'm trying to create a calculated measure as a running total.
    I'm using the following
    SUM({Null:[TIME].[YMD].currentmember},[Measures].[Qty])
    This is working fine except that I would like the measure only to show the running total up to todays date - right now if I am using the above and use my time hierachy together with this it shows me all dates in my time hierachy(which Means all future dates
    which results in the same qty for the future dates)
    How to avoid this ?
    Also is there a way where I could use all of my Time hierachies and Time attributes together with this measure (and not only the one that I have specified like [TIME].[YMD]) so to speak are more dynamic solution ?
    Thanks in advance,

    Hi Hcmj,
    According to your description, you want to calculate running total for all the time attributes, right?
    In Analysis Services, we can use YTD, QTD and MTD to to calculate running total for all the time attributes. We can calculate this measure in query directly or add the calculation to your cube. Please refer to the link below to see the detail information
    about it.
    http://aniruddhathengadi.blogspot.com/2011/03/how-to-calculate-ytd-qtd-mtd-and-wtd.html
    http://www.powerpivotblog.nl/add-time-functions-like-ytd-to-ssas-using-the-add-business-intelligence-wizard/
    Hope this helps.
    Regards,
    Charlie Liao
    TechNet Community Support

  • Running total required but with different timing

    Hi All:
    I am using Oracle9i Enterprise Edition Release 9.2.0.6.0
    I want to write a query to display the statistics report with running total.
    I have written following query that return last 60 minutes activities.
    SELECT se.tserv_id,
    sum(decode(eu.category,'I',1,0)) Imp,
    sum(decode(eu.category,'E',1,0)) Exp,
    count(*) Total
    FROM service_events se, equipment_uses eu
    WHERE se.performed BETWEEN trunc(sysdate-60/1440,'mi')
    AND trunc(sysdate,'mi')
    AND se.tserv_id IN ('EXM','GROUNDED')
    AND eu.gkey = se.equse_gkey
    AND eu.category IN ('I','E')
    GROUP BY se.tserv_id;
    now I want to write a query that shows me the result of above query and running total as well
    But the different between current activity and running total is as follows
    Current Activity = Last 60 minutes activity
    Running Total = Total activities from the 08:00 am till sysdate.
    for example if this query executed on 11:00 am then the current activity values must be from 10:00 am to 11:00 am and the running total values must be from 08:00 am to 11:00 am
    The reason for running total starts at 08:00 am is that the shift starts from 08:00 am to next day 07:59 am.
    Can I write one single query for this?
    Similarly running total values must be start from last day 08:00 am to current date and time when I executed this query from 00:00 to 08:00 (12 midnight to 8am)
    if difficult then plz let me know that how can I find that if the time is from 00 hrs to 08 hrs then start time must be the last date 08:00 am to the current date and time.
    Thanks
    Hassan

    Dear Frnak
    Thanks for your reply and help
    Its really did what I want
    I mean to say that the result format is correct but the value of Total_day column is not correct.
    Following is the sample data from 19-Jun-2008 08:00 AM to 20-JUN-2008 08:00 AM
    TSERV_ID  EQUSE_GKEY     PERFORMED
    ===============================================
    EXM       5188958     19-Jun-2008 8:00:32 AM
    EXM       5185586     19-Jun-2008 9:03:34 AM
    EXM       5181960     19-Jun-2008 9:03:35 AM
    EXM       5173333     19-Jun-2008 9:03:35 AM
    EXM       5188263     19-Jun-2008 9:08:49 AM
    GROUNDED  5174055     19-Jun-2008 9:10:29 AM
    GROUNDED  5152887     19-Jun-2008 9:10:29 AM
    GROUNDED  5187512     19-Jun-2008 9:10:29 AM
    GROUNDED  5196303     19-Jun-2008 9:27:41 AM
    EXM       5196370     19-Jun-2008 9:29:50 AM
    EXM       5172045     19-Jun-2008 10:01:23 AM
    GROUNDED  5196269     19-Jun-2008 11:11:29 AM
    GROUNDED  5200219     19-Jun-2008 11:24:39 AM
    GROUNDED  5185246     19-Jun-2008 12:06:30 PM
    GROUNDED  5190134     19-Jun-2008 12:09:27 PM
    GROUNDED  5190475     19-Jun-2008 12:09:27 PM
    GROUNDED  5156065     19-Jun-2008 12:26:49 PM
    GROUNDED  5132287     19-Jun-2008 1:56:44 PM
    EXM       5189203     19-Jun-2008 2:29:17 PM
    EXM       5180158     19-Jun-2008 3:27:03 PM
    EXM       5149310     19-Jun-2008 3:37:31 PM
    EXM       5196303     19-Jun-2008 3:42:48 PM
    EXM       5192768     19-Jun-2008 4:03:51 PM
    EXM       5174055     19-Jun-2008 4:14:23 PM
    EXM       5200231     19-Jun-2008 4:45:51 PM
    EXM       5157151     19-Jun-2008 4:51:06 PM
    EXM       5159930     19-Jun-2008 5:01:36 PM
    EXM       5185237     19-Jun-2008 5:01:37 PM
    EXM       5195434     19-Jun-2008 5:01:37 PM
    EXM       5152887     19-Jun-2008 5:06:49 PM
    EXM       5196269     19-Jun-2008 5:06:49 PM
    EXM       5187512     19-Jun-2008 5:17:26 PM
    EXM       5190134     19-Jun-2008 5:59:24 PM
    GROUNDED  5101919     19-Jun-2008 6:44:36 PM
    GROUNDED  5101918     19-Jun-2008 6:44:37 PM
    GROUNDED  5101913     19-Jun-2008 6:44:37 PM
    EXM       5185735     19-Jun-2008 6:46:38 PM
    GROUNDED  5195475     19-Jun-2008 6:47:11 PM
    GROUNDED  5195361     19-Jun-2008 6:47:12 PM
    EXM       5132608     19-Jun-2008 7:02:23 PM
    GROUNDED  5195334     19-Jun-2008 7:10:32 PM
    GROUNDED  5190558     19-Jun-2008 7:10:32 PM
    EXM       5183029     19-Jun-2008 7:33:53 PM
    EXM       5183480     19-Jun-2008 7:39:12 PM
    EXM       5189566     19-Jun-2008 8:00:11 PM
    EXM       5181825     19-Jun-2008 8:15:57 PM
    EXM       5183537     19-Jun-2008 8:15:57 PM
    EXM       5180210     19-Jun-2008 9:08:27 PM
    GROUNDED  5193136     19-Jun-2008 9:58:23 PM
    EXM       5171510     19-Jun-2008 10:21:59 PM
    EXM       5185615     19-Jun-2008 10:48:14 PM
    EXM       5184737     19-Jun-2008 10:53:29 PM
    EXM       5195475     19-Jun-2008 11:51:16 PM
    EXM       5195476     20-Jun-2008 12:01:46 AM
    EXM       5190558     20-Jun-2008 12:12:17 AM
    EXM       5196473     20-Jun-2008 12:54:19 AM
    EXM       5183204     20-Jun-2008 1:20:34 AM
    GROUNDED  5192844     20-Jun-2008 1:28:24 AM
    GROUNDED  5174238     20-Jun-2008 1:28:24 AM
    GROUNDED  5193127     20-Jun-2008 1:28:24 AM
    GROUNDED  5185794     20-Jun-2008 1:29:09 AM
    GROUNDED  5192895     20-Jun-2008 3:04:23 AM
    GROUNDED  5193039     20-Jun-2008 3:04:23 AM
    GROUNDED  5195447     20-Jun-2008 3:04:23 AM
    GROUNDED  5190228     20-Jun-2008 3:04:24 AM
    GROUNDED  5179370     20-Jun-2008 3:04:24 AM
    GROUNDED  5190289     20-Jun-2008 3:04:24 AM
    GROUNDED  5174065     20-Jun-2008 3:04:24 AM
    GROUNDED  5192862     20-Jun-2008 5:04:42 AM
    GROUNDED  5199903     20-Jun-2008 5:04:42 AM
    EXM       5178176     20-Jun-2008 6:30:29 AM
    EXM       5185161     20-Jun-2008 6:46:14 AM
    EXM       5180226     20-Jun-2008 6:46:14 AM
    GROUNDED  5202026     20-Jun-2008 7:03:14 AM
    EXM       5101912     20-Jun-2008 7:07:14 AM
    GROUNDED  5176763     20-Jun-2008 7:25:27 AM
    GROUNDED  5185494     20-Jun-2008 7:25:27 AM
    EXM       5185295     20-Jun-2008 7:38:46 AM
    EXM       5173837     20-Jun-2008 7:49:15 AM
    GKEY     CATEGORY
    =================
    5101912     I
    5101913     I
    5101918     I
    5101919     I
    5132287     I
    5132608     I
    5149310     I
    5152887     I
    5156065     I
    5157151     I
    5159930     I
    5171510     I
    5172045     I
    5173333     I
    5173837     I
    5174055     I
    5174065     I
    5174238     I
    5176763     I
    5178176     I
    5179370     I
    5180158     I
    5180210     I
    5180226     I
    5181825     I
    5181960     I
    5183029     I
    5183204     I
    5183480     I
    5183537     I
    5184737     I
    5185161     I
    5185237     I
    5185246     I
    5185295     I
    5185494     I
    5185586     I
    5185615     I
    5185735     I
    5185794     I
    5187512     I
    5188263     I
    5188958     I
    5189203     I
    5189566     I
    5190134     I
    5190228     I
    5190289     I
    5190475     I
    5190558     I
    5192768     I
    5192844     I
    5192862     I
    5192895     I
    5193039     I
    5193127     I
    5193136     I
    5195334     I
    5195361     I
    5195434     I
    5195447     I
    5195475     I
    5195476     I
    5196269     I
    5196303     I
    5196370     I
    5196473     I
    5199903     I
    5200219     E
    5200231     E
    5202026     EAnd I used the same query suggested by you.
    WITH     s     AS(     
    SELECT     se.tserv_id     ,     eu.category     ,
               CASE
                 WHEN     se.performed     BETWEEN     
                                    to_date('&target_dt','DD-MON-RRRR HH24:MI') - (1/24)
                                  AND     to_date('&target_dt','DD-MON-RRRR HH24:MI')
                          THEN     1
              ELSE     0
              END     AS in_hour     ,
            CASE
              WHEN     TRUNC (se.performed - (8/24)) =     TRUNC (to_date('&target_dt','DD-MON-RRRR HH24:MI')
                                                     - (8/24))
                THEN     1
              ELSE     0
              END     AS in_day
    FROM     service_events     se     ,     equipment_uses     eu
    WHERE     eu.gkey          = se.equse_gkey
         AND     eu.category     IN ('I', 'E')
      AND se.tserv_id in ('EXM','GROUNDED')
    SELECT     tserv_id,     
            SUM (DECODE (category, 'I', in_hour))     AS imp_hour,     
            SUM (DECODE (category, 'E', in_hour))     AS exp_hour,
            SUM (in_hour)     AS total_hour,
            SUM (in_day)     AS total_day
    FROM     s
    GROUP BY     tserv_idNow you have the sample data for almost 24 hours
    Let me clear it again
    When I executed this query at 20-JUN-2008 09:00 AM it gives me following result (that is accurate)
    TSERV_ID  IMP_HOUR  EXP_HOUR  TOTAL_HOUR  TOTAL_DAY
    EXM        2         0           2               3
    GROUNDED   1         0           1               8When I executed this query between 20-JUN-2008 00:00 to 20-JUN-2008 08:00
    then system must show the Total_Day value from 19-JUN-2008 08:00 to current date and time. e.g
    I executed this query at 20-JUN-2008 06:00 AM it gives me following result:
    TSERV_ID  IMP_HOUR  EXP_HOUR  TOTAL_HOUR  TOTAL_DAY
    EXM             0     0     0        44
    GROUNDED     2     0     2        35Now here you can see that the value of Total_Day is not as accurate as I required:
    EXM = 44 and GROUNDED = 35
    these values are between 19-JUN-2008 00:00 to 20-JUN-2008 06:00 (that is wrong in my case)
    The values of total_day should be as follows as 8 hours from 00:00 to 07:59 AM fall in the shift of 18-JUN-2008 not in 19-JUN-2008
    From 19-JUN-2008 08:00 to 20-JUN-2008 06:00
    EXM: 38
    GROUNDED: 32
    Plz note that this result is required because in our company the shift starts from 08:00 AM and ends 07:59 AM next day
    I hope you will be more clear now and there is no confusion anymore.
    Plz let me know if you still confused.
    Brgs,

  • Two processes running at the same time in Lookout

    I have installed Lookout 5.0 with 200 I/O Points onto our server computer. The application of motion control is next to the 200 I/O points through OPC PMAC server/client. Now I would like to have a second process in the same server for trouble shooting and testing without stopping the motion control process. However, this second testing process could have also many I/O points through Serial and USB ports. I assume that the total amount of I/O points of both processes will be greater than 200.I prefer to have independent processes for control and testing because access levels. Can I have these two processes running at the same time when needed?

    Hi,
    From your description you are using a third party OPC server for the motion application. You could have a second Lookout process communicating with the same OPC server with no problems, as long as you do not exceed the number of I/O points your license supports.
    Also, the process you are using for testing obviously could not overwrite datamembers (or registers if you will) that would interfere in the overall application, in other words you can test your application as long as you keep the coherency of the test.
    So the answer would be, yes it is possible, however you are still limited to the number of I/O's your license supports... You may even consider upgrade the number of I/O's you have in your license.
    Best Regards
    Andre Oliveira

  • Showing  running totals on AR statatement Report

    Hi All I need assistance in populating the Running totals on AR statement Report  , I am trying to calculate the running total on the RDF using the (sum Over Partition ) and Not on the RTF template please assist .
    here is my query below :
    select customers.customer_name,
      detail.customer_id,
      detail.customer_site_use_id,
    :CP_ACC_NAME,
    :CP_ACC_NUM,
    :CP_BRNCH_NAME,
    :CP_COL_EMAIL,
    :CP_COL_PHONE,
    :CP_BRANCH_NR,
    decode(  (select meaning
    from ar_lookups
    where lookup_type = 'INV/CM/ADJ' and lookup_code =  detail.class), 'Payment', apply_date, trx.trx_date)  TRX_DATE,
    trx.INVOICE_CURRENCY_CODE,
    trx.INVOICE_CURRENCY_CODE INVOICE_CURRENCY_CODE_BAL,
    NVL(trx.term_due_date,trx.TRX_DATE)  term_due_date,
    detail.customer_id cst_id,
      detail.customer_site_use_id cst_site_id,
      detail.customer_trx_id,
      detail.trx_number,
       (select meaning
    from ar_lookups
    where lookup_type = 'INV/CM/ADJ' and lookup_code =  detail.class) class,
      detail.amount_full_original,
      detail.end_bal * -1 , (detail.end_bal + detail.amount_full_original)* -1  closing_balance ,
    detail.amount_full_original -  detail.the_order * -1  closing_bal,
      (detail.amount_full_original - detail.running_total) running_total ,
      detail.running_tot ,
      customers.customer_name address1,
      customers.address1 address2,
      customers.address2 address3,
      customers.address3 address4,
      customers.city||' '||customers.state address5,
      customers.country||' '||customers.postal_code address6,
      addr.address1 rm_address1,
      addr.address2 rm_address2,
      addr.address3 rm_address3,
      addr.address4 rm_address4,
      addr.address5 rm_address4,
       :p_as_of_date_from date_from,
       to_char(to_date(:p_as_of_date_to,'DD-MON-YYYY'),'DD-Mon-YYYY') date_to,
      addr.org_id rm_org_id,
       rtrim(to_char(sysdate,'DD')||' '||to_char(sysdate,'Month'))||' '||to_char(sysdate, 'YYYY') curr_date
    from (select customer_id, CUSTOMER_SITE_USE_ID, trx.CUSTOMER_TRX_ID,trx.TRX_NUMBER, null apply_date,ps.class,ps.AMOUNT_DUE_ORIGINAL amount_full_original, the_trx.end_bal,the_trx.running_total , the_trx.running_tot , 1 the_order
    select customer_trx_id, sum(acctd_end_bal) end_bal  , running_total  ,  running_tot
    SELECT ps.customer_trx_id ,
       sum(ar_calc_aging.begin_or_end_bal(ps.gl_date,ps.gl_date_closed,
       NULL,to_date(:p_as_of_date_from))
      * ps.amount_due_remaining) start_bal,
       sum(ar_calc_aging.begin_or_end_bal(ps.gl_date,ps.gl_date_closed,
       NULL,to_date(:p_as_of_date_to))
      * ps.amount_due_remaining) end_bal,
       sum(ar_calc_aging.begin_or_end_bal(ps.gl_date,ps.gl_date_closed,
       NULL,to_date(:p_as_of_date_from))
      * ps.acctd_amount_due_remaining) acctd_start_bal,
       sum(ar_calc_aging.begin_or_end_bal(ps.gl_date,ps.gl_date_closed,
       NULL,to_date(:p_as_of_date_to))
      * ps.acctd_amount_due_remaining) acctd_end_bal ,
       (sum(ar_calc_aging.begin_or_end_bal(ps.gl_date,ps.gl_date_closed,
       NULL,to_date(:p_as_of_date_from))
      * ps.amount_due_remaining) + sum(ar_calc_aging.begin_or_end_bal(ps.gl_date,ps.gl_date_closed,
       NULL,to_date(:p_as_of_date_to))
      * ps.acctd_amount_due_remaining) ) running_total ,  
       sum((sum(ar_calc_aging.begin_or_end_bal(ps.gl_date,ps.gl_date_closed,
       NULL,to_date(:p_as_of_date_from))
      * ps.amount_due_remaining) + sum(ar_calc_aging.begin_or_end_bal(ps.gl_date,ps.gl_date_closed,
       NULL,to_date(:p_as_of_date_to))
      * ps.acctd_amount_due_remaining) )) over (partition by ps.customer_trx_id  order by ps.customer_trx_id)running_tot
       FROM ar_payment_schedules_all ps
       WHERE ps.payment_schedule_id+0 > 0
       --JF2 AND ps.gl_date_closed >= to_date(:p_as_of_date_from)
       AND  ps.class IN ( 'CB', 'CM','DEP','DM','GUAR','INV')
       AND  ps.gl_date  <= to_date(:p_as_of_date_to)
       and org_id = nvl(:P_ORG_ID,org_id)--1246
       --and customer_id = :p_customer_id --1075
       --and CUSTOMER_SITE_USE_ID = :p_customer_site_id --1066
       --and customer_trx_id = 66291
       --'|| l_ps_org_where ||'
       GROUP BY ps.customer_trx_id ,ps.acctd_amount_due_remaining , ps.gl_date,ps.gl_date_closed ,ps.class , rownum 
      ps.customer_trx_id ,
       sum(ar_calc_aging.begin_or_end_bal(ps.gl_date,ps.gl_date_closed,
      ra.gl_date,to_date(:p_as_of_date_from))
      * ( ra.amount_applied  + NVL(ra.earned_discount_taken,0)
       + NVL(ra.unearned_discount_taken,0))) start_bal,
       sum(ar_calc_aging.begin_or_end_bal(ps.gl_date,ps.gl_date_closed,
      ra.gl_date,to_date(:p_as_of_date_to))
      * ( ra.amount_applied  + NVL(ra.earned_discount_taken,0)
       + NVL(ra.unearned_discount_taken

    Hi
    Yes i would like more help as i have never used group by roll up before
    SELECT p.employee_number,
    p.full_name employee_name,
    DECODE (p.sex, 'M', 'Male', 'F', 'Female') gender,
    (DECODE (p.per_information4,
    '01', 'Indian',
    '02', 'African',
    '03', 'Coloured',
    '04', 'White')) race,
    p.original_date_of_hire,
    a.effective_start_date startdate_current_position,
    RTRIM (SUBSTR (ps.NAME, 1, INSTR (ps.NAME, ';')), ';') current_position,
    ho.NAME current_organization,
    xx_return_company_name(a.person_id, a.effective_start_date) current_company,
    RTRIM (SUBSTR (ps1.NAME, 1, INSTR (ps1.NAME, ';')),';') previous_position,
    ho1.NAME previous_organization,
    xx_return_company_name(a1.person_id, a1.effective_start_date) previous_company,
    a1.effective_start_date startdate_prev_position
    FROM per_assignments_v2 a,
    per_assignments_v2 a1,
    per_all_people_f p,
    per_all_positions ps,
    per_all_positions ps1,
    hr_all_organization_units_tl ho,
    hr_all_organization_units_tl ho1
    WHERE a1.person_id = a.person_id
    AND NVL (a1.position_id, 9) <> a.position_id
    AND a1.effective_end_date = a.effective_start_date - 1
    AND p.person_id = a.person_id
    and ho.ORGANIZATION_ID=ps.ORGANIZATION_ID
    and ho1.ORGANIZATION_ID=a1.ORGANIZATION_ID
    AND a.effective_start_date BETWEEN p.effective_start_date AND p.effective_end_date
    AND a.position_id = ps.position_id
    and p.EMPLOYEE_NUMBER not in ('1','2','3')
    AND a1.position_id = ps1.position_id(+)
    order by p.full_name

Maybe you are looking for

  • Multiple accounts, multiple devices, same computer

    I am so confused I don't even know how to ask this question. Please bear with me. My wife and i have an iMac OS X. She has an iPhone, iTouch, and MacBook. I have an iPod Classic and iTouch. Her MacBook is her home computer for devices. The iMac is my

  • Link to file in PDF: Receivers cannot open file

    I am creating an interactive PDF newsletter that is going out to many different recipients. In the PDF I would like to link to various files so the recipients can open them and read additional information. My problem is, that when recipients open the

  • Strip port number from request

    We recently upgraded our network and proxy servers. After the upgrade the proxy started adding port number of the origin web server to the request. For example www.xxx.com became www.xxx.com:85 if backend web server was listening to alteon on port 85

  • No class found error in ESR Interface mapping ?

    Hi all , I am trying to add a java validation in my Interface mapping . When i am compiling it in eclipse it is working fine.But when i am exporting and using it in my IM it is giving error as TEst_210/test/validation/OldFileJava6; details: java.lang

  • Skinning/Styling adfp:portlet component while consuming portlets.

    Hi Everyone, I am trying to skin the adfp:portlet component in my portal application. However, every styling that appears in JDeveloper to take effect ( for example if I change the background color, it shows up in editor) seems to go off at runtime.