How can rewrite the Query using Analytical functions ?

Hi,
I have the SQL script as shown below ,
SELECT cd.cardid, cd.cardno,TT.TRANSACTIONTYPECODE,TT.TRANSACTIONTYPEDESC DESCRIPTION,
SUM (NVL (CASE tt.transactiontypecode
WHEN 'LOAD_ACH'
THEN th.transactionamount
END, 0)
) AS load_ach,
SUM
(NVL (CASE tt.transactiontypecode
WHEN 'FUND_TRANSFER_RECEIVED'
THEN th.transactionamount
END,
0
) AS Transfersin,
( SUM (NVL (CASE tt.transactiontypecode
WHEN 'FTRNS'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'SEND_MONEY'
THEN th.transactionamount
END, 0)
)) AS Transferout,
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_ACH'
THEN th.transactionamount
END, 0)
) AS withdrawal_ach,
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK'
THEN th.transactionamount
END, 0)
) AS withdrawal_check,
( SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK_FEE'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'REJECTED_ACH_LOAD_FEE'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_ACH_REV'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK_REV'
THEN th.transactionamount
END,
0
) +
SUM
(NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK_FEE_REV'
THEN th.transactionamount
END,
0
) +
SUM
(NVL (CASE tt.transactiontypecode
WHEN 'REJECTED_ACH_LOAD_FEE_REV'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'OVERDRAFT_FEE_REV'
THEN th.transactionamount
END, 0)
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'STOP_CHECK_FEE_REV'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'LOAD_ACH_REV'
THEN th.transactionamount
END, 0)
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'OVERDRAFT_FEE'
THEN th.transactionamount
END, 0)
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'STOP_CHECK_FEE'
THEN th.transactionamount
END, 0)
)) AS Fee,
th.transactiondatetime
FROM carddetail cd,
transactionhistory th,
transactiontype tt,
(SELECT rmx_a.cardid, rmx_a.endingbalance prev_balance, rmx_a.NUMBEROFDAYS
FROM rmxactbalreport rmx_a,
(SELECT cardid, MAX (reportdate) reportdate
FROM rmxactbalreport
GROUP BY cardid) rmx_b
WHERE rmx_a.cardid = rmx_b.cardid AND rmx_a.reportdate = rmx_b.reportdate) a
WHERE th.transactiontypeid = tt.transactiontypeid
AND cd.cardid = th.cardid
AND cd.cardtype = 'P'
AND cd.cardid = a.cardid (+)
AND CD.CARDNO = '7116734387812758335'
--AND TT.TRANSACTIONTYPECODE = 'FUND_TRANSFER_RECEIVED'
GROUP BY cd.cardid, cd.cardno, numberofdays,th.transactiondatetime,tt.transactiontypecode,TT.TRANSACTIONTYPEDESC
Ouput of the above query is :
CARDID     CARDNO     TRANSACTIONTYPECODE     DESCRIPTION     LOAD_ACH     TRANSFERSIN     TRANSFEROUT     WITHDRAWAL_ACH     WITHDRAWAL_CHECK     FEE     TRANSACTIONDATETIME
6005     7116734387812758335     FUND_TRANSFER_RECEIVED     Fund Transfer Received     0     3.75     0     0     0     0     21/09/2007 11:15:38 AM
6005     7116734387812758335     FUND_TRANSFER_RECEIVED     Fund Transfer Received     0     272     0     0     0     0     05/10/2007 9:12:37 AM
6005     7116734387812758335     WITHDRAWAL_ACH     Withdraw Funds via ACH     0     0     0     300     0     0     24/10/2007 3:43:54 PM
6005     7116734387812758335     SEND_MONEY     Fund Transfer Sent     0     0     1     0     0     0     19/09/2007 1:17:48 PM
6005     7116734387812758335     FUND_TRANSFER_RECEIVED     Fund Transfer Received     0     1     0     0     0     0     18/09/2007 7:25:23 PM
6005     7116734387812758335     LOAD_ACH     Prepaid Deposit via ACH     300     0     0     0     0     0     02/10/2007 3:00:00 AM
I want the output like for Load_ACH there should be one record etc.,
Can any one help me , how can i rewrite the above query using analytical functions .,
Sekhar

Not sure of your requirements but this mayhelp reduce your code;
<untested>
SUM (
   CASE
   WHEN tt.transactiontypecode IN
      ('WITHDRAWAL_CHECK_FEE', 'REJECTED_ACH_LOAD_FEE', 'WITHDRAWAL_ACH_REV', 'WITHDRAWAL_CHECK_REV',
       'WITHDRAWAL_CHECK_FEE_REV', 'REJECTED_ACH_LOAD_FEE_REV', 'OVERDRAFT_FEE_REV','STOP_CHECK_FEE_REV',
       'LOAD_ACH_REV', 'OVERDRAFT_FEE', 'STOP_CHECK_FEE')
   THEN th.transactionamount
   ELSE 0) feeAlso, you might want to edit your post and use &#91;pre&#93; and &#91;/pre&#93; tags around your code for formatting.

Similar Messages

  • How can we write this in analytical function..

    select a.employee_id,a.last_name,b.count from employees a, (select manager_id, count(manager_id) as count from employees group by manager_id) b where a.employee_id=b.manager_id;
    As per my requirement I need each manager name and no of employees reporting to him... above query works.. Could anybody help to write the same using analytic function.... Hw this same can be written in effiect way??? (quick performance)
    Please also share the link to download some doc to have good understanding of analytical function..
    Thanks in advance....

    are you trying to do a hierarchical type of query?
    select ename, count(ename) -1 numr_of_emps_under_this_mgr  from  emp
    connect by  empno =prior mgr
    group by ename
    order by count(ename) desc ;
    ENAME     NUMR_OF_EMPS_UNDER_THIS_MGR
    KING     13
    BLAKE     5
    JONES     4
    CLARK     1
    FORD     1
    SCOTT     1
    ADAMS     0
    TURNER     0
    MARTIN     0
    JAMES     0
    SMITH     0
    MILLER     0
    ALLEN     0
    WARD     0Here is the table structure I used (I think you can download it from oracle somewhere)
    CREATE TABLE EMP
      EMPNO     NUMBER(4)                           NOT NULL,
      ENAME     VARCHAR2(10 BYTE),
      JOB       VARCHAR2(9 BYTE),
      MGR       NUMBER(4),
      HIREDATE  DATE,
      SAL       NUMBER(7,2),
      COMM      NUMBER(7,2),
      DEPTNO    NUMBER(2)
    SET DEFINE OFF;
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
    Values
       (7369, 'SMITH', 'CLERK', 7902, TO_DATE('12/17/1980 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        800, 20);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
    Values
       (7499, 'ALLEN', 'SALESMAN', 7698, TO_DATE('02/20/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        1600, 300, 30);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
    Values
       (7521, 'WARD', 'SALESMAN', 7698, TO_DATE('02/22/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        1250, 500, 30);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
    Values
       (7566, 'JONES', 'MANAGER', 7839, TO_DATE('04/02/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        2975, 20);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
    Values
       (7654, 'MARTIN', 'SALESMAN', 7698, TO_DATE('09/28/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        1250, 1400, 30);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
    Values
       (7698, 'BLAKE', 'MANAGER', 7839, TO_DATE('05/01/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        2850, 30);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
    Values
       (7782, 'CLARK', 'MANAGER', 7839, TO_DATE('06/09/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        2450, 10);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
    Values
       (7788, 'SCOTT', 'ANALYST', 7566, TO_DATE('12/09/1982 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        3000, 20);
    Insert into EMP
       (EMPNO, ENAME, JOB, HIREDATE, SAL, DEPTNO)
    Values
       (7839, 'KING', 'PRESIDENT', TO_DATE('11/17/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        5000, 10);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
    Values
       (7844, 'TURNER', 'SALESMAN', 7698, TO_DATE('09/08/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        1500, 0, 30);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
    Values
       (7876, 'ADAMS', 'CLERK', 7788, TO_DATE('01/12/1983 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        1100, 20);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
    Values
       (7900, 'JAMES', 'CLERK', 7698, TO_DATE('12/03/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        950, 30);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
    Values
       (7902, 'FORD', 'ANALYST', 7566, TO_DATE('12/03/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        3000, 20);
    Insert into EMP
       (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO)
    Values
       (7934, 'MILLER', 'CLERK', 7782, TO_DATE('01/23/1982 00:00:00', 'MM/DD/YYYY HH24:MI:SS'),
        1300, 10);
    COMMIT;

  • How to write the query using Index

    Hi All,
    I have to fetch the records from Database table using Index, how can i write the query using Index. Can any body plz send me the sample code.
    Help Me,
    Balu.

    Hi,
    See the below Example.
    select * from vbak up to 100 rows
    into table t_vbak
    where vbeln > v_vbeln.
    sort t_vbak by vbeln descending.
    read table t_vbak index 1.
    Regards,
    Ram
    Pls reward points if helpful.

  • How to rewrite the query

    hi ,
    Actual i write the query like this , and run this query in sql developer ,
    it will take more than 15 mins
    alloc_details table -- 2 lac records
    doc_details -- 2 lac records
    query :
    SELECT
         a.activity
    ,      sum( a.lamt ) lamt
    ,      sum( a.lamt ) camt
    FROM
         alloc_detail a
    WHERE
         a.alc_code <>'JOB'
    And      a.alc_value is not null
    And      EXISTS ( SELECT
                   distinct b.doc_no
         FROM
                   doc_detail b
         WHERE
                   a.org=b.org
         And      a.doc_no=b.doc_no
         And      a.doc_type=b.doc_type
         And      ( b.dr_act_code='11111' OR b.cr_act_code='111111')
         And      ( ( trunc( b.doc_date ) >='01-jan-08' ) )
         And      ( ( trunc( b.doc_date ) <= '31-jan-08' ) )
    GROUP BY
         a.alc_value
    Anyother way to rewrite the query .

    Is this query working? a.activity seems to be a "not a group by expression"
    select a.activity,sum(a.lamt) lamt,sum(a.lamt) camt  /* 2 times same sum - a typo ? */
      from alloc_detail a
    where a.alc_code != 'JOB'      /* not equal presumed */
       and a.alc_value is not null
       and exists(select distinct b.doc_no
                    from doc_detail b
                   where a.org = b.org
                     and a.doc_no = b.doc_no
                     and a.doc_type = b.doc_type
                     and (b.dr_act_code = '11111' or b.cr_act_code = '111111')
                     and trunc(b.doc_date) >= '01-jan-08'
                     and trunc(b.doc_date) <= '31-jan-08'
    group by a.alc_valuehow about applying predicates first
    select a.alc_value,sum(a.lamt) lamt,sum(a.camt) camt
      from (select alc_value,lamt,camt,org,doc_no,doc_type
              from alloc_detail
             where alc_code != 'JOB'
               and alc_value is not null
           ) a,
           (select distinct org,doc_no,doc_type
              from doc_detail
             where doc_date >= to_date('01-jan-08','dd-mon-rr')
               and doc_date <= to_date('31-jan-08','dd-mon-rr') + 1 - 1/24/60/60
               and (dr_act_code = '11111' or cr_act_code = '111111')
           ) b
    where a.org = b.org
       and a.doc_no = b.doc_no
       and a.doc_type = b.doc_type
    group by a.alc_valueRegards
    Etbin

  • How to Find the Query used by the Workbook ?

    Hi ,
    I am currently involved in trouble shooting a work book where some master data values are not being displayed.
    How do i find, which Query the work books is using ? or Let me know as to how should i proceed in solving the same .

    Hi,
    If you have access to BW Admin workbench... then choose the option metadatarepository(left side) of the BW admin workbench, search for the workbook(Technical name) and click on the "Network display dataflow" link.
    If you want to know the relationship between workbook and the queries:
    Check table RSRWORKBOOK which contains the WORKBOOKID and the GENUNIID.
    Table RSRREPDIR contains GENUNIID, query name RNAME, AUTHOR, LASTUSER, INFOCUBE
    Table RSRINDEXT contains the WORKBOOKID and workbook TITLE
    You should join the tables by GENUNIID and select in RSRREPDIR the rows that have COMPTYPE='REP' for queries and object version active/modified.
    With this information an infoset query can be created to list all workbook-query-user relationship (and even the query-infocube relationship).
    Hope this helps u...
    Regards,
    KK.

  • How to rewrite a query using an or condition?

    my query has a condition in the where caluse which has a OR part. because of the OR part the query cost increases.
    is ther any way to rewrite the OR part apart from using an UNION operator.
    select * from table1 a,table2 b where
    a.field1 = 'CONSTANT'
    and
    a.field2= b.field2
    and (a.field3 = 'input value' or a.field4 = 'input value')

    Just a suggestion(May be a blunder)
    select * from table1 a,table2 b where
    a.field1 = 'CONSTANT'
    and
    a.field2= b.field2
    and DECODE('INPUT',a.field3,'x',a.field4,'x','y')  = 'x'
    Message was edited by:
            jeneesh                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How to split the query using rowid

    can anybody pls tell me logic to spliting the sql query by using rowid through dba_extent...

    Using Parallel Execution will help if it is the DB that is the bottleneck, but if on the other hand it is your client program that is the bottleneck (and the DB is supplying the data as fast as it can) then splitting the query into rowid ranges and having multiple extract clients will have benefit.
    1) Each extract process should read DBA_EXTENTS for the given object and select the extents it should process (Ideally 1 extract process per Data File).
    2) Use the DBMS_ROWID.ROWID_CREATE to build low and high rowid ranges based on the BLOCK_ID and BLOCKS from DBA_EXTENTS. Note you will also have to look up the DATA_OBJECT_ID for the Table in question.
    Note. Because we won't know the Row Number of the last row in the last block, use the first row in the first block after the last block and then use <.
    3) Add the ROWID hint to your query.
    4) Use >= "Low Rowid" < "High Rowid" (Note. use of < rather than <=)
    This method is only safe when the table in question is not being updated, as each extract process will be running its own read consistent view, and so there is a danger that transaction updating 2 rows could have only half of the update extracted (if 1 row was in 1 extent after it had been extracted and another row was in another extent before it was extracted).
    Only 1 rowid range can be passed to the query at a time (you can supply more, but a ROWID access will no longer be performed, it will simply to a FTS).

  • How can change the query where clause values in formatted search

    Hi
    I have a formatted search with the below sql
    Select ItemCode,ItemName from OITM
    where ItemCode like 'FX%'
    how can I change 'FX%' to another value
    I tried to make it as paramter
    where ItemCode like $[ItemCode]
    but I am receiving an error
    sincerely yours
    Riade Asleh

    Try this:
    Select ItemCode,ItemName from OITM
    where ItemCode = $[$5.0.0]
    5 = Item number of form (On item master data 5 is item-number)
    0 = Column of item... since item 5 is no matrix 0 shoul be used.
    0 = Casting type (0 is default, but int, string and date is possible)

  • Query using Analytical Function

    /*Hi there, i have the following table:*/
    create table test (charcode varchar2(10),numberv number,father varchar2(10));
    insert into test values('1',null,null); // Level 1
    insert into test values('1.1',null,'1'); // Level 2
    insert into test values('1.1.1',10,'1.1'); // Level 3
    insert into test values('1.1.2',50,'1.1'); // Level 3
    insert into test values('1.2',100,'1'); // Level 2
    I want the following result
    charcode , numberv
    1 160
    1.1 60
    1.1.1 10
    1.1.2 50
    1.2 100
    The real table has more than 1,000,000 and i have more then 8 levels, without analytical sum, it spend more than 3 hours to process, and i know that with analytical way it should spend less than 15 minutes.
    I have a ideia on how to start: /*
    select charcode,sum(numberv) over(partition father order by charcode desc)
    order by charcode desc;
    /* but this result it´s far way from what i expect ( the charcode 1 wasn´t populate and other values appears different values than i want
    Thanks
    Hélio*/

    here is your query. it will work for charcode between 1 and 9999.9999.9999.9999
    of course you could extend it
    this use regexp, but you could also use substr and instr.
    select charcode,
    numberv,
    decode(length(charcode)-length(replace(charcode,'.')),
        0,
        sum(NUMBERV) over (partition by
            substr(
                replace(regexp_replace(
                regexp_replace(
                regexp_replace('.'||replace(charcode,'.','..')||'.',
                '\.([0-9]{1})\.' , '.000\1.'),
                '\.([0-9]{2})\.' , '.00\1.'),
                '\.([0-9]{3})\.' , '.0\1.'),'.')
            ,1, 4)
        1,
        sum(NUMBERV) over (partition by
            substr(
                replace(regexp_replace(
                regexp_replace(
                regexp_replace('.'||replace(charcode,'.','..')||'.',
                '\.([0-9]{1})\.' , '.000\1.'),
                '\.([0-9]{2})\.' , '.00\1.'),
                '\.([0-9]{3})\.' , '.0\1.'),'.')
            ,5, 4)
        2,
        sum(NUMBERV) over (partition by
            substr(
                replace(regexp_replace(
                regexp_replace(
                regexp_replace('.'||replace(charcode,'.','..')||'.',
                '\.([0-9]{1})\.' , '.000\1.'),
                '\.([0-9]{2})\.' , '.00\1.'),
                '\.([0-9]{3})\.' , '.0\1.'),'.')
            ,9, 4)
        3,
        sum(NUMBERV) over (partition by
            substr(
                replace(regexp_replace(
                regexp_replace(
                regexp_replace('.'||replace(charcode,'.','..')||'.',
                '\.([0-9]{1})\.' , '.000\1.'),
                '\.([0-9]{2})\.' , '.00\1.'),
                '\.([0-9]{3})\.' , '.0\1.'),'.')
            ,13, 4)
        4,
        sum(NUMBERV) over (partition by
            substr(
                replace(regexp_replace(
                regexp_replace(
                regexp_replace('.'||replace(charcode,'.','..')||'.',
                '\.([0-9]{1})\.' , '.000\1.'),
                '\.([0-9]{2})\.' , '.00\1.'),
                '\.([0-9]{3})\.' , '.0\1.'),'.')
            ,17, 4)
    ) "sum"
    from test
    order by
    replace(regexp_replace(
        regexp_replace(
        regexp_replace('.'||replace(charcode,'.','..')||'.',
        '\.([0-9]{1})\.' , '.000\1.'),
        '\.([0-9]{2})\.' , '.00\1.'),
        '\.([0-9]{3})\.' , '.0\1.'),'.')
    CHARCODE      NUMBERV        sum
    1                            160
    1.1                           60
    1.1.1              10         10
    1.1.2              50         50
    1.2               100        100I hope you appreciate !
    Regards
    Laurent

  • How to avoid the query using the index?

    select *from cstb_cut ;
    cust_id      cust_name
    10001        xxxx
    10002        yyyy
    10003        zzzz
    Please find the above table and 10000 records are there in that table and also one index was there on cust_id.
    but my query don't want to use the index?how it will be done?

    Hi,
    DEVI suvarna wrote:
    select *from cstb_cut ;
    cust_id      cust_name
    10001        xxxx
    10002        yyyy
    10003        zzzz
    Please find the above table and 10000 records are there in that table and also one index was there on cust_id.
    but my query don't want to use the index?how it will be done?
    The index doesn't help for this query, so it's not used.
    The main purpose of indexes is the help find a small number (like 1) of rows quickly.  You're asking for all rows; getting each one through the index would be slower than just getting all of them directly.
    Sometimes (but not here) the index can be used instead of the table, when the index contains all the data you need from the table.  An index on cust_id doesn't tell you anything about any other column, such as cust_name.  Since you asked to have all columns displayed, it has to fetch data from the table, not the index.

  • [b]Using Analytic functions...[/b]

    Hi All,
    I need help in writing a query using analytic functions.
    Foll is my scenario. I have a table cust_points
    CREATE TABLE cust_points
    ( cust_id varchar2(10),
    pts_dt date,
    reward_points number(3),
    bal_points number(3)
    insert into cust_points values ('ABC',01-MAY-2004',5, 15)
    insert into cust_points values ('ABC',05-MAY-2004',3, 12)
    insert into cust_points values ('ABC',09-MAY-2004',3, 9)
    insert into cust_points values ('XYZ',02-MAY-2004',8, 4)
    insert into cust_points values ('XYZ',03-MAY-2004',5, 1)
    insert into cust_points values ('JKL',10-MAY-2004',5, 11)
    I want a result set which shows for each customer, the sum of reward his/her points
    but balance points as of the last date. So for the above I should have foll results
    cust_id reward_pts bal_points
    ABC 11 9
    XYZ 13 1
    JKL 5 11
    I having tried using last_value(), for eg
    Select cust_id, sum(reward_points), last_value(bal_points) over (partition by cust_id)...but run into grouping errors.
    Can anyone help ?

    try this...
    SELECT a.pkcol,
         nvl(SUM(b.col1),0) col1,
         nvl(SUM(b.col2),0) col2,
         nvl(SUM(b.col3),0) col3
    FROM table1 a, table2 b, table3 c
    WHERE a.pkcol = b.plcol(+)
    AND a.pkcol = c.pkcol
    GROUP BY a.pkcol;
    SQL> select a.deptno,
    2 nvl((select sum(sal) from test_emp b where a.deptno = b.deptno),0) col1,
    3 nvl((select sum(comm) from test_emp b where a.deptno = b.deptno),0) col2
    4 from test_dept a;
    DEPTNO COL1 COL2
    10 12786 0
    20 13237 738
    30 11217 2415
    40 0 0
    99 0 0
    SQL> select a.deptno,
    2 nvl(sum(b.sal),0) col1,
    3 nvl(sum(b.comm),0) col2
    4 from test_dept a,test_emp b
    5 where a.deptno = b.deptno
    6 group by a.deptno;
    DEPTNO COL1 COL2
    30 11217 2415
    20 13237 738
    10 12786 0
    SQL> select a.deptno,
    2 nvl(sum(b.sal),0) col1,
    3 nvl(sum(b.comm),0) col2
    4 from test_dept a,test_emp b
    5 where a.deptno = b.deptno(+)
    6 group by a.deptno;
    DEPTNO COL1 COL2
    10 12786 0
    20 13237 738
    30 11217 2415
    40 0 0
    99 0 0
    SQL>

  • Self-join query to Analytical function query

    Hi All,
    I have converted a self-join query to Analytical function query due to the performance reasons.
    Query which is using Analytical function is giving the correct count as I compared it with query using Self-Join. Can you please tell what is wrong in the query.
    ==========================
    Query using Self-Join
    select count(1)
    From (select t1.*, max(t1.dw_creation_dt) over (partition by t1.empl_id) pers_max_date
    from ohr_pers_curr t1 ) pers
         , (select t2.*, max(t2.dw_creation_dt) over (partition by t2.empl_id, t2.empl_rcd) job_max_date
         from OHR_JOB_CURR t2) job
    where pers.empl_id = job.empl_id
    and pers.dw_creation_dt=pers.pers_max_date
    and job.dw_creation_dt=job.job_max_date
    and job.dummy_row_flag = 'N'
    and pers.dw_creation_rsn_cd in ('N', 'U')
    and job.dw_creation_rsn_cd in ('N', 'U')
    ================================================
    Query Using Analytical function
    select count(1)
    From (select t1.*, max(t1.dw_creation_dt) over (partition by t1.empl_id) pers_max_date
    from ohr_pers_curr t1 ) pers
         , (select t2.*, max(t2.dw_creation_dt) over (partition by t2.empl_id, t2.empl_rcd) job_max_date
         from OHR_JOB_CURR t2) job
    where pers.empl_id = job.empl_id
    and pers.dw_creation_dt=pers.pers_max_date
    and job.dw_creation_dt=job.job_max_date
    and job.dummy_row_flag = 'N'
    and pers.dw_creation_rsn_cd in ('N', 'U')
    and job.dw_creation_rsn_cd in ('N', 'U')
    ==================================

    Hi David,
    The base is same but the problem is different.
    As far as implementation concern these queries looks same, but when I see there counts, they do not match. I do not have any reason why this happening.
    Regards
    Gaurav

  • SQL Query With analytical function

    Hi
    Below is the scenario which i am looking for in sql query using analytical functions
    I/p
    Col1
    50
    0
    -150
    -200
    300
    -100
    -300
    500
    -100
    O/p
    Col1          col2
    50                 0
    0                   0
    -150          -100
    -200              -200
    300               0
    -100              0
    -300              -100
    500               400
    -100              0Any help really appreciated
    Thanks in advance
    Edited by: unique on Aug 10, 2010 4:53 AM
    Edited by: unique on Aug 10, 2010 4:55 AM
    Edited by: unique on Aug 10, 2010 4:55 AM

    Oh,In this case,There is OLAP solution ;-)
    OLAP samples of my homepage http://www.geocities.jp/oraclesqlpuzzle/oracle-sql1-olap.html
    with work(SK,Val) as(
    select  1,  50 from dual union
    select  2,   0 from dual union
    select  3,-150 from dual union
    select  4,-200 from dual union
    select  5, 300 from dual union
    select  6,-100 from dual union
    select  7,-300 from dual union
    select  8, 500 from dual union
    select  9,-100 from dual)
    select SK,Val,GID,
    case when Val > 0
         then greatest(0,sum(Val) over(partition by GID))
         else Least(0,Val+greatest(0,sum(Val) over(partition by GID
                                     order by SK rows between unbounded preceding
                                                          and 1 preceding)))
         end as COL3
    from (select SK,Val,
          sum(greatest(0,sign(Val))) over(order by SK) as GID
          from work)
    order by SK;
    SK   VAL  GID  COL3
    1    50    1     0
    2     0    1     0
    3  -150    1  -100
    4  -200    1  -200
    5   300    2     0
    6  -100    2     0
    7  -300    2  -100
    8   500    3   400
    9  -100    3     0

  • How to fetch the query string in BW?

    Hi Gurus,
    How can fetch the query string from BW Server to implement  Bex web application Iview.
    *Now to get the information "BEx Web Application Query String". open the SAP Log on and open the BW Browser, double click any of the queries available, this will open a window and select the Query String ( it should be between & to end &).*
    Explain the above point with more detail like how will open the bw browser what is the step for that?where the query string will available in BW browser.
    Higher points will be rewarded for valuable inputs.
    Thanks in Advance,
    Dharani

    hi ,
    What krishna suggested is right ... Another method can be like :
    &CMD=LDOC&infocube=0D_DECU&query=SALES_DEMO_2
    Get this information from your BI consultant.
    Please close thread if you got ur answer!!!
    Regards
    Parth

  • How can and where i use the function "HTMLDB_UTIL.CLEAR_PAGE_CACHE "

    how can and where i use the function "HTMLDB_UTIL.CLEAR_PAGE_CACHE " ?
    where is the place to put this function?

    i have notice that sometime, during visual a report of a table, order by data insert desc, some record are not displayed on some client.
    Only whe we clear the cache of internet explorer than this record can view.
    I have an idea that a clear page when i call the report may be solve the problem.
    Thank's.
    ------------------------

Maybe you are looking for

  • Multiple Languages - Challenging Question

    This may be an easy answer, as in 'can't do it' however, here it is: Project includes two languages - English, Spanish I understand the multi-language menu setup and multi-audio stream setup. In each video sequence of the project, two separate graphi

  • I can't print to a kyocera c3232E with network accounting

    I'm working with a Krocera KM-C3232E printer with network accounting. I added the printer using it's IP addr. It indicated the correct driver name, a valid unit and IP. After continuing an problem is indicated and asks if I still want to create a que

  • Basic questions about Logic Pro

    Reading the topics and discussion groupes in this Logic Pro community is a tiny bit scary to me I admitt. Yet I will launch my basic question into the air, hoping to get an answer. I am a complete beginner in this field and only read something about

  • Syntax error in Define entitytype method in MPC class.

    Hi, I am trying to create Odata service by creating new entity type and entity set at service level creation itself (Data model-->Create). Now at the time of generation of classes I am getting syntax error fields are not defined in method, Define Ent

  • ?? Save Project ???

    Not sure if this is possible, but if there is a way to collect all of your media used in an FCP project. I want to save my FCP project along with all of my raw footage, txt, snd fx (everything in the browser). Im working on a big project, drawing foo