How to use analytical function in this case

SELECT COUNT (rms.status_code) rms_status_count,
rms.status_name rms_status_name,
TO_CHAR (rtd.add_date, 'MON')|| ' '|| TO_CHAR (rtd.add_date, 'YYYY') month_year,
MAX (rtd.add_date) date_for_sort
FROM ri_mast_status rms, ri_tran_data rtd
WHERE rtd.status_code = rms.status_code
AND TRUNC (MONTHS_BETWEEN (SYSDATE, rtd.add_date)) < 36
AND NVL (rtd.delete_flg, '0') = '0'
GROUP BY TO_CHAR (rtd.add_date, 'MON')|| ' '|| TO_CHAR (rtd.add_date, 'YYYY'),
rms.status_name
ORDER BY MAX (rtd.add_date);
it gives output for the last 3 years based on month and year.

r you trying this ?
select *from
select rms.*,
row_number() over(partition by TO_CHAR (rtd.add_date, 'MON')|| ' '|| TO_CHAR (rtd.add_date,rms.status_name 'YYYY') order by rtd.add_date) RN,
MAX(rtd.add_date) over(partition by TO_CHAR (rtd.add_date, 'MON')|| ' '|| TO_CHAR (rtd.add_date,rms.status_name 'YYYY')  order by rtd.add_date) date_for_sort,
COUNT(rms.status_code) over(partition by TO_CHAR (rtd.add_date, 'MON')|| ' '|| TO_CHAR (rtd.add_date,rms.status_name 'YYYY')  order by rtd.add_date) rms_status_count
FROM ri_mast_status rms, ri_tran_data rtd
WHERE rtd.status_code = rms.status_code
AND TRUNC (MONTHS_BETWEEN (SYSDATE, rtd.add_date)) < 36
AND NVL (rtd.delete_flg, '0') = '0'
where rn=1

Similar Messages

  • How to use analytic function with aggregate function

    hello
    can we use analytic function and aggrgate function in same qurey? i tried to find any example on Net but not get any example how both of these function works together. Any link or example plz share with me
    Edited by: Oracle Studnet on Nov 15, 2009 10:29 PM

    select
    t1.region_name,
    t2.division_name,
    t3.month,
    t3.amount mthly_sales,
    max(t3.amount) over (partition by t1.region_name, t2.division_name)
    max_mthly_sales
    from
    region t1,
    division t2,
    sales t3
    where
    t1.region_id=t3.region_id
    and
    t2.division_id=t3.division_id
    and
    t3.year=2004
    Source:http://www.orafusion.com/art_anlytc.htm
    Here max (aggregate) and over partition by (analytic) function is in same query. So it means we can use aggregate and analytic function in same query and more than one analytic function in same query also.
    Hth
    Girish Sharma

  • How to use Analytic functions in Forms10g

    Hi,
    Can we use Analytic function in forms10g like Lead & Lag.
    Thanks & Regards,

    Use a db view as a data source of your form block ....
    Greetings...
    Sim

  • How to use analytical functions inside a mapping

    Hello everybody. Here Isend you a trick that we are using for two years.
    If you want to use a function (for instance :ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...)) inside a mapping you must create an expression with in the INGRP1 the fields you are going to use in the window function and in the OUTGRP1 the function you want. Create an Out -attribute with the expressión and link it to a distinct operator (using a "distinct" you encapsulte the sql and you will be able to use the function inside a filter - in the where clause.). The distinct can eliminate some register (depends on the function). If you validate the expression an error will appear (don't worry about that, the mapping will be ok).
    But there is a limitation, you will not be able tou sum over, min over, max over (it detect that they are aggregator functions). Other limitation: the debugger doesn't run with this kind of functions.
    Please publish this information on "The Warehouse Builder Utility Exchange". Mi email is [email protected] (if you need more information)

    It is possible to add SUM, MIN, MAX functions also to OWB releases prior to Paris - You have to put them in double quotes - write "MIN" "MAX" "SUM" etc. The rest of the rules (adding cut-off operator after the expression - as DISTINCT, or UNION ALL) is analogical to with ROW_NUMBER()
    That means You can create mapping with following functionality:
    SELECT
    sum (salary) over (partition by DEPARTMENT) department_salary,
    salary,
    employee_id,
    employee_name
    FROM employees_salaries
    writing it this way in OWB:
    SELECT
    "SUM" (salary) over (partition by DEPARTMENT) department_salary,
    salary,
    employee_id,
    employee_name
    FROM employees_salaries
    Regards,
    Martin

  • Can I use analytical function in this problem?

    Hi,
    I want to use query only for the following . I don't want to wright any function or procedure for this.
    create temp table test_3 (user_id number, auth_id number);
    insert into test_3 values (133,609);
    insert into test_3 values (133,610);
    insert into test_3 values (133,611);
    insert into test_3 values (133,612);
    insert into test_3 values (133,613);
    insert into test_3 values (133,614);
    insert into test_3 values (144,1);
    insert into test_3 values (134,610);
    insert into test_3 values (135,610);
    insert into test_3 values (135,610);
    insert into test_3 values (135,610);
    insert into test_3 values (136,610);
    insert into test_3 values (136,610);
    insert into test_3 values (137,610);
    insert into test_3 values (137,610);
    insert into test_3 values (137,609);
    insert into test_3 values (137,11);
    I want to count:
    1. for each auth_id, how many users are there who is assigned to this aut_id only
    example
    user_id 134 and 135 is assigned to auth_id 610 only and the count is 3 and 2 respectively .
    user_id 144 is assigned to auth_id 1 only and the count is 1.
    2.how many user_id is common between auth_id 609 and 610
    how many user_id is common between auth_id 609 and 611
    how many user_id is common between auth_id 609 and 612
    and so on.
    I have re-written the problem bellow
    Regards,
    Edited by: user576726 on Feb 13, 2011 3:54 AM

    Hi,
    user576726 wrote:
    Hi,
    Thanks for the response.
    drop table test_3;
    create table test_3 (user_id number, auth_id number);
    insert into test_3 values (133,609);     --row 1
    ...Thanks. That makes the problem a lot clearer.
    My desired output is:
    auth_id_1 auth_id_2 count1 count2
    1 12 1 --(user_id 144) 2 --(row 15, row 16)
    1 610 1 --(user_id 144) 1 --(row 19)
    11 609 1 --(user_id 137) 1 --(row 13)
    11 610 1 --(user_id 137) 2 --(row 11, row 12)
    12 1 1 --(user_id 144) 1 --(row 4)
    12 610 1 --(user_id 144) 1 --(row 19)
    609 11 1 --(user_id 137) 1 --(row 14)
    609 610 2 --(user_id 133 & 137) 3      --(row 2, row 11 and row 12)
    609 611 1 --(user_id 133) 1 --(row 3)
    610 1 1 --(user_id 144) 1 --(row 4)
    610 11 1 --(user_id 137) 1 --(row 14)
    610 12 1 --(user_id 144) 2 --(row 15, row 16)
    610 609 2 --(user_id 133 & 137) 4 --(row 1, row 13, row 17 and row 18)
    610 611 1 --(user_id 133) 1 --(row 3)
    611 609 1 --(user_id 133) 3 --(row 1, row 17 and row 18)
    611 610 1 --(user_id 133) 1 --(row 2)               1 --(user_id 133)               1 --(row 2)
    Count1 is the number of common different user id between auth_id_1 and auth_id_2
    example
    for the first row in the output:-
    common user ids between 609 and 610 are 133 and 137. so the count1 should be 2
    Count2 is how many rows are there for auth_id_2 where user id is common for auth_id_1 and auth_id_2
    example
    for the first row in the output:-
    the common user_id for 609 and 610 are 133 & 137
    the rows in the test_3 table that has auth_id 610 and user_id 133 & 137 are
    row 2, row 11 and row 12 so the count is 3.
    What I have done is
    I have writtent the following query to get the first two columns of the output:
    select tab1.auth_id auth_id_1, tab2.auth_id auth_id_2
    from
    (select user_id, auth_id
    from test_3
    group by user_id, auth_id
    ) tab1,
    (select user_id, auth_id
    from test_3
    group by user_id, auth_id
    ) tab2
    where tab1.user_id = tab2.user_id
    and tab1.auth_id <> tab2.auth_id
    group by tab1.auth_id, tab2.auth_id
    order by 1,2;You're on the right track. You're doing a self-join and getting the right combinations of auth_id_1 and auth_id_2.
    Why are you doing the GROUP BY in sub-queries tab1 and tab2? Eventually, you'll need to count identical rows, like these:
    insert into test_3 values (137,610);     --row 11
    insert into test_3 values (137,610);     --row 12If you do a GROUP BY in the sub-queries, all you'll know is that user_id=137 was related to auth_id=610. You won't know how many times, which is what count2 is based on. So don't do a GROUP BY in the sub-queries; just do the GROUP BY in the main query. That means you won't need to do sub-queries; you might as well just join two copies of the original test_3 table.
    Count1 is the number of common different user id between auth_id_1 and auth_id_2Great; that's very clear. In SQL, how do you count the number of different user_ids in such a group? (Hint "different" means the same thing as "distinct".)
    Count2 is how many rows are there for auth_id_2 where user id is common for auth_id_1 and auth_id_2
    example
    for the first row in the output:-The first row in the output you posted was
    1 12 1 --(user_id 144) 2 --(row 15, row 16)Isn't this one that you're explaining here the 8th row of output?
    the common user_id for 609 and 610 are 133 & 137
    the rows in the test_3 table that has auth_id 610 and user_id 133 & 137 are
    row 2, row 11 and row 12 so the count is 3.So, for count2, you want to know how many distinct rows from tab2 are in each group. If you had a primary key in the table, or anything that uniquely identified the rows, you could count the distinct occurrences of that, but you're not storing anything unique on each row (at least you haven't mentioned it in your sample data). If that's really the case, then this is one place where the ROWID pseudocolumn is handy; it uniquely identifies any row in any table, so you can just count how many different values of tab2.ROWID are in each group.

  • How to use dynamic SQL in this case for best performance

    I have the table with following columns
    ID NUMBER,
    DATA LONG,
    TAG VARCHAR2(255)
    Records in this table will be like following
    1 this is an abstract ABSTRACT
    1 this is author AUTHOR
    1 100 PRICE
    2 this is an abstract ABSTRACT
    2 this is author AUTHOR
    3 contract is this CONTRACT
    Basically all the records with the same number constitute 1 record for another table. Tag in the above table indicates that what column it is and DATAwill have the actual data for that column. I need to populate the second table based an the above table but will not get the same number of TAGS all the time. I need to insert the values only for the columns provided in the TAG field. How will I accomplish this by dynamic sql. Do I create a loop and create two strings one with columns and one with values and then combine them and use execute immediate to insert into table? Is there an easier way to do this??
    Please respond quickly.
    Thanks
    Bhawna
    null

    > so which collection should i use to perform it..
    so that performance is best......
    Program to interfaces. That way, you can switch out implementations and test for yourself which performance is best in an actual production context. But first, write your program so that it works. Worry about refactoring for performance once your program is written and it works.
    > plz send me the logic....
    Give it a shot on your own first; we can help if you get stuck.
    ~

  • How to use the List in this case.

    I got sample code from http://developer.java.sun.com/developer/technicalArticles/ThirdParty/WebCrawler/. But when I compiled it, there was error message like "webcrawler.java:16: reference to List is ambiguous, both class java.util.List in java.util and class java.awt.List in java.awt match List listMatches; "
    I think it might be because it was written by JDK1.1.3. I am using JDK1.4 to compile it. Can any body tell me how to make it can be compiled by JDK1.4?
    Thanks a lot.

    Hi olivia16
    There is an interface called java.util.List and a class java.util.List :-)
    To correct source code just put java.util. prefix at lines 16 and 76:
    java.awt.List listMatches; // 16
    listMatches = new java.awt.List(10); // 76
    Regards.

  • Using analytic function to get the right output.

    Dear all;
    I have the following sample date below
    create table temp_one
           id number(30),  
          placeid varchar2(400),
          issuedate  date,
          person varchar2(400),
          failures number(30),
          primary key(id)
    insert into temp_one values (1, 'NY', to_date('03/04/2011', 'MM/DD/YYYY'), 'John', 3);
    insert into temp_one values (2, 'NY', to_date('03/03/2011', 'MM/DD/YYYY'), 'Adam', 7);
    insert into temp_one values (3, 'Mexico', to_date('03/04/2011', 'MM/DD/YYYY'), 'Wendy', 3);
    insert into temp_one values (4, 'Mexico', to_date('03/14/2011', 'MM/DD/YYYY'), 'Gerry', 3);
    insert into temp_one values (5, 'Mexico', to_date('03/15/2011', 'MM/DD/YYYY'), 'Zick', 9);
    insert into temp_one values (6, 'London', to_date('03/16/2011', 'MM/DD/YYYY'), 'Mike', 8);this is output I desire
    placeid       issueperiod                               failures
    NY              02/28/2011 - 03/06/2011          10
    Mexico       02/28/2011 - 03/06/2011           3
    Mexico        03/14/2011 - 03/20/2011          12
    London        03/14/2011 - 03/20/2011          8All help is appreciated. I will post my query as soon as I am able to think of a good logic for this...

    hI,
    user13328581 wrote:
    ... Kindly note, I am still learning how to use analytic functions.That doesn't matter; analytic functions won't help in this problem. The aggregate SUM function is all you need.
    But what do you need to GROUP BY? What is each row of the result set going to represent? A placeid? Yes, each row will represent only one placedid, but it's going to be divided further. You want a separate row of output for every placeid and week, so you'll want to GROUP BY placeid and week. You don't want to GROUP BY the raw issuedate; that would put March 3 and March 4 into separate groups. And you don't want to GROUP BY failures; that would mean a row with 3 failures could never be in the same group as a row with 9 failures.
    This gets the output you posted from the sample data you posted:
    SELECT       placeid
    ,             TO_CHAR ( TRUNC (issuedate, 'IW')
                  , 'MM/DD/YYYY'
                ) || ' - '|| TO_CHAR ( TRUNC (issuedate, 'IW') + 6
                                             , 'MM/DD/YYY'
                               )     AS issueperiod
    ,       SUM (failures)                  AS sumfailures
    FROM        temp_one
    GROUP BY  placeid
    ,            TRUNC (issuedate, 'IW')
    ;You could use a sub-query to compute TRUNC (issuedate, 'IW') once. The code would be about as complicated, efficiency probably won't improve noticeably, and the the results would be the same.

  • How to use a CTE in a CASE statement?

    Hi everyone,
    I'm trying to use a CTE in a CASE statement and getting error. In fact, I'm not sure how to use a CTE in this case and this is the first time I'm using a CTE. I managed to build a CTE successfully but, failed to incorporate it in the CASE statement.
    SELECT wbs,
    MIN(srt),
    MAX(srt)
    CASE ust
    WHEN
    WITH CTE AS
    SELECT p_id + p_name + ps_name AS colg, p_id
    FROM pd_info
    WHERE s_desc = 'warehouse'
    OR p_desc = 'Originated from warehouse'
    OR p_desc = 'Originated from a partner store'
    SELECT colg FROM CTE
    WHERE cte.colg IN (SELECT p_id + p_name + ps_name AS c
    omb from pd_info)
    OR cte.p_id = 'xxxxxxxx' THEN 'invalid'
    ELSE 'valid'
    END,
    p_name,
    sub_name,
    ps_name,
    FROM pd_info;
    Could experts here please help me?
    Thank you,
    Bangaaram
    Known is a DROP, Unknown is an OCEAN.

    With the help from some experts, I've figured out that CTE doesn't work in this case. Here are the queries that I'm using for the above. Either of them is working good so far.
    SELECT Wbs
    , MIN ( Srt )
    , MAX ( Srt )
    , CASE
    WHEN S_Desc = 'warehouse'
    OR P_Desc = 'Originated from warehouse'
    OR P_Desc = 'Originated from a partner store'
    OR P_Id = 'xxxxxxxx'
    THEN 'invalid'
    ELSE 'valid'
    END AS Ust
    , P_Name
    , Sub_Name
    , Ps_Name
    FROM Pd_Info
    GROUP BY Wbs
    , CASE
    WHEN S_Desc = 'warehouse'
    OR P_Desc = 'Originated from warehouse'
    OR P_Desc = 'Originated from a partner store'
    OR P_Id = 'xxxxxxxx'
    THEN 'invalid'
    ELSE 'valid'
    END
    , P_Name
    , Sub_Name
    , Ps_Name;
    SELECT wbs,
    MIN(srt),
    MAX(srt)
    CASE
    WHEN cte.p_id = 'xxxxxxxx'
    or exists
    (select *
    from pd_info p2
    WHERE (s_desc = 'warehouse'
    OR p_desc = 'Originated from warehouse'
    OR p_desc = 'Originated from a partner store' )
    and p2.p_id = p1.p_id and p2.p_name = p1.p_name and p2.ps_name = p1.ps_name
    THEN 'invalid'
    ELSE 'valid'
    END ust,
    p_name,
    sub_name,
    ps_name,
    FROM pd_info p1;
    Thank you,
    Bangaaram
    Known is a DROP, Unknown is an OCEAN.

  • 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.

  • Oracle:how to use max() function in case expression

    how to use max() function in case expression, Please explain with any example

    Hope this helps and should be self explanatory
    with t as
    (select 1 col,100 col2 from dual union
    select 2 ,100 from dual union
    select 2 ,200 from dual union
    select 3,100  from dual union
    select 3,200  from dual  )
    select col, case when max(col2)=100 then 'with 100 range'
    when  max(col2)=200 then 'with 200 range' end  from t group by col

  • Hi fellow apple guys, i have this problem. Hope you can help me. I don't know how to use the function keys (F1 to F12) on my macbook air. Pls help

    Hi fellow apple guys, i have this problem. Hope you can help me. I don't know how to use the function keys (F1 to F12) on my macbook air. Pls help

    Out of the box, to use the function keys as function keys, hold down the fn key when you press the key. Otherwise, you get the picture function on the key. You can reverse this behavior in the Keyboard system prefs.

  • How to use Aggregate Functions during Top N analysis?

    Say i want to find top 5 highest salaries and their totals and average. In that case how to use aggregate functions. Please give me an example on this.
    Regards,
    Renu
    Message was edited by:
    user642387

    Hi,
    Yes, you can do that with aggregate functions.
    First, do a sub-query to retrieve all the salaries (in descending order), then say "WHERE ROWNUM <= 5" in the main query. Use the aggregate SUM and AVG functions in the main query.
    Analytic functions are easier to use for jobs like this, once you get familiar with them. If you're not leaving the field this month, then it's probably worthwhile for you to get familiar with analytic functions.

  • Using analytical function - value with highest count

    Hi
    i have this table below
    CREATE TABLE table1
    ( cust_name VARCHAR2 (10)
    , txn_id NUMBER
    , txn_date DATE
    , country VARCHAR2 (10)
    , flag number
    , CONSTRAINT key1 UNIQUE (cust_name, txn_id)
    INSERT INTO table1 (cust_name, txn_id, txn_date,country,flag) VALUES ('Peter', 9870,TO_DATE ('15-Jan-2011', 'DD-Mon-YYYY'), 'Iran', 1);
    INSERT INTO table1 (cust_name, txn_id, txn_date,country,flag) VALUES ('Peter', 9871,TO_DATE ('16-Jan-2011', 'DD-Mon-YYYY'), 'China', 1);
    INSERT INTO table1 (cust_name, txn_id, txn_date,country,flag) VALUES ('Peter', 9872,TO_DATE ('17-Jan-2011', 'DD-Mon-YYYY'), 'China', 1);
    INSERT INTO table1 (cust_name, txn_id, txn_date,country,flag) VALUES ('Peter', 9873,TO_DATE ('18-Jan-2011', 'DD-Mon-YYYY'), 'Japan', 1);
    INSERT INTO table1 (cust_name, txn_id, txn_date,country,flag) VALUES ('Peter', 9874,TO_DATE ('19-Jan-2011', 'DD-Mon-YYYY'), 'Japan', 1);
    INSERT INTO table1 (cust_name, txn_id, txn_date,country,flag) VALUES ('Peter', 9875,TO_DATE ('20-Jan-2011', 'DD-Mon-YYYY'), 'Russia', 1);
    INSERT INTO table1 (cust_name, txn_id, txn_date,country,flag) VALUES ('Peter', 9877,TO_DATE ('22-Jan-2011', 'DD-Mon-YYYY'), 'China', 0);
    INSERT INTO table1 (cust_name, txn_id, txn_date,country,flag) VALUES ('Peter', 9878,TO_DATE ('26-Jan-2011', 'DD-Mon-YYYY'), 'Korea', 0);
    INSERT INTO table1 (cust_name, txn_id, txn_date,country,flag) VALUES ('Peter', 9811,TO_DATE ('17-Jan-2011', 'DD-Mon-YYYY'), 'China', 0);
    INSERT INTO table1 (cust_name, txn_id, txn_date,country,flag) VALUES ('Peter', 9854,TO_DATE ('13-Jan-2011', 'DD-Mon-YYYY'), 'Taiwan', 0);
    The requirement is to create an additional column in the resultset with country name where the customer has done the maximum number of transactions
    (with transaction flag 1). In case we have two or more countries tied with the same count, then we need to select the country (among the tied ones)
    where the customer has done the last transaction (with transaction flag 1)
    e.g. The count is 2 for both 'China' and 'Japan' for transaction flag 1 ,and the latest transaction is for 'Japan'. So the new column should contain 'Japan'
    CUST_NAME TXN_ID TXN_DATE COUNTRY FLAG country_1
    Peter 9811 17-JAN-11 China 0 Japan
    Peter 9854 13-JAN-11 Taiwan 0 Japan
    Peter 9870 15-JAN-11 Iran 1 Japan
    Peter 9871 16-JAN-11 China 1 Japan
    Peter 9872 17-JAN-11 China 1 Japan
    Peter 9873 18-JAN-11 Japan 1 Japan
    Peter 9874 19-JAN-11 Japan 1 Japan
    Peter 9875 20-JAN-11 Russia 1 Japan
    Peter 9877 22-JAN-11 China 0 Japan
    Peter 9878 26-JAN-11 Korea 0 Japan
    Please let me know how to accomplish this using analytical functions
    Thanks
    -Learnsequel

    Does this work (not spent much time checking it)?
    WITH ana AS (
    SELECT cust_name, txn_id, txn_date, country, flag,
            Sum (flag)
                OVER (PARTITION BY cust_name, country)      n_trx,
            Max (CASE WHEN flag = 1 THEN txn_date END)
                OVER (PARTITION BY cust_name, country)      l_trx
      FROM cnt_trx
    SELECT cust_name, txn_id, txn_date, country, flag,
            First_Value (country) OVER (PARTITION BY cust_name ORDER BY n_trx DESC, l_trx DESC) top_cnt
      FROM ana
    CUST_NAME      TXN_ID TXN_DATE  COUNTRY          FLAG TOP_CNT
    Fred             9875 20-JAN-11 Russia              1 Russia
    Fred             9874 19-JAN-11 Japan               1 Russia
    Peter            9873 18-JAN-11 Japan               1 Japan
    Peter            9874 19-JAN-11 Japan               1 Japan
    Peter            9872 17-JAN-11 China               1 Japan
    Peter            9871 16-JAN-11 China               1 Japan
    Peter            9811 17-JAN-11 China               0 Japan
    Peter            9877 22-JAN-11 China               0 Japan
    Peter            9875 20-JAN-11 Russia              1 Japan
    Peter            9870 15-JAN-11 Iran                1 Japan
    Peter            9878 26-JAN-11 Korea               0 Japan
    Peter            9854 13-JAN-11 Taiwan              0 Japan
    12 rows selected.

  • How to use BAPI_MATERIAL_AVAILABILITY Function Module....

    Hi Experts,
    I want to know actual free available quantity (ATP Qty.) for mass materials,
    How to use BAPI_MATERIAL_AVAILABILITY function module ?
    I have select all material from MARA and inserted in itab.
    LOOP AT ITAB.
    CALL FUNCTION 'BAPI_MATERIAL_AVAILABILITY'
      EXPORTING
        PLANT                    =
        MATERIAL                 =
        UNIT                     =
      CHECK_RULE               =
      STGE_LOC                 =
      BATCH                    =
      CUSTOMER                 =
      DOC_NUMBER               =
      ITM_NUMBER               =
      WBS_ELEM                 =
      STOCK_IND                =
      DEC_FOR_ROUNDING         =
      DEC_FOR_ROUNDING_X       =
      READ_ATP_LOCK            =
      READ_ATP_LOCK_X          =
    IMPORTING
      ENDLEADTME               =
      AV_QTY_PLT               =
      DIALOGFLAG               =
      RETURN                   =
      TABLES
        WMDVSX                   =
        WMDVEX                   =
    I dont know how to use it.
    pl. guide us.
    Yusuf

    Hi,
           data:  iwmdvsx type table of bapiwmdvs with header line,
           iwmdvex type table of bapiwmdve with header line.
    parameters: p_matnr type mara-matnr,
                p_werks type marc-werks,
                p_meins type mara-meins.
    call function 'BAPI_MATERIAL_AVAILABILITY'
      exporting
        plant            = p_werks
        material         = p_matnr
        unit             = p_meins
      CHECK_RULE       =
      STGE_LOC         =
      BATCH            =
      CUSTOMER         =
      DOC_NUMBER       =
      ITM_NUMBER       =
      WBS_ELEM         =
      STOCK_IND        =
    IMPORTING
      ENDLEADTME       =
      AV_QTY_PLT       =
      DIALOGFLAG       =
      RETURN           =
      tables
        wmdvsx           = iwmdvsx
        wmdvex           = iwmdvex.
    check sy-subrc = 0.
    Text
    ATP information
    Functionality
    Using this function module, you can determine the receipt quantity still available for a particular material in a certain plant according to ATP logic (MRPII).
    The availability check is carried out on transferring the material number, the plant and the input table (WMDVSX). The scope of the check, that is, which stocks, receipts and issues are to be included in the check is defined by the combination of checking group (material master) and cheking rule. In the function module, the system uses the checking rule defined in Sales & Distribution (A). You can overrule this checking rule by using an interface or a user-exit (exit_saplw61v_001). A similar procedure is also valid for the plant parameters. The customer number in the user-exit means that the plant selection can be controlled via the customer. If no plant parameters are given via the interface, the system uses the parameter 'WRK' saved in the user's fixed values.
    The results of the availability check are recorded in the output table (WMDVEX). This table contains dates and available receipt quantities (ATP quantities). The results of the check depends on the following entries:
    If no date and no quantity is transferred, the system displays the ATP situation from today's date into the future as the result.
    If only a date and no quantity is transferred, the system displays the ATP situation from the corresponding date as the result.
    If both a date and a quantity are transferred, the system calculates the availability situation for the quantity specified.
    In the last two cases, the parameter 'DIALOGFLAG' is supplied. This can result in the following:
    ' ' (blank)   <=> quantity completely available
    'X'           <=> only partial quantity available or not available at
                      all
    'N'           <=> Material not included in the availability check
                      (Material not relevant to the availability check)
    The system also displays the end of the replenishment lead time (ENDLEADTME).
    regards

Maybe you are looking for

  • ISE Config Backup Failure - Data filesystem full above threshold

    Hi, Both the config and operational backups were working until earlier last month. Now the config backup is failing with the following error. No configuration or repository settings were changed. ISE 1.2.0.899 Patch 8 - Clustered with persona Node 1

  • Can't get File.copy(target) to work.

    Hi, I'm writing a script where I want to copy a file to another location after the render has finished, but I can't get the File.copy() method to work. Here's a snippet of the code: app.project.renderQueue.render(); var renderedFile = app.project.ren

  • PC Suite - How do I keep it connected/Force connec...

    My PC Suite 7.0.7.0 and Nokia N95 (XP SP2) is working just fine. Everything talks to each other, I have another application which talks to the N95 through PC Suite, also no problem... but :-) Sometimes for some reason unknown to me, PC suite looses c

  • Image Fade! - doesnt want to fade!

    Hi all, Hope someone can help me. I have found a piece of code from Codelifter.com ( http://www.codelifter.com/main/javas...lideshow2.html) enabling me to have a images fading on top of each other in a slideshow effect. However, somewhere down the li

  • Event for not supported sound file type

    Hi, Is there an event or error that is dispatched when a user is trying to load a sound file that is not supported? When I try to load a ".wav" file (that is not supported) I don't get any event or error that indicates the sound load didn't succeed,