How to Get Top 10 records using SQL select statement.

:-) Hiee E'body,
I want to generate a sql report in which I only want the top 10 records based on the numeric value of a column.
For Example,
To generate a Location Wise, Employee Wise Years of Service Report.
Here I need the Top 10 Employees according to their No. of Years of Service for all the existing locations using a single query.
eg.
Location Emp No. YOS
India - 22 30
212 28
819 24 ...
US 123 40
312 33
90 33
144 30 ...
UK 77 20
79 20
331 18
109 16 ...
Every Location should display their respective Top 10 Employees
regarding their No. of Years of Service.
Please let me the know the solution to this.
Have a nice day.
Thanking You,
Vivek Kapoor.

For example if the table contained (India rows only shown) :
India 202 30
India 212 28
India 819 24
India 820 24
India 900 20
India 920 18
India 922 17
India 925 16
India 926 15
India 927 14
India 928 13
India 929 13
India 930 12
do you want to see
India 202 30
India 212 28
India 819 24
India 820 24
India 900 20
India 920 18
India 922 17
India 925 16
India 926 15
India 927 14
or
India 202 30
India 212 28
India 819 24
India 820 24
India 900 20
India 920 18
India 922 17
India 925 16
India 926 15
India 927 14
India 928 13
India 929 13
Also if the India rows were
India 202 30
India 212 30
India 819 30
India 820 30
India 900 30
India 920 30
India 922 30
India 925 30
India 926 30
India 927 30
India 928 30
India 929 30
do you want to see
India 202 30
India 212 30
India 819 30
India 820 30
India 900 30
India 920 30
India 922 30
India 925 30
India 926 30
India 927 30
or
India 202 30
India 212 30
India 819 30
India 820 30
India 900 30
India 920 30
India 922 30
India 925 30
India 926 30
India 927 30
India 928 30
India 929 30
Please clarify.
Thanks,
Partha

Similar Messages

  • Trying to get newest record using a select statement

    Hello everyone.. I'm a bit new at this but I've been teaching myself by trial and error so bear with me.
    Using a 'DEDUCTION' table for this example, I am trying to get it to resolve the record that is most current. Since each employee record could have changes, the table could have multiple entries/deductions of which only one is the most current.
    Ok, so medical deductions are like DEDUCTIONS 700-715 while dental deductions are 730-737. Initially, I was trying to figure out how to pull the deductions based on an employee and have it compare which is newer and report the newest one for each medical and then the dental deduction. If there is not a type of deduction for someone, it should report zero like a NVL I think. See example:
    EMP_ID DEDUCTIONS EFF_DATE AMT
    1 710 2000/12/01 85.39
    1 735 2000/12/01 8.31
    9 701 2001/08/29 12
    9 707 2000/12/01 12
    9 730 2000/12/01 .23
    10 700 2001/02/25 .46
    10 730 2001/02/25 .23
    The record that stands out in this example is employee #9 with two medical deduction, one that supersedes the other - 701 and 707. Because 701 is newer it should ignore the 707 deduction.
    Any ideas? Thank you!
    ken

    Here are a couple of methods. The second one should run faster, but requires Oracle 8i.
    SQL> SELECT   emp_id, deductions, eff_date, amt,
      2           'MEDICAL' AS type
      3  FROM     deduction
      4  WHERE    (emp_id, eff_date) IN
      5           (SELECT   emp_id, MAX (eff_date)
      6            FROM     deduction
      7            WHERE    deductions BETWEEN 700 AND 715
      8            GROUP BY emp_id)
      9  AND      deductions BETWEEN 700 AND 715
    10  UNION
    11  SELECT   emp_id, deductions, eff_date, amt,
    12           'DENTAL' AS type
    13  FROM     deduction
    14  WHERE    (emp_id, eff_date) IN
    15           (SELECT   emp_id, MAX (eff_date)
    16            FROM     deduction
    17            WHERE    deductions BETWEEN 730 AND 737
    18            GROUP BY emp_id)
    19  AND      deductions BETWEEN 730 AND 737
    20  ORDER BY emp_id, deductions
    21  /
        EMP_ID DEDUCTIONS EFF_DATE          AMT TYPE
             1        710 2000/12/01      85.39 MEDICAL
             1        735 2000/12/01       8.31 DENTAL
             9        701 2001/08/29         12 MEDICAL
             9        730 2000/12/01        .23 DENTAL
            10        700 2001/02/25        .46 MEDICAL
            10        730 2001/02/25        .23 DENTAL
    6 rows selected.
    SQL> SELECT   emp_id, deductions, eff_date, amt,
      2           'MEDICAL' AS type
      3  FROM     (SELECT   emp_id, deductions, eff_date, amt,
      4                     RANK () OVER
      5                          (PARTITION BY emp_id
      6                           ORDER BY     eff_date DESC) AS rk
      7            FROM     deduction
      8            WHERE    deductions BETWEEN 700 AND 715)
      9  WHERE    rk = 1
    10  UNION
    11  SELECT   emp_id, deductions, eff_date, amt,
    12           'DENTAL' AS type
    13  FROM     (SELECT   emp_id, deductions, eff_date, amt,
    14                     RANK () OVER
    15                          (PARTITION BY emp_id
    16                           ORDER BY     eff_date DESC) AS rk
    17            FROM     deduction
    18            WHERE    deductions BETWEEN 730 AND 737)
    19  WHERE    rk = 1
    20  ORDER BY emp_id, deductions
    21  /
        EMP_ID DEDUCTIONS EFF_DATE          AMT TYPE
             1        710 2000/12/01      85.39 MEDICAL
             1        735 2000/12/01       8.31 DENTAL
             9        701 2001/08/29         12 MEDICAL
             9        730 2000/12/01        .23 DENTAL
            10        700 2001/02/25        .46 MEDICAL
            10        730 2001/02/25        .23 DENTAL
    6 rows selected.

  • How to get multiple records using fn-bea:execute-sql()

    Hi,
    I created Proxy service(ALSB3.0) to get records from DB table. I have used Xquery function(fn-bea:execute-sql()). Using simple SQL query I got single record, but my table having multiple records. Please suggest how to get multiple records using fn-bea:execute-sql() and how to assign them in ALSB variable.
    Regards,
    Nagaraju
    Edited by: user10373980 on Sep 29, 2008 6:11 AM

    Hi,
    Am facing the same issue stated above that I couldnt get all the records in the table that am querying in the Proxyservice.
    For example:
    fn-bea:execute-sql('EsbDataSource', 'student', 'select Name from StudentList' ) is the query that am using to fetch the records from the table called StudentList which contains more than one records like
    Id Name
    01 XXX
    02 YYY
    03 ZZZ
    I tried to assign the result of the above query in a variable and while trying to log the variable, I can see the below
    <student>
    <Name>XXX</Name>
    </student>
    I want to have all the records from my table in xml format but it's not coming up. I get the value only from the first row of my table.
    Please suggest.
    regards,
    Venkat

  • How to get this output using sql query?

    Hi,
      How to get this output using sql query?
    Sno Name Age ADD Result
    1 Anil 23 delhi Pass
    2 Shruti 25 bangalor Pass
    3 Arun 21 delhi fail
    4 Sonu 23 pune Pass
    5 Roji 26 hydrabad fail
    6 Anil 28 delhi pass
    Output
    Sno Name Age ADD Result
    1 Anil 23 delhi pass
    28 delhi pass

    Hi Vamshi,
    Your query is not pretty clear.
    write the select query using Name = 'ANIL' in where condition and display the ouput using Control-break statements.
    Regards,
    Kannan

  • How to get all records using Invoke-webrequest?/Why Invoke-webrequest returns only first 2000 Records?

    invoke-webrequest content returning only 2000 records though it has around 4000 records in web api.
    The same url if I give in excel oData Data feed I am getting all the records.
    See the below script
    Script:
    $QueryResult= (Invoke-WebRequest -Uri $ODataURI -UseDefaultCredentials)
    [xml]$xmlResult=$QueryResult.content
    foreach($obj in $xmlResult.feed.entry.content.properties)
    $Name=$obj.Name;
    $IsAvail=$obj.isAvail.'#text';
    $PGroup=$obj.PGroup
    I am exporting the above result as a CSV file and my CSV file contains only 2000 records. 
    But,  $xmlResult.feed.Count --> it Shows 4000 Records.
    The same Odata url if I give in excel oData Data feed I am getting all the 4000 records.
    So Please help me how can I get all the records using power shell.
    Thanks
    A Pathfinder..
    JoSwa
    If a post answers your question, please click &quot;Mark As Answer&quot; on that post and &quot;Mark as Helpful&quot;
    Best Online Journal

    Hi Jo Swa(K.P.Elayaraja)-MCP,
    Would you please also post code which is used to export the records?
    In addition, to use the cmdlet invoke-RestMethod to work on ODate feeds, please refer to this article:
    Interacting with TechEd NA 2012 Schedule using PowerShell v3
    I hope this helps.

  • How to insert the records using sql query

    hi,
    i insert the record uisng sql query and DOTNET programme successfully and increase the doc num .ubut when i try to  add record using SAP B1 the old Doc no show.It means its not consider the docnums which are not inserted by sql query.
    how can i solve this problem?
    Regards,
    M.Thippa Reddy

    You are not support use Insert / Update / Delete on SAP Databases directly.
    SAP will not support any database, which is inconsistent, due to SQL-Queries, which modify datasets or the datastructure of the SAP Business One Database. This includes any update-, delete- or drop-statements executed via SQL-Server Tools or via the the query interface of SAP Business One
    Check SAP Note: 896891 Support Scope for SAP Business One - DB Integrity
    [https://websmp130.sap-ag.de/sap(bD1lbiZjPTAwMQ==)/bc/bsp/spn/smb_searchnotes/display.htm?note_langu=E&note_numm=896891]

  • How to get next record using next  button

    my database is very large and i awnt to display results 1 page at a time by keeping next button.so when i click next i should get next record.
    help me out

    Re-direct this post under relevant Developer Tools Forums.
    http://forums.oracle.com/forums/category.jspa?categoryID=19
    Regards,
    Sabdar Syed.

  • How to get partition key using SQL

    I want to get the subpartition key of a Rang-List partition table in program.
    How to write the SQL.
    Thanks.

    Can you query against dba_tab_partitions & dba_part_key_columns?
    Edit: Misread the question, didnt see SUBpartitions. More coffee, less posting for me.
    Edited by: mrk on Dec 22, 2011 9:10 AM

  • How to get top CPU consuming sql oracle 10g

    I can see the top sql by running ASH report. but there is too much infomation. is there a way I can just display TOP SQL section, and filter out other parts? this way I can use it in a shell script, and loop through the database.
    or is there a query can do the ASH job?
    thanks.
    Karl

    Hi,
    What you will do if the EM console bug present?
    As a best pratice, i will do like below
    1) execute the TOP command and you see process consuming high CPU
    2)
    SQL> oradebug setospid <&OSPid>
    SQL> oradebug tracefile_name --This will list the name of tracefile with location
    SQL> oradebug unlimit
    SQL> oradebug Event 10046 trace name context forever, level 12;
    Let it run for 20 mins around
    SQL> oradebug Event 10046 trace name context off;
    SQL> exit
    3) Tkprof with explain plan
    4) check any plan change for that sql
    Kind Regards,
    Rakesh jayappa

  • How to pass datetype parameter in Sql Select Statements to

    public void Mountain()
                   String dat=lastcollecteddatestr.substring(0,2);
                   String mon=lastcollecteddatestr.substring(3,5);
                   String yr=lastcollecteddatestr.substring(6,10);
                   try
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy"+"MM"+"dd");
                        java.util.Date utilDate = new java.util.Date(Integer.parseInt(yr)-1900,Integer.parseInt(mon),Integer.parseInt(dat));
                        Date newdate=sdf.parse(yr+"-"+mon+"-"+dat);
                        System.out.println(newdate);
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        Connection con = DriverManager.getConnection(url);
                        Statement stat = con.createStatement();
                        ResultSet rs=stat.executeQuery("select empcd,date from mmm where date = '"+newdate+"' ");
                        while(rs.next())
                        String str1= new String(rs.getString("empcd"));
                        String str2= new String(rs.getString("date"));
                        System.out.println(str1+"\t"+str2);
                        }catch(Exception ex){System.out.println(ex);}
                   Error : [MicroSoft] [ODBC Visual FoxPro Driver]Operator/operand type mismatch.
                   myNote: There is no Problem with connecting to database of something like that
                        its working well with Different SQL Statment
                        like: ("select empcd,date from mmm where empcd = '"+empcdstr+"' ")
                        This is not Working and giving errors
                        like: ("select empcd,date from mmm where date = '"+newdate+"' ")
                        HOW TO SOLVE THIS PLZ HELP. It's Urgent.

    You will see this error if the search condition was entered with an invalid or missing relational operator.
    You need to include a valid relational operator such as
      =, !=, ^=, <>, >, <, >=, <=, ALL, ANY, [NOT] BETWEEN, EXISTS, [NOT] IN, IS [NOT] NULL, or [NOT] LIKE in the condition. in the sql statement.
    Can you throw some more light on how are you designing your project?

  • How to get Listener Information using PL/SQL code

    How to get Listener Information using PL/SQL code

    user2075318 wrote:
    How to get Listener Information using PL/SQL codeThis approach (somewhat of a hack) can be used - but it does not really provide meaningful data at application layer.
    SQL> create or replace function TnsPing( ipAddress varchar2, port number default 1521 ) return varchar2 is
      2          type THexArray is table of varchar2(2);
      3          --// tnsping packet (should be 10g and 11g listener compatible)
      4          TNS_PING_PACKET constant THexArray := new THexArray(
      5                  '00', '57', '00', '00', '01', '00', '00', '00',
      6                  '01', '39', '01', '2C', '00', '00', '08', '00',
      7                  '7F', 'FF', '7F', '08', '00', '00', '01', '00',
      8                  '00', '1D', '00', '3A', '00', '00', '00', '00',
      9                  '00', '00', '00', '00', '00', '00', '00', '00',
    10                  '00', '00', '00', '00', '00', '00', '00', '00',
    11                  '00', '00', '00', '00', '00', '00', '00', '00',
    12                  '00', '00', '28', '43', '4F', '4E', '4E', '45',
    13                  '43', '54', '5F', '44', '41', '54', '41', '3D',
    14                  '28', '43', '4F', '4D', '4D', '41', '4E', '44',
    15                  '3D', '70', '69', '6E', '67', '29', '29'
    16          );
    17 
    18          socket  UTL_TCP.connection;
    19          txBytes number;
    20          rxBytes number;
    21          rawBuf  raw(1024);
    22          resp    varchar2(1024);
    23  begin
    24          socket := UTL_TCP.open_connection(
    25                          remote_host => ipAddress,
    26                          remote_port => port,
    27                          tx_timeout => 10
    28                  );
    29 
    30          --// convert hex array into a raw buffer
    31          for i in 1..TNS_PING_PACKET.Count loop
    32                  rawBuf := rawBuf || HexToRaw( TNS_PING_PACKET(i) );
    33          end loop;
    34 
    35          --// send packet
    36          txBytes := UTL_TCP.write_raw( socket, rawBuf, TNS_PING_PACKET.Count  );
    37 
    38          --// read response
    39          rxBytes := UTL_TCP.read_raw( socket, rawBuf, 1024 );
    40 
    41          UTL_TCP.close_connection( socket );
    42 
    43          --// convert response to varchar2
    44          resp := UTL_RAW.Cast_To_Varchar2( rawBuf );
    45 
    46          --// strip the header from the response and return the text only
    47          return( substr(resp,13) );
    48  end;
    49  /
    Function created.
    SQL>
    SQL> select tnsping( '10.251.93.30' ) as TNSPING from dual;
    TNSPING
    (DESCRIPTION=(TMP=)(VSNNUM=169869568)(ERR=0)(ALIAS=LISTENER))
    SQL> select tnsping( '10.251.95.69' ) as TNSPING from dual;
    TNSPING
    (DESCRIPTION=(TMP=)(VSNNUM=0)(ERR=0)(ALIAS=LISTENER))
    SQL>

  • How to get latest record on top of the result list

    Hi Gurus,
    How to get latest record on top of the result list when you open the record.
    saved data method in BT120H_CPL of OverView page and result list in ICCMP_INBOX.
    Regards,
    Ravi

    Hi
    Try sort descending by on fileld "changed at ".
    manipulate the sort depends on your requirement
    Regards
    Logu

  • How to use variables in an sql select statement.

    Hi, I have seen examples using the update, but nothing using a select statement.
    I have four variables I am getting from drop down menus in my JSP I set the user selected items to strings.
    How would I create the select statement like this:
    .String XName = request.getParameter("UserInputX");
    rsInResult = stmtInResult.executeQuery("SELECT ColumxX FROM TableZ WHERE ColumnX = XName")Obviously it tries to read "XName" as a column and of course can't find it, how would I set it up to have it see the Value of XName and not the literal XName.

    read this:
    rsInResult = stmtInResult.executeQuery("SELECT ColumxX FROM TableZ WHERE ColumnX = '"+XName+"')
    {code}
    better way is PreparedStatement:
    {code}
         // example code to change password to an user in the USERS table
         String newPWD = "my password";
         String user = "luxjava";
         PreparedStatement prepStat;
         String updatePWD = "UPDATE LOGISTIC.USERS "+
                                  "SET logistic.users.password = ? "+
                                  "WHERE logistic.users.username = ?";
         prepStat = ql.conDb.prepareStatement(updatePWD);
         prepStat.setString(1, newPwd);
         prepStat.setString(2, user);
         int zeroNotFound = prepStat.executeUpdate();
         if (zeroNotFound==0)
              ql.conDb.rollback();
              prepStat.close();
         else
              ql.conDb.commit();
              prepStat.close();
    {code}
    ... use the SQLException to capture errors                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • I use iPhoto and want to know how to get the 'key photo' i select on my MacBook Pro to be the one that gets used on my iPhone when i sync them. Any ideas? also, i want the events to appear in the order i choose on my MacBook too

    I use iPhoto and want to know how to get the 'key photo' i select on my McBook Pro to be the same one that gets used on my iPhone 5s when i sync. i also want the events to appear on the iPhone in the same order i have them on my macbook. any ideas there too?

    Killerfinch wrote:
    My new yahoo account nestles comfortably in iCloud on the mine iPad.
    No, your Yahoo account is not in iCloud (which only handles iCloud mail), it is in Yahoo, and the Yahoo mail account is on your iPad.
    But the MacBook Pro will have none of it! I write this question now as I fear that I will be totally demented very soon and unable to formulate my thoughts clearly!
    Get the correct settings for your account from Yahoo and set it up manually.
    By the way, I also find the "password" issue problematical. It seems Apple want my Apple password rather than my eMail password. All very confusing.
    That would depend on what you are trying to do.

  • How to get the value from a function using a select statement

    I have a function(user defined not built in) that returns multiple values(like an array). My question is how do i get those values in a select statement. when i tried to retrieve it,
    select pack.my_members from dual;
    i am getting an error
    ORA-00902: invalid datatype
    I am sure this must be a syntax error with the select statement
    The following is the function that give the array of data
    package pack
    package spec
    Type my_table is table of varchar2(25);
    function the_members
    return pack.my_table;
    pakcage body
    function the_members return pack.my_table
    Remarks: This function returns a table containing names of the
    members
    is
    tm pack.my_table:= pack.my_table('first member','second member','third member','fourth member');
    begin
    return tm;
    end the_members;

    Check this example on Pipelinedfunction

Maybe you are looking for