How to retrieve 3 max values

Hi ,
can anyone let me know how to retrive the max 3 values from a table...
I want to get the top 3 values of salary from emp table
Thanks,
Praveen

Praveen,
Consider the following example:
SQL> set pagesize 9999
SQL> select empno, ename, sal
  2  from scott.emp;
     EMPNO ENAME             SAL
      7369 SMITH             800
      7499 ALLEN            1600
      7521 WARD             1250
      7566 JONES            2975
      7654 MARTIN           1250
      7698 BLAKE            2850
      7782 CLARK            2450
      7788 SCOTT            3000
      7839 KING             5000
      7844 TURNER           1500
      7876 ADAMS            1100
      7900 JAMES             950
      7902 FORD             3000
      7934 MILLER           1300If you want to get the top three salaries, you can use this query
SQL> select sal
  2  from
  3     (select distinct sal
  4       from scott.emp
  5     order by sal desc)
  6  where  rownum<=3;
       SAL
      5000
      3000
      2975If you want to see all the employees having any of top-3 salaries you can use the following one:
SQL> select empno, ename, sal
  2  from
  3     (select empno, ename, sal, dense_rank() over (order by sal desc) dr
  4      from scott.emp
  5     )
  6  where dr<=3;
     EMPNO ENAME             SAL
      7839 KING             5000
      7788 SCOTT            3000
      7902 FORD             3000
      7566 JONES            2975If you want to get the first 3 employees with the highest salaries. You can use the following one.
But, be aware, there might be more employees having $3000,00 salary.
SQL>  select empno, ename, sal
  2  from
  3     (select empno, ename, sal
  4      from scott.emp
  5     order by sal desc)
  6* where rownum<=3
SQL> /
     EMPNO ENAME             SAL
      7839 KING             5000
      7788 SCOTT            3000
      7902 FORD             3000
SQL>

Similar Messages

  • How to retrieve the values from a table if they differ in Unit of Measure

    How to retrieve the values from a table if they differ in Unit of Measure?

    If no data is read
    - Insure that you use internal code in SELECT statement, check via SE16 desactivating conversion exit on table T006A. ([ref|http://help.sap.com/saphelp_nw70/helpdata/en/2a/fa0122493111d182b70000e829fbfe/frameset.htm])
    If no quanity in result internal table
    - There is no adqntp field in the internal table, so no quantity is copied in itab ([ref|http://help.sap.com /abapdocu_70/en/ABAPINTO_CLAUSE.htm#&ABAP_ALTERNATIVE_1@1@]).
    - - Remove the CORRESPONDING, so quantity will fill the first field adqntp1.  ([ref|http://help.sap.com/abapdocu_70/en/ABENOPEN_SQL_WA.htm])
    - - Then loop at the internal table and move the quantity when necessary to the 2 other fields.
    * Fill the internal table
    SELECT msehi adqntp
      INTO TABLE internal table
      FROM lipso2
      WHERE vbeln = wrk_doc1
        AND msehi IN ('KL','K15','MT').
    * If required move the read quantity in the appropriate column.
    LOOP AT internal_table ASSIGNING <fs>.
      CASE <fs>-msehi.
        WHEN 'K15'.
          <fs>-adqnt2 = <fs>-adqnt1.
          CLEAR <fs>-adqnt1.
        WHEN 'MT'.
          <fs>-adqnt3 = <fs>-adqnt1.
          CLEAR <fs>-adqnt1.
      ENDCASE.
    ENDLOOP.
    - You could also create another table with only fields msehi and adqntp and then collect ([ref|http://help.sap.com/abapdocu_70/en/ABAPCOLLECT.htm]) the data to another table.
    Regards,
    Raymond

  • How to retrieve the value of last identity has been updated in a database?

    how to retrieve the value of last identity has been updated in a database

    Hi,
    Oracle 10g, FGA - Fine grained auditing, supports DML statements (9i only select).
    Set up FGA using the DBMS_FGA.ADD_POLICY procudure:
    sql> BEGIN
    DBMS_FGA.ADD_POLICY (
    policy_name => 'AUD_EMPLOYEE_SAL',
    object_schema => 'HR',
    object_name => 'EMPLOYEE',
    audit_column => SALARY',
    audit_condition => '',
    statement_type => 'UPDATE');
    END;
    NEXT:
    sql> SELECT dbuid, lsqltesxt FROM sys.fga_logs$;
    The database inserts the audit record into the FGA_LOG$ table using an autonomous transaction; even if you roll back the update statement, the update action will still be logged in this table. The fga_log$ tracks the session, machine id, timestamp, schema, scn, etc:
    SQL> desc fga_log$
    Name Null? Type
    SESSIONID NOT NULL NUMBER
    TIMESTAMP# DATE
    DBUID VARCHAR2(30)
    OSUID VARCHAR2(255)
    OSHST VARCHAR2(128)
    CLIENTID VARCHAR2(64)
    EXTID VARCHAR2(4000)
    OBJ$SCHEMA VARCHAR2(30)
    OBJ$NAME VARCHAR2(128)
    POLICYNAME VARCHAR2(30)
    SCN NUMBER
    SQLTEXT VARCHAR2(4000)
    LSQLTEXT CLOB
    SQLBIND VARCHAR2(4000)
    COMMENT$TEXT VARCHAR2(4000)
    PLHOL LONG
    STMT_TYPE NUMBER
    NTIMESTAMP# TIMESTAMP(6)
    PROXY$SID NUMBER
    USER$GUID VARCHAR2(32)
    INSTANCE# NUMBER
    PROCESS# VARCHAR2(16)
    XID RAW(8)
    AUDITID VARCHAR2(64)
    STATEMENT NUMBER
    ENTRYID NUMBER
    DBID NUMBER
    LSQLBIND CLOB
    SQL> spool off

  • How to retrieve 2 values from a table in a LOV

    Hi
    I'm pretty new to APEX. I try to retrieve two values from a table using a LOV. I have a table named DEBIT with then columns SITE, NAME and KEY
    I want to display NAME to the user in a list. When the user select an item from the list, I want to retrieve the data of the SITE and KEY column of this item in order to launch an SQL command based on this two values.
    How to retrieve thes two values whant the user chooses an item from the list ?
    I apologize for my english, being french.
    Regards.
    Christian

    Christian,
    From what I understood about your requirement, you want a 'select list with submit' which displays "NAME" and based on the value selected, you want to get the corresponding values for "SITE" and "KEY" from the table.
    <b>Step 1: Create a select list with submit, say P1_MYSELECT </b><br><br>
    Use something like this in the dynamic list of values for the select list: <br>
    SELECT NAME display_value, NAME return_value
    FROM DEBIT<br><br>
    <b>Step 2: Create a page process of type PL/SQL block. Also create 2 hidden items P1_KEY and P1_SITE. </b><br><br>
    In the PL/sQL, write something like:
    DECLARE
      v_key DEBIT.KEY%TYPE;
      v_site DEBIT.SITE%TYPE;
      CURSOR v_cur_myvals IS
              SELECT KEY, SITE
              FROM DEBIT
              WHERE NAME = :P1_MYSELECT;
    BEGIN
      OPEN v_cur_myvals;
      LOOP
              FETCH v_cur_myvals
              INTO  v_key,v_site;
              EXIT WHEN v_cur_myvals%NOTFOUND;
    :P1_KEY := v_key;   
    :P1_SITE := v_site; 
      END LOOP;
      CLOSE v_cur_myvals;
    END; <br><br>
    Then you can use these values for whatever purpose you need to.
    Hope this helps.

  • How to retrieve the values from a LinkedList

    Hello,
    I have just put this question in java programming forums by mistake...I think that it should be here ...
    I have created a LinkedList to store the results of a query to a database.
    These reasults are decimal numbers and then I want to sum all these numbers to be able to make the average.
    But when I try to retrieve the values of the Linked List I always receive an incopatible types error..
    Here is an extract of my code in a jsp page.
    LinkedList Average = new LinkedList();
    String Media = rst.getString(10);
    Average.add(Media);
    int Size = Average.size();
    double Sum = 0.0;
    for (int i=0; i<=Size; i++)
                    double Result = Average.get(i)     
                  Sum = Sum + Result;
    }If I try to retrieve the value of only one node from the list , I can just putting <%=Average.get(i)%>...but..how can I retrieve all the values (they are decimal numbers) to be able to add them?

    If you want to sum all the values, is there any reason you just don't retrieve the sum from the database rather than the list of values?
    anyway
    List average = new LinkedList();
    while (rst.next()){
      // retrieve the number:
      String mediaString = rst.getString(10);
      Double media = Double.valueOf(mediaString);
      // or maybe like this if it is a number in the database
      Double media = new Double(rst.getDouble(10));
      average.add(media);
    doubleSum = 0.0;
    for (Iterator it = average.iterator(); it.hasNext(); ){
      Double result= (Double) it.next();
      doubleSum += result.doubleValue();
    }

  • How to retrieve constants value from compiled code in jar file?

    Hi everyone,
    I've been looking for a way to solve this for a week now, without any much success... and I've finally decided to ask the Java gurus for a solution! :-)
    Here's what I am basically trying to do:
    I have several jar files in which there are only compiled code (.class).
    In every class, there are 2 constants (declared as static final String) that I would like to retrieve (one is the version and the other the date of the last modification).
    My goal is to print a list of all the classes in the jar files with the values of these 2 constants for each class.
    The solution that I have right now to do this does not work properly: for now, I read all the elements of the classpath, check if these are jar files, and if so, I look into each one and load all the classes one by one and print the results.
    The problem with this is that it uses the method Class.forName(className) and as some classes are unfortunately present in many jar files (2 or 3 copies), once the classes have been loaded, then it won't be "reloaded".
    Without the possibility to "reload" these classes, I cannot see inconsistencies in the versions of the classes present in the jar files.
    I have read many articles, and I thought that I could then use a custom classloader and create a new instance of this classloader for each jar file.
    2 problems with this:
    - according to many posts in the different forums I have read, the jar files should not appear in the CLASSPATH (but this would be easier for me if I could use it...)
    - some classes will not be loaded if some classes (present in other jar files) are not loaded... and this makes things really really complicated to implement...
    So, I thought that I was maybe doing this the wrong way, and that there might be an easy way out of this...
    In fact, I do not need to load the classes... all I need to do, is take a sneak peek at the constants and print their values... and that's it!
    Somehow, I think that this is possible to retrieve the values of compile time constants (declared as static final String) as I can see that with Eclipse (when opening a jar file).
    So, my question is: how can I do that within my java application?
    Or maybe there is another easier solution to do what I need?
    Thanks in advance for your help!

    Hi everyone,
    I've been looking for a way to solve this for a week now, without any much success... and I've finally decided to ask the Java gurus for a solution! :-)
    Here's what I am basically trying to do:
    I have several jar files in which there are only compiled code (.class).
    In every class, there are 2 constants (declared as static final String) that I would like to retrieve (one is the version and the other the date of the last modification).
    My goal is to print a list of all the classes in the jar files with the values of these 2 constants for each class.
    The solution that I have right now to do this does not work properly: for now, I read all the elements of the classpath, check if these are jar files, and if so, I look into each one and load all the classes one by one and print the results.
    The problem with this is that it uses the method Class.forName(className) and as some classes are unfortunately present in many jar files (2 or 3 copies), once the classes have been loaded, then it won't be "reloaded".
    Without the possibility to "reload" these classes, I cannot see inconsistencies in the versions of the classes present in the jar files.
    I have read many articles, and I thought that I could then use a custom classloader and create a new instance of this classloader for each jar file.
    2 problems with this:
    - according to many posts in the different forums I have read, the jar files should not appear in the CLASSPATH (but this would be easier for me if I could use it...)
    - some classes will not be loaded if some classes (present in other jar files) are not loaded... and this makes things really really complicated to implement...
    So, I thought that I was maybe doing this the wrong way, and that there might be an easy way out of this...
    In fact, I do not need to load the classes... all I need to do, is take a sneak peek at the constants and print their values... and that's it!
    Somehow, I think that this is possible to retrieve the values of compile time constants (declared as static final String) as I can see that with Eclipse (when opening a jar file).
    So, my question is: how can I do that within my java application?
    Or maybe there is another easier solution to do what I need?
    Thanks in advance for your help!

  • How to Retrieve VARIABLE VALUES in an Email Definition Form?

    Hi,
    I have created an email definition. I am getting email notifications on pwd expiry, but I am not able to get the values of the variables that I selected under target & variables part of the form.
    for eg. I have been getting emails in the following format:
    Hi <User Profile Information.First Name>
    The Password of User ID <User Profile Information.User Login> has expired.
    I want the values ofg those variables such as "User Profile Information.First Name" to be retrived an put in the email body.
    Can anybody help me as to how and wat settings need to be made to retrieve those values.
    Thanks,
    Abhishek

    Hi Juan,
    do you have an example for me?
    Thanks
    Achim

  • How to retrieve checkboxes values?

    I am using a few checkboxes
    <form method="post" action="testgetPara.jsp">
    <input name="termcond1" type="checkbox" value="1" id="termcond" checked>
    <input name="termcond2" type="checkbox" value="2" id="termcond" >
    <input name="termcond3" type="checkbox" value="3" id="termcond" >
    <input type ="submit" name ="submit" value ="submit">
    </form>
    and hope to retrieve the "checked" values from these boxes
    so i do this
    <%int i = 0;
    //chkno is used only to retrieve the number of checked checkboxes
    String chkno[] = request.getParameterValues("chkdelbook");
                   //using a while loop to retrieve the value + execute it to the database.
    while (i < chkno.length)
    String isbn = request.getParameter("termcond");//get the specific array item
    out.println(isbn);
    //increment the i counter           
    ++i;
    %>
    but it doesn't seem to be working
    it return me with
    An error occurred between lines: 2 and 16 in the jsp file: /testgetPara.jsp
    Generated servlet error:
    C:\jakarta-tomcat-4.0.3\work\localhost\Testing\testgetPara$jsp.java:69: [] can only be applied to arrays. It can't be applied to java.lang.String.
         String isbn = request.getParameter("termcond")[i];//get the specific array item
         ^
    1 error
    this error
    may i know how to solve it?

    but it give me a java.lang.NullPointerException
    i have same id for my checkboxes, different name and
    values for them
    but it just doesnt work.NPE might be because tehere is no parameters named 'hobbies'
    having same ID is not same as having same NAME (see the difference ID vs NAME)
    if you had
    <form>
    <br>java<input type="checkbox" name="hobbies" value="java">
    <br>haskell<input type="checkbox" name="hobbies" value="haskell">
    <br>prolog<input type="checkbox" name="hobbies" value="prolog">
    <input type="submit">
    </form>
    and it would appear something like this:
    java [X]
    haskell[X]
    prolog[_]
    [submit]
    then you could probably write some code for it like this:
    String[] hobbies = request.getParameterValues("hobbies");
    for (int j = 0; j < hobbies.length; j++) {
    %>
    Your hobby is: <%= hobbies[j] %><br>
    <%
    }and it's output would hopefully look somewhat like this:
    Your hobby is: java
    Your hobby is: haskell
    of course i have not tryed that code nore looked methods up from API recently, so it might have some minor bugs.

  • How to get Max value in Essbase

    Hi,
    I have problem to get max value from 3 years in Essbase.
    How can i get max value from Dec 2009,Dec 2010, Dec 2011.
    Suppose value of Dec 2009 = 1000, Dec 2010 = 1500 and Dec 2011 = 2000
    I want to get max value from these three value, how can i do that in Essbase Calculation Script.
    Any idea?
    Thanks.
    Regards,
    Joni

    You have not specified if year and period are separate dimensions, anyway as always there a number of different possbilities and I don't have much time today to think about it but one method could be to use @MAXRANGE
    FIX(Other members to fix on,"Dec")
    "MemberToStoreAgainst" = @MAXRANGE("MemberToFindMaxRangeFor","2009:"2011");
    ENDFIX
    Cheers
    John
    http://john-goodwin.blogspot.com/

  • How to pick max value from a column of a table using cursor and iteration

    Hello Everybody
    I have a table loan_detail
    and a column in it loan_amount
    now i want to pick values from this table using cursor and then by using iteration i want to pick max value from it using that cursor
    here is my table
    LOAN_AMOUNT
    100
    200
    300
    400
    500
    5600
    700i was able to do it using simple loop concepts but when i was trying to do this by using cursor i was not able to do it .
    Regards
    Peeyush

    SQL> SELECT MAX(sal) Highest_Sal,MIN(sal) Lowest_Sal FROM emp;
    HIGHEST_SAL LOWEST_SAL
           5000        800
    SQL> set serverout on
    SQL> DECLARE
      2    TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
      3    sal_tbl tmp_tbl;
      4    CURSOR emp_sal IS
      5      SELECT sal FROM emp;
      6    counter INTEGER := 1;
      7  BEGIN
      8    FOR i IN emp_sal LOOP
      9      sal_tbl(i.sal) := counter;
    10      counter := counter + 1;
    11    END LOOP;
    12    DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
    13    DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
    14  END;
    15  /
    Lowest SAL:800
    Highest SAL:5000
    PL/SQL procedure successfully completed.
    SQL> Even smaller
    SQL> DECLARE
      2    TYPE tmp_tbl IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
      3    sal_tbl tmp_tbl;
      4    CURSOR emp_sal IS
      5      SELECT sal FROM emp;
      6    counter INTEGER := 1;
      7  BEGIN
      8    FOR i IN emp_sal LOOP
      9      sal_tbl(i.sal) := 1;
    10    END LOOP;
    11    DBMS_OUTPUT.put_line('Lowest SAL:' || sal_tbl.FIRST);
    12    DBMS_OUTPUT.put_line('Highest SAL:' || sal_tbl.LAST);
    13  END;
    14  /
    Lowest SAL:800
    Highest SAL:5000
    PL/SQL procedure successfully completed.
    SQL> Edited by: Saubhik on Jan 5, 2011 4:41 PM

  • How to retrieve the values from PL/SQL table types.

    Hi Every one,
    I have the following procedure:
    DECLARE
    TYPE t1 IS TABLE OF emp%ROWTYPE
    INDEX BY BINARY_INTEGER;
    t t1;
    BEGIN
    SELECT *
    BULK COLLECT INTO t
    FROM emp;
    END;
    This procedure works perfectly fine to store the rows of employee in a table type. I am not able to retrieve the values from Pl/SQL table and display it using dbms_output.put_line command.
    Can anybody help me please!!!!!
    Thanks
    Ahmed.

    You mean, you can't add this
    for i in t.first..t.last loop
    dbms_output.put_line(t(i).empno||' '||t(i).ename||' '||t(i).job);
    end loop;or you can't add this
    set serveroutput onor maybe, you are working in third party application where dbms_output is not applicable at all?
    You see, not able like very similar it is not working - both are too vague...
    Best regards
    Maxim

  • How to get max value of a column in VO?

    Hi ,
    i need to find the max value of a column named insured value in VO.
    Please help!!

    Hi,
    If this value is cuming from the Query then u can easily go for MAX function. If its a user enterable value then u need to iterate through all the rows for that column to find the max of them.
    Let me know.
    Regards,
    Gyan

  • How to retrieve hashmap Values in other class within the same package.

    I have created two class ie A and B.
    In A class I have created one hashmap and stored some keys and values.
    But i want to retrieve that values in class B.
    So can any one can give any code ,so that i will try on Eclipse IDE

    Maybe anything. I'm not that interested. It's Java programming 101, about 10 minutes into the first class.

  • How to take max value for this senario

    Hi,
    i have table xyz in which i have column display_code
    for eg.
    display_code in table like
    1. '1100000-999'
    2. '1100000-1000'
    3. '1100000-1001'
    now i have to take max value among 3 display code. my requirement when i take max it will give me '1100000-1001' or the next value
    select max(display_code) from xyz table;
    max(display_code)
    '1100000-999'
    Please help me to fetch max(display_code) = '1100000-1001'
    thanks & regard
    shyam~

    SQL> with xyz as (
      2  select '1100000-999' display_code from dual union
      3  select '1100000-1000' from dual union
      4  select '1100000-1001' from dual
      5  )
      6  --
      7  -- actual query:
      8  --
      9  select display_code
    10  from   xyz
    11  where  to_number(substr(display_code, instr(display_code, '-')+1)) =  ( select max(to_number(substr(display_code, instr(display_code, '-')+1)))
    12                                                                          from   xyz
    13                                                                        );
    DISPLAY_CODE
    1100000-1001or
    SQL> with xyz as (
      2  select '1100000-999' display_code from dual union
      3  select '1100000-1000' from dual union
      4  select '1100000-1001' from dual
      5  )
      6  --
      7  -- actual query
      8  --
      9  select display_code
    10  from   xyz
    11  where  to_number(replace(display_code, '-')) = ( select max(to_number(replace(display_code, '-')))
    12                                                                          from   xyz
    13                                                                        );
    DISPLAY_CODE
    1100000-1001Edited by: hoek on Sep 7, 2010 5:20 PM

  • Scripting: How to retrieve the value for next year

    Hi Experts,
    Need help on HFM rules for retrieving the next year value
    Specificationt: Financial year values start from Apr to Mar
    I am bale to retrieve the values from Apr to dec, when trying to retrieve for Jan and mar getting the wrong values
    using : A# xxx.C1#xxx.y#cur
    please suggest your comments
    Thanks,
    SSr

    Hi,
    Still getting the wrong values
    FYear start from April to Mar
    For ex:
    2012                                                                                    2013
              apr     may     jun     jul     aug     sep     oct     nov     dec   jan     feb     mar
              1        1            1     1       1        1         1        1        1       1       1        1
    YTD
    OT     1          2          3      4       5        6        7        8        9     10     11     12
    Thanks,
    Ssr

  • How to find max value from a list of numbers stored in a varray?

    hi,
    Can any body help me to get the max value from a list of numbers stored in a varray?
    thanks in advance....
    regards
    LaxmiNarsimha

    Yes. Could you post what you have tried before we start helping you in this?

Maybe you are looking for