What is Top-N analysis

What is Top-N analysis.?? can some one explain. I encountered this while going thru
the Sql book
The ORDER BY clause in the subquery is not
needed unless you are performing Top-N analysis.

That means you are doing something like this in order to get top five paid people::
SELECT * FROM
(  SELECT empno, ename, sal+nvl(comm,0) as remuneration
   FROM   emp
   ORDER BY remuneration DESC )
WHERE rownum <= 5
/We have to do the ORDER BY in a sub-query because rownum gets applied before the rows are sorted.
Cheers, APC

Similar Messages

  • Top n analysis using hierarchial queries

    hi all,
    can we do top n analysis in hierarchial queries using level pseudo columns. if so please give an example.
    thanks and regards,
    sri ram.

    Hi,
    Analytic functions (such as RANK) often interfere with CONNECT BY queries. Do one of them in a sub-query, and the other in a super-query, as shown below.
    If you do the CONNECT BY first, use ROWNUM (which is assigned after ORDER SIBLINGS BY is applied) to preserve the order of the CONNECT BY query.
    WITH     connect_by_results     AS
         SELECT     LPAD ( ' '
                   , 3 * (LEVEL - 1)
                   ) || ename          AS iname
         ,     sal
         ,     ROWNUM               AS r_num
         FROM     scott.emp
         START WITH     mgr     IS NULL
         CONNECT BY     mgr     = PRIOR empno
         ORDER SIBLINGS BY     ename
    SELECT       iname
    ,       sal
    ,       RANK () OVER (ORDER BY sal DESC)     AS sal_rank
    FROM       connect_by_results
    ORDER BY  r_num
    ;Output:
    INAME                  SAL   SAL_RANK
    KING                  5000          1
       BLAKE              2850          5
          ALLEN           1600          7
          JAMES            950         13
          MARTIN          1250         10
          TURNER          1500          8
          WARD            1250         10
       CLARK              2450          6
          MILLER          1300          9
       JONES              2975          4
          FORD            3000          2
             SMITH         800         14
          SCOTT           3000          2
             ADAMS        1100         12 
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and the results you want from that data. If you use only commonly available tables (such as those in the scott or hr schemas), then you don't have to post any sample data; just post the results.
    Explain how you get those results from that data.
    Always say what version of oracle you're using.

  • Performing Top-n Analysis (but per group)

    The Oracle University Guide SQL Volume 2 says:
    To perform Top-n Analysis the general syntax is
    SELECT [column_list], ROWNUM
    FROM (SELECT [column_list]
    FROM table
    ORDER BY Top-N_column)
    WHERE ROWNUM <= N;
    for example
    To display the top three earner names and salaries
    from the EMPLOYEES table.
    SELECT ROWNUM as RANK, last_name, salary
    FROM (SELECT last_name,salary FROM employees
    ORDER BY salary DESC)
    WHERE ROWNUM <= 3;
    or to display the four most senior employees in the company.
    SELECT ROWNUM as SENIOR,E.last_name, E.hire_date
    FROM (SELECT last_name,hire_date FROM employees
    ORDER BY hire_date)E
    WHERE rownum <= 4;
    but what about if I have groups? for example if I want to display the 3 top earners per department?
    In my case now
    I want to fetch the top 4 items per category, with the biggest quantity (posothta)
    SELECT ROWNUM as RANK,
    H.KATHG_EIDOYS,
    H.KATHG_EIDOYS_DESCR,
    H.EIDOS,
    H.EIDOS_DESCR,
    H.CODE_SUP_BASIKOS_NAME,
    H.RAFI_CODE,
    H.LINES,
    H.POSOTHTA
    from (
    SELECT B.KATHG_EIDOYS,
    D.DESCRIPTION KATHG_EIDOYS_DESCR,
    B.CODE EIDOS,
    B.DESCRIPTION EIDOS_DESCR,
    S.NAME CODE_SUP_BASIKOS_NAME,
    C.RAFI_CODE,
    COUNT(A.FLD_SEQ_NUM) LINES,
    nvl(SUM(decode(k.INV_APOGRAFH_FLAG,'0', decode(k.INV_EXAGOGH_POSOTHTA,1, a.POSOTHTA_TIMOLOGHSHS,2,-a.POSOTHTA_TIMOLOGHSHS))),0) POSOTHTA
    FROM ERP_EIDOI_ANA_RAFI C,
    ERP_KODIKOI_KINHSHS K,
    ERP_POLHSEIS_DETAILS A,
    ERP_SUP_CUST S,
    ERP_KATHG_EIDON D,
    ERP_EIDH b
    WHERE B.COMPANY = DECODE(1,1,'9',B.COMPANY)
    AND a.COMPANY_KK=K.COMPANY
    AND a.KK_CODE=K.CODE
    and A.company_WAREHOUSE = c.COMPANY_WARE(+)
    and A.MASTER_WAREHOUSE = c.MASTER_WARE_CODE(+)
    and A.CODE_WAREHOUSE = c.DETAIL_WARE_CODE(+)
    and A.COMPANY_EIDOS = c.COMPANY_EIDOS(+)
    and A.EIDOS = c.CODE_EIDOS (+)
    AND C.DEFAULT_FLAG (+)= 1
    AND b.code = a.EIDOS
    and b.company = a.COMPANY_EIDOS
    AND D.CODE= B.KATHG_EIDOYS
    AND D.COMPANY= B.COMPANY_KATHG_EIDOYS
    AND B.COMPANY_SUP_BASIKOS = S.COMPANY
    AND B.CODE_SUP_BASIKOS = S.CODE
    AND B.PROMHTHEYTHS_FLAG_BASIKOS = S.PELATHS_PROMHTHEYTHS_FLAG
    AND /*&p_where*/
    a.COMPANY='9' and (a.group_source) = '10' and (A.MASTER_WAREHOUSE) = '01' and (A.CODE_WAREHOUSE) = '0101' and (a.hmerom_parast) >= to_date('01/01/2006','dd/mm/rrrr') and (a.hmerom_parast) <= to_date('25/05/2006','dd/mm/rrrr')
    GROUP BY B.KATHG_EIDOYS, D.DESCRIPTION, B.CODE, B.DESCRIPTION, S.NAME,C.RAFI_CODE
    ORDER BY 8 DESC
    ) H
    where 1=1 and ROWNUM <= 4
    this select does not bring me the desired results, because if for example
    category 01 has 10 items
    and category 02 has 2 items,
    this select will bring me only the first four rows
    and not the items from the 02 category.
    If you understand what is the case I will wait for your replies.
    Thanks in advance

    Hi,
    Here is an example. It gives you customers ids with highest salary per department.
    SELECT CUSTOMER_ID, SALARY, RANK, DEPARTMENT
    FROM (SELECT CUSTOMER_ID, SALARY, DEPARTMENT,
    RANK() OVER (PARTITION BY DEPARTMENT ORDER BY SALARY DESC) AS RANK
    FROM TABLEA)
    WHERE RANK < 2;
    Peter D.

  • What is Short Dump Analysis and  secendry index  ?

    Dear Experts .
    1.) What is purpose of T-codes SE30 and ST22 ?
    What is Short Dump Analysis ?
    2.) What is secendry index , How to use it ? How it effects the Performance of a report ?
    Please it is urgent ...
    Regards : Rajneesh

    Hi
    A dump analysis is a comprehensive list that should enable you to identify the causes and possible solutions of program errors. The ABAP Workbench generates a short dump whenever a report or transaction terminates due to a serious error. The system enters the error in the system log and writes a snapshot of the program at the moment when it terminated into a special database table called SNAP.
    Dump analyses give the user or programmer information about the causes of the error that has caused the program to terminate. Experienced users can use them to identify very quickly where and why this occurred. He or she can them solve the problem.
    The snapshot contains the following information:
    Why the program has terminated
    What caused the program termination
    Where in the program code the termination occurred
    What you can do to correct the error
    The values of the relevant system fields when the program terminated
    The calls or events that were active when the program terminated
    Any other programs that are affected.
    http://help.sap.com/saphelp_nw70/helpdata/en/c6/617d0ce68c11d2b2ab080009b43351/content.htm
    Index: Technical key of a database table.
    Primary index: The primary index contains the key fields of the table and a pointer to the non-key fields of the table. The primary index is created automatically when the table is created in the database.
    Secondary index: Additional indexes could be created considering the most frequently accessed dimensions of the table.
    Structure of an Index
    An index can be used to speed up the selection of data records from a table.
    An index can be considered to be a copy of a database table reduced to certain fields. The data is stored in sorted form in this copy. This sorting permits fast access to the records of the table (for example using a binary search). Not all of the fields of the table are contained in the index. The index also contains a pointer from the index entry to the corresponding table entry to permit all the field contents to be read.
    When creating indexes, please note that:
    An index can only be used up to the last specified field in the selection! The fields which are specified in the WHERE clause for a large number of selections should be in the first position.
    Only those fields whose values significantly restrict the amount of data are meaningful in an index.
    When you change a data record of a table, you must adjust the index sorting. Tables whose contents are frequently changed therefore should not have too many indexes.
    Make sure that the indexes on a table are as disjunctive as possible.
    (That is they should contain as few fields in common as possible. If two indexes on a table have a large number of common fields, this could make it more difficult for the optimizer to choose the most selective index.)
    Accessing tables using Indexes
    The database optimizer decides which index on the table should be used by the database to access data records.
    You must distinguish between the primary index and secondary indexes of a table. The primary index contains the key fields of the table. The primary index is automatically created in the database when the table is activated. If a large table is frequently accessed such that it is not possible to apply primary index sorting, you should create secondary indexes for the table.
    The indexes on a table have a three-character index ID. '0' is reserved for the primary index. Customers can create their own indexes on SAP tables; their IDs must begin with Y or Z.
    If the index fields have key function, i.e. they already uniquely identify each record of the table, an index can be called a unique index. This ensures that there are no duplicate index fields in the database.
    When you define a secondary index in the ABAP Dictionary, you can specify whether it should be created on the database when it is activated. Some indexes only result in a gain in performance for certain database systems. You can therefore specify a list of database systems when you define an index. The index is then only created on the specified database systems when activated

  • Top n Analysis using correlated subquery

    Please explain this query. It is doing top n analysis using correlated subquery. I need explaination of execution of this query.
    Select distinct a.sal
    From emp a
    where 1=(select count ( distinct b.sal) from emp b
         where a.sal <=b.sal)
    Thanks in advance

    Try breaking the query down and rewriting it in order to follow the logic;
    SQL> --
    SQL> -- Start by getting each salary from emp along with a count of all salaries in emp
    SQL> --
    SQL> select   a.sal,
            (select count (distinct b.sal) from scott.emp b ) count_sal
    from scott.emp a
    order by 1 desc
           SAL  COUNT_SAL
          5000         12
          3000         12
          3000         12
          2975         12
          2850         12
          2450         12
          1600         12
          1500         12
          1300         12
          1250         12
          1250         12
          1100         12
           950         12
           800         12
    14 rows selected.
    SQL> --
    SQL> --Add a condition to the count for only salaries below or equal to the current salarySQL> --
    SQL> select   a.sal,
            (select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) rank_sal
    from scott.emp a
    order by 1 desc
           SAL   RANK_SAL
          5000          1
          3000          2
          3000          2
          2975          3
          2850          4
          2450          5
          1600          6
          1500          7
          1300          8
          1250          9
          1250          9
          1100         10
           950         11
           800         12
    14 rows selected.
    SQL> --
    SQL> -- Add a condition to only pick the nth highest salary
    SQL> --
    SQL> select    a.sal,
             (select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) rank_sal
    from scott.emp a
    where (select count (distinct b.sal) from scott.emp b where a.sal <=b.sal) = 4
           SAL   RANK_SAL
          2850          4
    1 row selected.Hope this helps.

  • Top-n analysis in oracle 8i

    How to use top-n analysis in oracle 8i?
    I mean,take a example.
    I am maintaining a database of a 1000 employees.I want to display the names of the employees who are getting top 10 salaries(more further top 100 salaries) using a SQL query in oracle 8i only.Please answer my problem.

    Sorry, my suggestion will return 10 emp with highest salaries, not all employees with 10 highest salaries. To get all employees with 10 highest salaries in 8i:
    SQL> SELECT  ename,
      2          sal
      3    FROM  emp
      4    WHERE sal IN (
      5                  SELECT  sal
      6                    FROM  (
      7                           SELECT  sal
      8                             FROM  emp
      9                             GROUP BY sal
    10                             ORDER BY sal DESC
    11                          )
    12                    WHERE rownum <= 10
    13                 )
    14  /
    ENAME             SAL
    KING             5000
    FORD             3000
    SCOTT            3000
    JONES            2975
    BLAKE            2850
    CLARK            2450
    ALLEN            1600
    TURNER           1500
    MILLER           1300
    MARTIN           1250
    WARD             1250
    ENAME             SAL
    ADAMS            1100
    12 rows selected.
    SQL> SY.

  • Performing Top-N Analysis

    An Oracle University Material in Sql Says
    The high-level structure of a Top-N analysis query is:
    SELECT [column_list], ROWNUM
    FROM (SELECT [column_list]
    FROM table
    ORDER BY Top-N_column)
    WHERE ROWNUM <= N;
    For example to display the top three earner names and salaries from the EMPLOYEES table:
    SELECT ROWNUM as RANK, last_name, salary
    FROM (SELECT last_name,salary FROM employees
    ORDER BY salary DESC)
    WHERE ROWNUM <= 3;
    My question is
    If, instead of this query, I write
    1)
    SELECT ROWNUM as RANK, last_name, salary
    FROM employees
    WHERE ROWNUM <= 3
    ORDER BY salary DESC
    or
    2)
    SELECT ROWNUM as RANK, last_name, salary
    FROM ( SELECT last_name,salary
    FROM employees
    WHERE ROWNUM <= 3
    ORDER BY salary DESC
    is any difference?
    The results in schema hr are the same.............
    Thank you

    is any difference? yes, there is!
    SQL> with t as (select 1 num from dual union all
      2             select 4 from dual union all
      3             select 3 from dual union all
      4             select 2 from dual)
      5             --
      6  SELECT ROWNUM as RANK, num
      7    FROM (SELECT  num FROM t ORDER BY num desc)
      8   WHERE ROWNUM <= 3;
          RANK        NUM
             1          4
             2          3
             3          2
    SQL>
    SQL> with t as (select 1 num from dual union all
      2             select 4 from dual union all
      3             select 3 from dual union all
      4             select 2 from dual)
      5  --
      6  SELECT ROWNUM as RANK, num
      7    FROM t
      8   WHERE ROWNUM <= 3
      9   ORDER BY num DESC
    10  /
          RANK        NUM
             2          4
             3          3
             1          1
    SQL>
    SQL> with t as (select 1 num from dual union all
      2             select 4 from dual union all
      3             select 3 from dual union all
      4             select 2 from dual)
      5  --
      6  SELECT ROWNUM as RANK, num
      7    FROM (SELECT num FROM t
      8          WHERE ROWNUM <= 3
      9          ORDER BY num DESC)
    10  /
          RANK        NUM
             1          4
             2          3
             3          1
    SQL> rownum is confered before ordering is made.
    Thats why you should place the subquery with ordering in the inline view.

  • TOP N analysis with same values

    Dear Members,
    Suppose we have the following data in the table Student.
    Sname GPA
    Jack 4.0
    Smith 3.7
    Rose 3.5
    Rachel 3.5
    Ram 2.8
    I have seen many questions in this forum which gives good queries for TOP N analysis. But in my case those are not working.
    There are total 5 students. I should write a query which should take an input and should give the students with top gpa as output in desc order.
    Suppose if i give 4 as input i must get 4,3.7,3.5,3.5,2.8Gpa's since we have 2 gpa's which are same. Suppose i give 3 as the input i must get 4,3.7,3.5 and 3.5 GPA's.
    The query must consider the GPA's which are same as one not different. How can we achive this. i.e the top three students (suppose input is 3) must be
    Jack 4.0
    Smith 3.7
    Rose 3.5
    Rachel 3.5
    It must also include Rachel.
    Any help is greatly appreciated.
    Thanks
    Sandeep

    SQL> select * from test;
    NAME                        GPA
    Jack                          4
    Smith                       3.7
    Rose                        3.5
    Rachel                      3.5
    Ram                         2.8
    SQL> select name,gpa
      2  from
      3    (select name,gpa,dense_rank() over(order by gpa desc) rn
      4     from test)
    5 where rn <= 3
      6  order by rn;
    NAME                        GPA
    Jack                          4
    Smith                       3.7
    Rose                        3.5
    Rachel                      3.5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • What are the GAP analysis? could you pls... explain with examples?

    hi everybody.....
    may i know what are the GAP analysis with examples?
    thanks in advance...
    and hope u will get it me that.

    Dear arunteja vasan ,
    GAP Analysis
    A through gap analysis will identify the gaps between how the business operates ad its needs against what the package can can't do.   For each gap there will be one of three outcomes which must be recorded and actioned, GAP must be closed and customised software can be developed close the gap, GAP must be closed but software cannot be written therefore a workaround is required, GAP does not need to be closed.
    In simple terms: Gap means small cracks. In SAP world.  In information technology, gap analysis is the study of the differences between two different information systems or applications( ex; existing system or legacy system with Client and new is SAP), often for the purpose of determining how to get from one state to a new state. A gap is sometimes spoken of as "the space between where we are and where we want to be." Gap analysis is undertaken as a means of bridging that space.
    Actual gap analysis is time consuming and it plays vital role in blue print stage.
    Cut Over Plans
    Detailed plans need to be developed for cutting over from the old system(s) to the new.   Parallel runs of what will happen over the conversion period using test data, convert and watch for a period after wards to ensure nothing unexpected happens.
    Train Users
    Well trained users will support and defend the system on site.  Unsupportive users will continually undermine the system and eventually it will be replaced.   Therefore the more effort you put into helping the users master the system early the better.
    Do award points if you found them useful.
    Regards,
    Rakesh

  • What is top level class declaration in java?

    What is top level class declaration in java?

    The declaration of a class that's not nested inside any other class.

  • What is the Statistical Analysis

    Hi,
    What is the Statistical Analysis(I need Tcode and What would i check in this) and Indexes Analysis(I need Tcode and What would i check in this) and RSRV also.
    Please do the needful for me.
    Thanks

    Hi Gali,
    Basically statistical analysis is done for the system design purpose.
    you can use transaction DB02 where you can check for how much space does your object needs to store all the data based on the storage type it is going to use.
    Suppose your scheduling option is From PSA and then data target,then from this stats you can determine how much space your data in BW will take.
    You can go to option "Detailed analysis" in this transaction and give your object name and there you have various options to get the information on like compression,Index type.
    Basically it gives you an idea about all the database aspect of your system and your object.
    In RSRV is bcaically to check the data cosistency of your objects like cubes ODS, Mater data where you can correct the SID's of your object.So after your developments and data loads and you just need to check the data conistency through RSRV.
    Hope this helps.
    If helpful please assign points
    Message was edited by:
            Ajeet Kumar Singh

  • What is Road map Analysis, Issue tracking,, Monitoring, Reporting?

    Hi
    Friends
    what is Road map Analysis, Issue tracking,, Monitoring, Reporting?
    can any one clear my doubts in detail full points will be given

    Dear Venkat,
    Your questions are very high level questions.
    Roadmap Analysis: This term is generally used in SAP Implementation where we compare the project status with the SAP roadmap. Generally in ASAP Roadmap, we have SAP specified standard deliverables, and there can be custom deliverables as per the client's requirements. If we need to do an analysis of custom deliverables viz standard deliverables or standard roadmap viz custom roadmap, we call it as roadmap analysis.
    Issue Tracking: Very broad level term - can be in Implementation as well as support projects. Issues are either support problems or implementation problems which are documented and recorded for solutioning and approval. These are generally steering committee terms used in a project.
    Monitoring: Again a broad level term. Any watch process can be termed as monitoring. Eg: Monitoring of Backup; Monitoring of System health check etc.
    Reporting: Again a broad level term. It is the same as defined in the english dictionary. Nothing related to any process in SAP with technical solution in mind.
    Regards,
    Anirban

  • What are top-level workitems

    what are top-level workitems,i could see this check box in swwl tcode

    Any single task is a top-level item, and in addition any workflow work items that are not subworkflows are top-level items. It is explained <a href="http://help.sap.com/saphelp_nw2004s/helpdata/en/15/7fbf2e2e18d411b1320060b03c6bfb/content.htm">at help.sap.com</a>
    All work items are of course found in SWWIHEAD - not just the top-level items. Since both single tasks (type B or W) and multistep tasks (type F) can be top-level items you must identify them by their lack of a parent item. Specify WI_CHCKWI = (initial), that will return only top-level items.

  • Top N Analysis with multiple columns

    Hi
    I am using Oracle 9i. I do have a table which contains datewise promotional material types for an organisation.
    The structure is as follows:
    CREATE TABLE TEST
    (CDATE DATE,
    BROCHURE VARCHAR2(1),
    WEBSITE VARCHAR2(1),
    DIRECT_MAIL VARCHAR2(1),
    PRESS_RELEASE VARCHAR2(1),
    JOURNAL_AD VARCHAR2(1)
    and the sample data is as follows:
    CDate          Brochure     Website     Direct_Mail     Press_Release Journal_Ad
    01/04/1996     Y Y Y N N
    02/04/1996     Y Y N N N
    23/06/1996     Y N Y Y N
    13/09/1996     Y Y N N N
    01/04/1997     Y Y N N N
    02/04/1997     Y Y Y N Y
    23/06/1997     N Y N N Y
    13/09/1997     Y Y N N N
    01/04/1998     Y Y Y N N
    02/04/1998     Y N N Y N
    23/06/1998     N Y N N Y
    13/09/1998     Y Y N N Y
    01/04/1999     Y Y Y N Y
    02/04/1999     Y N N Y N
    23/06/1999     N Y N N N
    13/09/1999     Y Y Y N N
    I want to have year wise top 4 promotional types in terms of count of 'Y' only. The result should be like as follows:
    YEAR:1996
    TYPE     COUNT
    BROCHURE 4
    WEBSITE 3
    DIRECT_MAIL 2
    PRESS_RELEASE 1
    JOURNAL_AD 0
    YEAR:1997
    TYPE     COUNT
    WEBSITE 4
    BROCHURE 3
    JOURNAL_AD 2
    DIRECT_MAIL 1
    PRESS_RELEASE 1
    Please suggest a solution for the same. I am not able to sort it for multiple columns.
    Regards
    MS

    One of the questions that must be asked when you have a requirement to only show the top N ranked items in a list, is "what about a tie in the ranking?".
    Oracle has two ranking functions that allow you to deal with either requirement - RANK and DENSE_RANK. Both operate as either analytic or aggregate functions, so either will work for your requirements. The previous posting by Miguel demonstrated how to decode your Y/N flags and pivot the data.
    In this example, I've taken the liberty of adding some data to year 2000 that will show the difference between RANK and DENSE_RANK as well as how to use them to filter your results.
    First, here's the decoded/pivoted data:
    SQL>WITH test AS
      2  (         SELECT TO_DATE('01/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      3  UNION ALL SELECT TO_DATE('02/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      4  UNION ALL SELECT TO_DATE('23/06/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      5  UNION ALL SELECT TO_DATE('13/09/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      6  UNION ALL SELECT TO_DATE('01/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      7  UNION ALL SELECT TO_DATE('02/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
      8  UNION ALL SELECT TO_DATE('23/06/1997','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
      9  UNION ALL SELECT TO_DATE('13/09/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    10  UNION ALL SELECT TO_DATE('01/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    11  UNION ALL SELECT TO_DATE('02/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    12  UNION ALL SELECT TO_DATE('23/06/1998','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    13  UNION ALL SELECT TO_DATE('13/09/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    14  UNION ALL SELECT TO_DATE('01/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    15  UNION ALL SELECT TO_DATE('02/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    16  UNION ALL SELECT TO_DATE('23/06/1999','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    17  UNION ALL SELECT TO_DATE('13/09/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    18  UNION ALL SELECT TO_DATE('01/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    19  UNION ALL SELECT TO_DATE('02/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    20  UNION ALL SELECT TO_DATE('23/06/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    21  UNION ALL SELECT TO_DATE('13/09/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    22  )
    23  SELECT cyear
    24        ,ctype
    25        ,RANK()       OVER (PARTITION BY cyear ORDER BY num_media DESC) ranking
    26        ,DENSE_RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) dense_ranking
    27  FROM (SELECT TRUNC(CDATE,'Y') CYEAR
    28             ,'BROCHURE' CTYPE, SUM(DECODE(BROCHURE, 'Y', 1, 0)) NUM_MEDIA
    29        FROM test
    30        GROUP BY TRUNC(CDATE,'Y')
    31        UNION ALL
    32        SELECT TRUNC(CDATE,'Y') CYEAR
    33             ,'WEBSITE' CTYPE, SUM(DECODE(WEBSITE, 'Y', 1, 0))
    34        FROM test
    35        GROUP BY TRUNC(CDATE,'Y')
    36        UNION ALL
    37        SELECT TRUNC(CDATE,'Y') CYEAR
    38             ,'DIRECT_MAIL' CTYPE, SUM(DECODE(DIRECT_MAIL, 'Y', 1, 0))
    39        FROM test
    40        GROUP BY TRUNC(CDATE,'Y')
    41        UNION ALL
    42        SELECT TRUNC(CDATE,'Y') CYEAR
    43             ,'PRESS_RELEASE' CTYPE, SUM(DECODE(PRESS_RELEASE, 'Y', 1, 0))
    44        FROM test
    45        GROUP BY TRUNC(CDATE,'Y')
    46        UNION ALL
    47        SELECT TRUNC(CDATE,'Y') CYEAR
    48             ,'JOURNAL_AD' CTYPE, SUM(DECODE(JOURNAL_AD, 'Y', 1, 0))
    49        FROM test
    50        GROUP BY TRUNC(CDATE,'Y')
    51       )
    52* order by cyear desc, ranking
    53  /
    CYEAR                         CTYPE             RANKING DENSE_RANKING
    01-Jan-2000 00:00:00          BROCHURE                1             1
    01-Jan-2000 00:00:00          WEBSITE                 1             1
    01-Jan-2000 00:00:00          DIRECT_MAIL             3             2
    01-Jan-2000 00:00:00          PRESS_RELEASE           4             3
    01-Jan-2000 00:00:00          JOURNAL_AD              5             4
    01-Jan-1999 00:00:00          BROCHURE                1             1
    01-Jan-1999 00:00:00          WEBSITE                 1             1
    01-Jan-1999 00:00:00          DIRECT_MAIL             3             2
    01-Jan-1999 00:00:00          PRESS_RELEASE           4             3
    01-Jan-1999 00:00:00          JOURNAL_AD              4             3
    01-Jan-1998 00:00:00          BROCHURE                1             1
    01-Jan-1998 00:00:00          WEBSITE                 1             1
    01-Jan-1998 00:00:00          JOURNAL_AD              3             2
    01-Jan-1998 00:00:00          DIRECT_MAIL             4             3
    01-Jan-1998 00:00:00          PRESS_RELEASE           4             3
    01-Jan-1997 00:00:00          WEBSITE                 1             1
    01-Jan-1997 00:00:00          BROCHURE                2             2
    01-Jan-1997 00:00:00          JOURNAL_AD              3             3
    01-Jan-1997 00:00:00          DIRECT_MAIL             4             4
    01-Jan-1997 00:00:00          PRESS_RELEASE           5             5
    01-Jan-1996 00:00:00          BROCHURE                1             1
    01-Jan-1996 00:00:00          WEBSITE                 2             2
    01-Jan-1996 00:00:00          DIRECT_MAIL             3             3
    01-Jan-1996 00:00:00          PRESS_RELEASE           4             4
    01-Jan-1996 00:00:00          JOURNAL_AD              5             5You can see that in year 2000 there is a tie for first place (ranking #1). The RANK function will name the second highest count 3 (skipping the rank of 2 due to the tie), while the DENSE_RANK function will not skip a ranking.
    Now, to filter on the ranking, wrap your query in another in-line view like this - but use which ever ranking function YOUR requirements call for:
    SQL>WITH test AS
      2  (         SELECT TO_DATE('01/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      3  UNION ALL SELECT TO_DATE('02/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      4  UNION ALL SELECT TO_DATE('23/06/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      5  UNION ALL SELECT TO_DATE('13/09/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      6  UNION ALL SELECT TO_DATE('01/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
      7  UNION ALL SELECT TO_DATE('02/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
      8  UNION ALL SELECT TO_DATE('23/06/1997','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
      9  UNION ALL SELECT TO_DATE('13/09/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    10  UNION ALL SELECT TO_DATE('01/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    11  UNION ALL SELECT TO_DATE('02/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    12  UNION ALL SELECT TO_DATE('23/06/1998','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    13  UNION ALL SELECT TO_DATE('13/09/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    14  UNION ALL SELECT TO_DATE('01/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    15  UNION ALL SELECT TO_DATE('02/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    16  UNION ALL SELECT TO_DATE('23/06/1999','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    17  UNION ALL SELECT TO_DATE('13/09/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    18  UNION ALL SELECT TO_DATE('01/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
    19  UNION ALL SELECT TO_DATE('02/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    20  UNION ALL SELECT TO_DATE('23/06/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    21  UNION ALL SELECT TO_DATE('13/09/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
    22  )
    23  SELECT * FROM (
    24      SELECT cyear
    25            ,ctype
    26            ,RANK()       OVER (PARTITION BY cyear ORDER BY num_media DESC) ranking
    27            ,DENSE_RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) dense_ranking
    28      FROM (SELECT TRUNC(CDATE,'Y') CYEAR
    29                 ,'BROCHURE' CTYPE, SUM(DECODE(BROCHURE, 'Y', 1, 0)) NUM_MEDIA
    30            FROM test
    31            GROUP BY TRUNC(CDATE,'Y')
    32            UNION ALL
    33            SELECT TRUNC(CDATE,'Y') CYEAR
    34                 ,'WEBSITE' CTYPE, SUM(DECODE(WEBSITE, 'Y', 1, 0))
    35            FROM test
    36            GROUP BY TRUNC(CDATE,'Y')
    37            UNION ALL
    38            SELECT TRUNC(CDATE,'Y') CYEAR
    39                 ,'DIRECT_MAIL' CTYPE, SUM(DECODE(DIRECT_MAIL, 'Y', 1, 0))
    40            FROM test
    41            GROUP BY TRUNC(CDATE,'Y')
    42            UNION ALL
    43            SELECT TRUNC(CDATE,'Y') CYEAR
    44                 ,'PRESS_RELEASE' CTYPE, SUM(DECODE(PRESS_RELEASE, 'Y', 1, 0))
    45            FROM test
    46            GROUP BY TRUNC(CDATE,'Y')
    47            UNION ALL
    48            SELECT TRUNC(CDATE,'Y') CYEAR
    49                 ,'JOURNAL_AD' CTYPE, SUM(DECODE(JOURNAL_AD, 'Y', 1, 0))
    50            FROM test
    51            GROUP BY TRUNC(CDATE,'Y')
    52           )
    53      )
    54  where RANKING <= 4
    55* order by cyear desc, ranking
    56  /
    CYEAR                         CTYPE             RANKING DENSE_RANKING
    01-Jan-2000 00:00:00          WEBSITE                 1             1
    01-Jan-2000 00:00:00          BROCHURE                1             1
    01-Jan-2000 00:00:00          DIRECT_MAIL             3             2
    01-Jan-2000 00:00:00          PRESS_RELEASE           4             3
    01-Jan-1999 00:00:00          BROCHURE                1             1
    01-Jan-1999 00:00:00          WEBSITE                 1             1
    01-Jan-1999 00:00:00          DIRECT_MAIL             3             2
    01-Jan-1999 00:00:00          JOURNAL_AD              4             3
    01-Jan-1999 00:00:00          PRESS_RELEASE           4             3
    01-Jan-1998 00:00:00          BROCHURE                1             1
    01-Jan-1998 00:00:00          WEBSITE                 1             1
    01-Jan-1998 00:00:00          JOURNAL_AD              3             2
    01-Jan-1998 00:00:00          PRESS_RELEASE           4             3
    01-Jan-1998 00:00:00          DIRECT_MAIL             4             3
    01-Jan-1997 00:00:00          WEBSITE                 1             1
    01-Jan-1997 00:00:00          BROCHURE                2             2
    01-Jan-1997 00:00:00          JOURNAL_AD              3             3
    01-Jan-1997 00:00:00          DIRECT_MAIL             4             4
    01-Jan-1996 00:00:00          BROCHURE                1             1
    01-Jan-1996 00:00:00          WEBSITE                 2             2
    01-Jan-1996 00:00:00          DIRECT_MAIL             3             3
    01-Jan-1996 00:00:00          PRESS_RELEASE           4             4

  • Top N analysis..

    .Don't plz send the links ... plz provide with sme solution ...
    ....Suppose ..i want to find TOP 3 earners of the Company so tht i can do .by using ..Inline vies.......
    ...My query is ..how to find ...the THIRD highest Earner of the Company .......
    The Table is Employees ..Which is located in HR schema.....

    I don't understand what you are trying to say, sorry.
    My point is:
    If you know how to get the top 3 earners:
    SQL> get t1
      1  select v.*, rownum rn
      2  from   ( select *
      3           from   emp
      4           order by sal desc
      5         ) v
      6* where rownum <= 3
    SQL> /
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO         RN
          7839 KING       PRESIDENT            17-NOV-81       5000                    10          1
          7788 SCOTT      ANALYST         7566 09-DEC-82       3000                    20          2
          7902 FORD       ANALYST         7566 03-DEC-81       3000                    20          3
    SQL> then it is easy to select only the 3rd row from this result set:
    SQL> get t2
      1  select *
      2  from   (
      3           select v.*, rownum rn
      4           from   ( select *
      5                    from   emp
      6                    order by sal desc
      7                  ) v
      8           where rownum <= 3
      9         ) v2
    10* where v2.rn >= 3
    SQL> /
         EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO         RN
          7902 FORD       ANALYST         7566 03-DEC-81       3000                    20          3
    SQL> So I can not get your point.
    Message was edited by:
    Leo Mannhart

Maybe you are looking for

  • Ipod photo stuck in disk mode and screen is in Black and white

    came to use my ipod photo after about a week of not using it. when i took it out the colour screen was in black and white and the screen is stuck to saying it is in disk mode with a large tick. the ipod will mount and does show up in itunes it will e

  • Ipad air 2 touch id not working when plugged in

    A very strange problem tonight. After work, I grabbed my iPad Air 2 which was plugged in to the Apple Charger next to my chair.  Touched the home button to open it - nothing happened.  Tried again.  Nothing happened.  Entered the code and the IPad Ai

  • File Save As Not In Menu

    Hi I transferred my data and apps from my Snow Leopard (upgraded from Tiger) MacBook Pro to my new iMac and am gradually testing to see how things work, that my data is intact and so on.  I haven't tried any non-Apple applications yet, but in both Pr

  • How to install oracle 9i jdeveloper

    hi i just download 9i jdeveloper from here. it is a 135MB jdev9i_902.zip file. however i couldn't find the setup file to install. Is there any instruction that i can follow to install oracle 9i jdeveloper and use it for Oracle9i interMedia/BC4J Inter

  • Interface Not Working as I Thought

    I thought I understood interfaces pretty well, but I've run across a problem that makes me think otherwise. I defined the following interface: public interface OffsetObject { Offset getOffset(); void setOffset(Offset offset); }Then I implemented it: