Display top 5 highest salaries

Hi all..
i need to retrieve the top 5 highest salaries from the emp table please help

An ORDER BY and ROWNUM in the same query don't do what you might think:
SQL> select empno, ename, sal
  2  from emp
  3  where rownum <= 5
  4  order by sal;
               EMPNO ENAME                       SAL
                7369 SMITH                       800
                7521 WARD                       1250
                7654 MARTIN                     1250
                7499 ALLEN                      1600
                7566 JONES                      2975
SQL> select empno, ename, sal
  2  from emp
  3  where rownum <= 5
  4  order by sal desc;
               EMPNO ENAME                       SAL
                7566 JONES                      2975
                7499 ALLEN                      1600
                7654 MARTIN                     1250
                7521 WARD                       1250
                7369 SMITH                       800These are neither the 5 highest or lowest sals.
That's because Oracle assigns rownum before the order by.
I would use dense_rank for this:
SQL> select *
  2  from
  3  (
  4     select empno, ename, sal
  5           ,dense_rank() over (order by sal desc) dr
  6     from   emp
  7     order  by sal desc
  8  )
  9  where dr <= 5;
               EMPNO ENAME                       SAL                   DR
                7839 KING                       5000                    1
                7788 SCOTT                      3000                    2
                7902 FORD                       3000                    2
                7566 JONES                      2975                    3
                7698 BLAKE                      2850                    4
                7782 CLARK                      2450                    5
6 rows selected.Of course, it depends on whether you want to count 3000 twice.
Message was edited by:
SomeoneElse

Similar Messages

  • ABAP Query to display top 5 employees who are having highest salary

    How will you write a query in abap to display top 5 emplyeee records who are having Highest Salary Among all the employees.
    Please Reply...

    Hi Kush,
    Before writing any Database Query, always keep 2 important thumb rules in your mind :
    1. Keep the number of data base access small
    2. Keep the amount of data transfer small.
    As you mentioned the Database Table has only 100 records, you can locally buffer the Table. So, make a local buffering and then you can sort the table based on the salary.
    Select *
      from Table_name
    into table gt_itab.
    Sort gt_itab by salary descending.
    Now you can read the  entries one after the another based on the index.
    Have a look at the following code for an efficient performance feature. Goto SE38 and have a look at the ABAP Examples.
    demo_select_some_columns. Package : SABAPDEMOS
    Hope this will help.
    Thanks,
    Samantak.

  • Display top 10 highest values

    Hi All,
    Please help me on how to achieve this.
    I have a report and a db prompt in my dashboard. Based on the prompt selected and given the GO button, the report gets filtered accordingly.
    Now what i want is , I wanted to filter only the Top 10 values of a particular column in my report when the prompt is given go button.
    For ex, say there is a report with Name, EMp No, Hours Booked and a prompt with dept column.
    When a particular dept is selected from the prompt and given go, the report should display only top highest 10 values of the Hours Booked corresponding to that particular dept and not display everything.
    How can i achieve this?
    Regards
    Gowrish

    Hi,
    Use the TOPN or rank functions.
    TOPN (n_expression, n)
    n_expression is any expression that evaluates to a numerical value.
    The N of TopN which is any positive integer. It Represents the top number of rankings displayed in the result set, 1 being the highest rank.
    ex:- GO-->your column and click fx--->use this function -- TOPN("Hours Booked", 10)
    ex:-2
    Top10 for each year--- fx---> topn(<<col>>,10,by<<col>>)<=10
    Second method.----
    Using rank function:
    case when RANK("F1 Revenue"."1-01 Revenue (Sum All)") <= 10 then RANK("F1 Revenue"."1-01 Revenue (Sum All)"end
    http://total-bi.com/2010/10/obiee-topn-versus-the-rest/
    Award points it is useful.
    Thanks,
    satya
    Edited by: satya R on Apr 27, 2012 7:48 PM
    Edited by: satya R on Apr 27, 2012 9:35 PM

  • How to display top 10 values in a Webi report?

    Hi,
    I have the following requirement.
    If the report contains 'State' and 'Revenue' columns, I need to display top 10 cities with highest revenue and the revenue of the remaining cities can be summed together and should be available in a separate cell.
    I tried several formulae to achieve the requirement but was unsuccessful.
    I created a variable using the formula: '= If (Rank(<State> ,<Sales revenue>)>10 ) Then Sum(<Sales revenue>) Else 0' and added it as a column in the report. This provides the required results.
    When I add this variable as a column in the report, it displays 0 for the first top 10 values and only displays the remaining values. After that we can apply the Sum on this column to get the sum of the remaining (Other) sales revenue excluding top 10 values. However I cannot display this sum in a separate cell.
    Please provide some suggestions on this.
    Regards,
    Aditya Joshi

    Hi Aditya,
    Could you please try the following solutions to resolve the issue.
    Solution1:
    In Oracle, there is a function called: RANK() OVER (PARTITION BY () ORDER BY ()) which can be used to define an object at Universe level to return a rank position when used in a report. If you can check with your DBA - there is a similar function available for SQL Server. It should be RANK() OVER (ORDER BY colname) AS () FROM.
    Solution2:
    Or you can also try returning all rows in query, apply sort at report level & apply alerter to highlight the top 10 values.
    Regards,
    Sarbhjeet Kaur

  • Need to retrieve two highest salaries from table like that 3highest salarie

    Hi,
    I created emp table.I want to retrieview two highest salaries from emp table.
    and also first three highest salaries...Pls can anyone suggest me..i am new to oracle.
    Thanks

    Welcome to the forum.
    What you're looking for is the so called 'Top-N query'.
    It is explained by Tom Kyte in these articles:
    http://www.oracle.com/technetwork/issue-archive/2007/07-jan/o17asktom-093877.html
    http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html
    And Tim Hall also has an article about it:
    http://www.oracle-base.com/articles/misc/TopNQueries.php
    Edited by: hoek on Feb 20, 2012 9:00 PM

  • Problem while displaying top of page in list display

    Hi,
    M facing problem in displaying top of page in list .
    M using following methods:-
    lr_outer_grid TYPE REF TO cl_salv_form_layout_grid,
    lr_inner_grid TYPE REF TO cl_salv_form_layout_grid,
    lr_label TYPE REF TO cl_salv_form_label,
    lr_text TYPE REF TO cl_salv_form_text.
    For displaying the top of page m using event TOP_OF_PAGE.
      CREATE OBJECT lr_outer_grid.
    *Outer grid
      lr_inner_grid = lr_outer_grid->create_grid(
      row = 1
      column = 1 ).
    *... create lable information in inner_grid
    User ID
      lr_label = lr_inner_grid->create_label(
      row = 1
      column = 1
      text = text-h03 ).
    *... create text information in inner_grid
      lr_text = lr_inner_grid->create_text(
      row = 1
      column = 2
      text = sy-uname ).
    *... set label for text
      lr_label->set_label_for( lr_text ).
    But while m going to do same coding for second parameter its not intializing length so adding extra spaces while displaying second row.
    e.g
    USERID       abcdefghjik
    PLANT         jhk            to hjk
    I want that output should look like
    USERID      abcdefghjik
    PLANT        jhk to hjk
    Please tell me the solution what should i do for avoiding extra space

    Hi Neelema,
    You could try this sample code.
    [http://sap-img.com/fu037.htm]
    [http://sap-img.com/abap/sample-alv-heading-in-alv.htm]
    Regards,
    Amit.

  • XLR - How to display top 5 customer or top 5 products?

    I am trying to modify the XL Reporter Dashboards and I am stumped by the following issues:
    Display top 5 customer
    Display top 5 products.
    Could someone please help with this project?
    Your help is appreciated!!
    John

    Hi John,
    The syntax for this is:
    FACT Top 5 BPA(*) Order By Fig.SO_DocTotal Descending
    There is a good document on the SAP portal called XLR Syntax and Functions which covers this and the XLR specific date and period type functions that I found (and still find) very useful.

  • How display top 3 revenues in one block and remaining in another block

    How display top 3 revenues in one block and remaining in another block.

    create a variable as
    v_Rank =Rank([Revenue])
    then create a block level filter on the 2nd block as v_Rank Greater than 3.
    you may need to modify the variable formula as per requirement to get the rank of the row.

  • Display top,middile,bottom in prompt selection

    Hi All,
    i have one requirement like
    I created a prompt values like top,middile,bottom
    When I select top I want to display top 10 rows
    if I select bottom display bottom 31st row to last row rows
    and when I select middile display between 11th row to 30th row
    I have total 30 rows in a column
    i want case stement for the above requirement
    can you please help on this issue
    reg,
    jel

    @Deep, my thoughts too!
    Okay, OP, assuming you really have more than 30 rows, here is a way to look at your data in a very simple format:
    1) Create a Pivot Table in the usual manner with all your attributes, facts, etc.
    2) In the design mode of the Request, select another column from the Selection Pane (it doesn’t matter which because the column will be a “dummy.”) Call the column "Pagination."
    3) Click on the Edit Formula icon. Type the following SQL:
    CASE WHEN RCOUNT(1) <11 THEN ‘1-10’
    WHEN RCOUNT(1) <31 THEN ’11-30
    WHEN RCOUNT(1) >30THEN ’31 - END'
    END
    4) Put the Pagination column in the Page section of the Pivot Table.
    Now your report is divided into the 3 sections. If you want, you could break the report into more sections and if you insist on calling the sections "Top," "Middle," and "Bottom," you can replace the labels above with those words.
    You don't even need a dashboard prompt at all!

  • What's the price for a MacBook Pro Retina Display Top Case with battery in the US?

    I'd like to know what´s the price for a MacBook Pro Retina Display Top Case with battery in the US.

    Here you go.

  • Certain site displays top half of screen on FireFox but all on Explorer 8

    The URL below only displays top 50% of screen on Firefox 3.6.3 but 100% of screen on Explorer 8. To see comparison, go to my website at: http://bobenglish.com/ff052110.xls and open the file in Microsoft Excel.
    == URL of affected sites ==
    http://www.harmls.com/default.aspx?showStartPage=true

    http://www.harmls.com/default.aspx?showStartPage=true
    That page says to use Internet Explorer, and it also says "Tempo is no longer accessible from this page. ..."

  • How to display top n records(sorted by no of defects) within date range

    I would like to display top n production orders(sorted by defects linked with each order) within the date range entered by the user.
    Please advice.
    I have 3 parameters to my report:
    1. n(n is no of top records to be displayed)
    2. From date
    3. To date
    Appreciate quick response.
    Regards,
    Hari

    if you use the parameters in the report by placing them in the record selection it should display the records within your date range.
    if {date field} in ?start to ?end then true

  • Want to know top 3 salaries from emp_table

    hello every body....I've a simple table managing the records of employees in an office, its columns r as given:
    Emp_Id     //Employeed ID
    Emp_name          //Employee Name
    Dept_name     //Department Name
    salary
    Thep problem I'm facing is that I wanna know the Department wise Average Salary and Department wise top 3 salaries but I'm unable to retrieve both the things (Average & top 3 salaries) in a single query. is there any body who can write this query for me??? thank u very much.

    You should play with ROW_NUMBER, RANK and DENSE_RANK to see the differences between the three. They all produce similar, but not neccessarily identical results.
    Given:
    SQL> SELECT * FROM t;
          DEPT     SALARY
            10       1000
            10       2000
            10       3000
            10       4000
            10       5000 Then all three will give the same results:
    SQL> SELECT salary, rn
      2  FROM (SELECT salary, ROW_NUMBER() OVER (PARTITION BY dept
      3                                          ORDER BY salary DESC) rn
      4        FROM t)
      5  WHERE rn <= 3
        SALARY         RN
          5000          1
          4000          2
          3000          3
    SQL> SELECT salary, rn
      2  FROM (SELECT salary, RANK() OVER (PARTITION BY dept
      3                                    ORDER BY salary DESC) rn
      4        FROM t)
      5  WHERE rn <= 3;
        SALARY         RN
          5000          1
          4000          2
          3000          3
    SQL> SELECT salary, rn
      2  FROM (SELECT salary, DENSE_RANK() OVER (PARTITION BY dept
      3                                    ORDER BY salary DESC) rn
      4        FROM t)
      5  WHERE rn <= 3;
        SALARY         RN
          5000          1
          4000          2
          3000          3Now, if we create some ties:
    SQL> INSERT INTO t values(10,3000);
    1 row created.
    SQL> INSERT INTO t values(10,4000);
    1 row created.
    SQL> INSERT INTO t values(10,5000);
    1 row created.
    SQL> COMMIT;What do we get now?
    SQL> SELECT salary, rn
      2  FROM (SELECT salary, ROW_NUMBER() OVER (PARTITION BY dept
      3                                          ORDER BY salary DESC) rn
      4        FROM t)
      5  WHERE rn <= 3
        SALARY         RN
          5000          1
          5000          2
          4000          3
    SQL> SELECT salary, rn
      2  FROM (SELECT salary, RANK() OVER (PARTITION BY dept
      3                                    ORDER BY salary DESC) rn
      4        FROM t)
      5  WHERE rn <= 3;
        SALARY         RN
          5000          1
          5000          1
          4000          3
          4000          3
    SQL> SELECT salary, rn
      2  FROM (SELECT salary, DENSE_RANK() OVER (PARTITION BY dept
      3                                    ORDER BY salary DESC) rn
      4        FROM t)
      5  WHERE rn <= 3;
        SALARY         RN
          5000          1
          5000          1
          4000          2
          4000          2
          3000          3
          3000          3Which one is correct? As Laurent said, you need clarification from your users.
    TTFN
    John

  • How to display top 10 customers in FR

    Hi,
    I've a requirement in FR. When i select an entity from POV, it should display top 10 customers out of 100 customers in that entity (for ex. top revenue customers). Can any one pls. help on how we can achive this dynamic reporting through FR. The rank function displays the ranks, but what i would like to see is, the report should display top 20 customers only. Pls. help
    Thanks,
    PVR

    Hi,
    I am not sure what version you are, from version 9.3 I think there is a "Top" function, just highlight the row, tick the option for "Top", set the number and the column
    Failing that if you need to use rank could you not hide the column.
    Cheers
    John
    http://john-goodwin.blogspot.com/

  • How to display top Position Level using conditions / Exception Aggregation

    Hello,
    I have the following problem where I need to list out the row with the highest POSITION LEVEL (with reference to BUSINESS PARTNER). I have tried out all possibilities including Conditions and Exception Aggregation (MAX) but nothing seems to work. Conditions work to an extent i.e. it shows the top position level but unfortunately for the entire query result i.e. only one row is displayed in the result (the first row to satisfy the condition) instead of all the rows for each Business Partner. POSITION LEVEL is a KEY FIGURE value. It also exists as a CHARACTERISTIC under a diferent name in the cube. How can I solve this? Pls help.
    This is how the report looks at present...
    First Name     Last Name     Business Partner Nr.     Date of Birth     Postalcode     City     Position Level
    Testfallini     Enzweiler     3000000020     21.10.1990     63674     New York     1
    Tamara     Dimmer     3000179274     10.09.1987     54689     Chicago     2
    Tamara     Dimmer     3000179274     10.09.1987     54689     Chicago     1
    Tamara     Dimmer     3000179274     10.09.1987     54689     Chicago     1
    Thu-Ha     Tran     3000069951     25.12.1988     93047     Atlanta     2
    Thu-Ha     Tran     3000069951     25.12.1988     93047     Atlanta     1
    This is how the report looks when I use conditions...
    First Name     Last Name     Business Partner Nr.     Date of Birth     Postalcode     City     Position Level
    Tamara     Dimmer     3000179274     10.09.1987     54689     Chicago     2
    This is how the report should look...
    First Name     Last Name     Business Partner Nr.     Date of Birth     Postalcode     City     Position Level
    Testfallini     Enzweiler     3000000020     21.10.1990     63674     New York     1
    Tamara     Dimmer     3000179274     10.09.1987     54689     Chicago     2
    Thu-Ha     Tran     3000069951     25.12.1988     93047     Atlanta     2
    Thanks in advance,
    SD

    Hi Sebastian,
    I thought you need to display all the records with the highest position level at top. But now I realize your requirement.
    It should only display only the maximum position level for each level.
    You can try using the Condition for the position level KF, with Top N operator and value = '1'
    And then go to char assignement tab in the exception and select Most Detail Chars along Rows/Individual Chars. and combinations for the required combination of Chars.
    [List Condition for All Characteristics in Drilldown Independent|http://help.sap.com/saphelp_nw04/helpdata/en/86/dfc405ab60524ea0d3e89db15fb2e7/content.htm]
    [Defining Conditions |http://help.sap.com/saphelp_nw04/helpdata/en/73/702e39074dc93de10000000a114084/frameset.htm]
    Hope this helps....
    Rgs,
    Ravikanth.

Maybe you are looking for