Getting the first row of a result set

I need to get the first row returned from a select query that looks like this:
SELECT DISTINCT parm1 FROM table1 WHERE parm1 > 10 ORDER BY parm1
Can someone please show me how to do this?
Alex

Because you're only getting one row you don't need to bother with a DISTINCT clause. You'll need to order your data first and then nab the first row from that result set.
SELECT parm1
  FROM (SELECT   parm1
            FROM table1
           WHERE parm1 > 10
        ORDER BY parm1)
WHERE ROWNUM <= 1;

Similar Messages

  • Getting the first row for each group

    Hi Everyone,
    I have a query which returns a number of rows, all of which are valid. What I need to do is to get the first row for each group and work with those records.
    For example ...
    client flight startairport destairport stops
    A fl123 LGW BKK 2
    A fl124 LHR BKK 5
    B fl432 LGW XYZ 7
    B fl432 MAN ABC 8
    .... etc.
    I would need to return one row for Client A and one row for Client B (etc.) but find that I can't use the MIN function because it would return the MIN value for each column (i.e. mix up the rows). I also can use the rownum=1 because this would only return one row rather than one row per group (i.e. per client).
    I have been investigating and most postings seem to say that it needs a second query to look up the first row for each grouping. This is a solution which would not really be practical because my query is already quite complex and incorporating duplicate subqueries would just make the whole thing much to cumbersome.
    So what I really new is a "MIN by group" or a "TOP by group" or a "ROWNUM=1 by group" function.
    Can anyone help me with this? I'm sure that there must be a command to handle this.
    Regards and any thanks,
    Alan Searle
    Cologne, Germany

    Something like this:
    select *
    from (
       select table1.*
       row_number() over (partition by col1, col2 order by col3, col4) rn
       from table1
    where rn = 1In the "partition by" clause you place what you normally would "group by".
    In the "order by" clause you define which will have row_number = 1.
    Edit:
    PS. The [url http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions004.htm#i81407]docs have more examples on using analytical functions ;-)
    Edited by: Kim Berg Hansen on Sep 16, 2011 10:46 AM

  • To get the first row

    hi all,
    i have query like
    select container,size,serial
    from containers
    order by serialin the above query for the same container and size there might be more than one serial.out of that i have to sort it by serial and fetch the first row.
    can we fetch with row num, before fetching rownum =1 we should order it first?
    Please help me..

    So for a given container (say :b1) and size (say :b2), you want to retrieve the row with the smallest serial value?
    I think this should work then:
    select *
    from (select container,size,serial
          from containers
          where container = :b1
            and size = :b2
          order by serial)
    where rownum = 1

  • Getting the first row from a ResultSet??

    I have a query that returns a set of values but am interested only in the first value.How can I get that. I tried to use getArray method but that gives a "java.sql.SQLException: Exhausted Resultset".
    Any suggestions??

    I think I'm having the same problem with you.
    this is my method:
    private Vector getData( ResultSet rs){
    String str = "";
    try{
    while( rs.next() ){
    str = rs.getString( 1 );
    rows.addElement( str );
    }catch( SQLException sqlex ){
    sqlex.printStackTrace();
    return rows;
    I don' t know if you meant this.hoping this can help you

  • How can I get Numbers to return the first row in a group of VLOOKUP query results instead of last one?

    I'm trying to query from one table (call it Table1) a batch of rows in another table (Table2) using VLOOKUP on a date specified in the first table (Table1). My problem is it's returning the last incident in Table2 of the requested date instead of the first incident. This really breaks the OFFSET scheme I'd like to use to collect the rest of the items for that date. Is there some way to compel VLOOKUP to return the first row of query results, not the last?
    NOTE: I see I've asked this before, but forgot to go back and look at responses given. It's been a while and I've limped along until now with the way things were. I'm actually trying to delete this questions, so if you see it, ignore it. I suppose if someone can tell me real quick how to delete a stupid question, that might be helpful.

    you cannot delete a post yourself.  You can flag the post an request a moderator remove.

  • How to highlight the first row of a table

    When entering the form, how do I get the first row of the table to be highlighted when it has not been selected yet? There is already data in the table. Thanks.
    Allyson

    Any ideas on what would cause this NOT to work? I added this row right after the table is created and populated and it is still not highlighted when I enter the form. The snippet of code that does this is:
    MyTableModel modelS = (MyTableModel)dataModel.get("S");
    jTable1 = new JTable(modelS);
    jTable1.setRowSelectionInterval(0,0);MyTableModel is this:
    class MyTableModel extends DefaultTableModel {
      public MyTableModel(Object[] columnNames, int numRows) {
        super(columnNames, numRows);
      public boolean isCellEditable(int row, int column) {
        if (TableKey == 'S') {
          if (column == 6) {
            return false;
        if (TableKey == 'O') {
          if (column == 0 || column == 4 || column == 5) {
            return false;
        if (TableKey == 'P') {
          return false;
      return true;
    }Can anyone help? Thanks.
    Allyson

  • Getting the first record

    I have a table containing infor about employees promotion details.Problem is that the table was so badly designed that the only way for me to know the first promotion that an employee got in that year was the way it was inserted.Hence below for Manohar ,91 was the first promotion in the year 2001 and
    for Sanohar it is 81.
    Wondering is there sql query throught which I can get hold of the first record that was inserted.
    YEAR____EMPCODE_____NAME_______POSITION
    2001______02_________Manohar________91
    2001______02_________Manohar________01
    2001______02_________Manohar________07
    2001______03_________Sasohar________81
    2001______03_________Sasohar________84
    2001______03_________Sasohar________87

    this migth be of help:
    SQL> select rowid, year, empcode, name, position
      2    from employees;
    ROWID              YEAR EM NAME       PO
    AAD8PAAAJAAAAzwAAA 2001 02 Manohar    91
    AAD8PAAAJAAAAzwAAB 2001 02 Manohar    01
    AAD8PAAAJAAAAzwAAC 2001 02 Manohar    07
    AAD8PAAAJAAAAzwAAD 2001 03 Sasohar    81
    AAD8PAAAJAAAAzwAAE 2001 03 Sasohar    84
    AAD8PAAAJAAAAzwAAF 2001 03 Sasohar    87
    6 rows selected.
    Elapsed: 00:00:00.00
    SQL> select emp.year, emp.empcode, emp.name, emp.position,
      2         emp.promotion, rowid
      3    from (select year, empcode, name, position,
      4                 rank() over (partition by empcode order by rowid) promotion
      5            from employees) emp
      6  /
    YEAR EM NAME       PO  PROMOTION ROWID
    2001 02 Manohar    91          1 AAD8PAAAJAAAAzwAAA
    2001 02 Manohar    01          2 AAD8PAAAJAAAAzwAAB
    2001 02 Manohar    07          3 AAD8PAAAJAAAAzwAAC
    2001 03 Sasohar    81          1 AAD8PAAAJAAAAzwAAD
    2001 03 Sasohar    84          2 AAD8PAAAJAAAAzwAAE
    2001 03 Sasohar    87          3 AAD8PAAAJAAAAzwAAF
    6 rows selected.
    Elapsed: 00:00:00.00
    SQL> note: you can use the rowid to identify which row has been inserted first. if this is something that you want to get the first row that was inserted.

  • How do I set only the first row of a DataGrid as the selected row?

    I have a "Go" button on a search form that fetches data into an already data bound grid.  After the data is fetched, I want to make the first row in the Datagrid the selected row, as if the user clicked on it.  If the result set is empty, I don't want the code to crash.  (I only want one row to be able to be selected at a time)
                protected function btnGo_clickHandler(event:MouseEvent):void
                    getSBJsResult.token = baa_data_svc.getSBJs(cmbSrch.text);
                    grdSBJs. //  ?????  What goes here to select the first row?

    This should do it.
    If this post answered your question or helped, please mark it as such.
    if(myDataGrid.dataProvider.length > 0){
      myDataGrid.selectedIndex = 0;

  • How do I create a 1d array that takes a single calculation and insert the result into the first row and then the next calculation the next time the loop passes that point and puts the results in thsecond row and so on until the loop is exited.

    The attached file is work inprogress, with some dummy data sp that I can test it out without having to connect to equipment.
    The second tab is the one that I am having the problem with. the output array from the replace element appears to be starting at the index position of 1 rather than 0 but that is ok it is still show that the new data is placed in incrementing element locations. However the main array that I am trying to build that is suppose to take each new calculation and place it in the next index(row) does not ap
    pear to be working or at least I am not getting any indication on the inidcator.
    Basically what I am attempting to do is is gather some pulses from adevice for a minute, place the results for a calculation, so that it displays then do the same again the next minute, but put these result in the next row and so on until the specifiied time has expired and the loop exits. I need to have all results displayed and keep building the array(display until, the end of the test)Eventually I will have to include a min max section that displays the min and max values calculated, but that should be easy with the min max function.Actually I thought this should have been easy but, I gues I can not see the forest through the trees. Can any one help to slear this up for me.
    Attachments:
    regulation_tester_7_loops.vi ‏244 KB

    I didn't really have time to dig in and understand your program in depth,
    but I have a few tips for you that might things a bit easier:
    - You use local variables excessively which really complicates things. Try
    not to use them and it will make your life easier.
    - If you flowchart the design (very similar to a dataflow diagram, keep in
    mind!) you want to gather data, calculate a value from that data, store the
    calculation in an array, and loop while the time is in a certain range. So
    theres really not much need for a sequence as long as you get rid of the
    local variables (sequences also complicate things)
    - You loop again if timepassed+1 is still less than some constant. Rather
    than messing with locals it seems so much easier to use a shiftregister (if
    absolutely necessary) or in this case base it upon the number of iterations
    of the loop. In this case it looks like "time passed" is the same thing as
    the number of loop iterations, but I didn't check closely. There's an i
    terminal in your whileloop to read for the number of iterations.
    - After having simplified your design by eliminating unnecessary sequence
    and local variables, you should be able to draw out the labview diagram.
    Don't try to use the "insert into array" vis since theres no need. Each
    iteration of your loop calculates a number which goes into the next position
    of the array right? Pass your result outside the loop, and enable indexing
    on the terminal so Labview automatically generates the array for you. If
    your calculation is a function of previous data, then use a shift register
    to keep previous values around.
    I wish you luck. Post again if you have any questions. Without a more
    detailed understanding of your task at hand it's kind of hard to post actual
    code suggestions for you.
    -joey
    "nelsons" wrote in message
    news:[email protected]...
    > how do I create a 1d array that takes a single calculation and insert
    > the result into the first row and then the next calculation the next
    > time the loop passes that point and puts the results in thsecond row
    > and so on until the loop is exited.
    >
    > The attached file is work inprogress, with some dummy data sp that I
    > can test it out without having to connect to equipment.
    > The second tab is the one that I am having the problem with. the
    > output array from the replace element appears to be starting at the
    > index position of 1 rather than 0 but that is ok it is still show that
    > the new data is placed in incrementing element locations. However the
    > main array that I am trying to build that is suppose to take each new
    > calculation and place it in the next index(row) does not appear to be
    > working or at least I am not getting any indication on the inidcator.
    >
    > Basically what I am attempting to do is is gather some pulses from
    > adevice for a minute, place the results for a calculation, so that it
    > displays then do the same again the next minute, but put these result
    > in the next row and so on until the specifiied time has expired and
    > the loop exits. I need to have all results displayed and keep building
    > the array(display until, the end of the test)Eventually I will have to
    > include a min max section that displays the min and max values
    > calculated, but that should be easy with the min max function.Actually
    > I thought this should have been easy but, I gues I can not see the
    > forest through the trees. Can any one help to slear this up for me.

  • How to get the first or last row value from a group using ntile

    I want to query and use ntile to divide by data in 4 groups (quartiles). I got this part no problem and it returns the list of rows and the ntile bucket value for each one.
    What I want though is to get only the first row from each ntile group (so in effect the max, q3, median, q1 and min values from the whole result). Of course I coud get max, min and median directly using the other functions, but how about the values for q3 and q1?
    My query contains this:
    NTILE(4) OVER (ORDER BY salary DESC NULLS LAST) as ntile
    So I want the highest, lowest, middle, q3 and q1 values for the salaries.
    Should I be using NTILE, ROWNUM, PERCENTIL_DIST....?
    Any ideas? Thanks in advance.

    SQL> select ename,sal,
      2  NTILE(4) OVER (ORDER BY sal DESC NULLS LAST) as ntile
      3  from emp
      4  /
    ENAME             SAL      NTILE
    KING             5000          1
    FORD             3000          1
    SCOTT            3000          1
    JONES            2975          1
    BLAKE            2850          2
    CLARK            2450          2
    ALLEN            1600          2
    TURNER           1500          2
    MILLER           1300          3
    WARD             1250          3
    MARTIN           1250          3
    ENAME             SAL      NTILE
    ADAMS            1100          4
    JAMES             950          4
    SMITH             800          4
    14 rows selected.
    SQL> select  ename,
      2          sal
      3    from  (
      4           select  ename,
      5                   sal,
      6                   ROW_NUMBER() OVER (PARTITION BY ntile ORDER BY sal DESC NULLS LAST) as rn
      7             from  (
      8                    select  ename,
      9                            sal,
    10                            NTILE(4) OVER (ORDER BY sal DESC NULLS LAST) as ntile
    11                      from  emp
    12                   )
    13          )
    14    where rn = 1
    15  /
    ENAME             SAL
    KING             5000
    BLAKE            2850
    MILLER           1300
    ADAMS            1100
    SQL> SY.

  • (JTable) How can i get the first columns cell in a selected row?

    Hello. I am trying to figure out how i can get the first columns cell within a selected row no matter what cell is selected in that row. I have a class that extends AbstractTableModel which represents the table. Now i have another class that extends DefaultSelectionModel. Each model is added to the JTable via setModel(TableModel dataModel), setSelectionModel(ListSelectionModel newModel). Now i don't understand what i have to return from getMaxSelectionIndex(). Any ides? Thanks.
      class xTableModel extends AbstractTableModel{
         private static String[] cols;
         private Object[][] data;
            public void setTableModel(Object data[][], String[] cols){
             this.cols = cols;
             this.data = data;
            public String getColumnName(int col){
             return cols[col];
            public int getRowCount() {
             return data.length;
            public Object getValueAt(int row, int col) {
             return data[row][col];           
            public void setValueAt(Object value,int row,int col){
             data[row][col]=value;
            public int getColumnCount(){
             return cols.length;
    class ColumnListenerModel extends DefaultListSelectionModel{
         public int getMaxSelectionIndex(){}

    int row = table.getSelectedRow();
    Object data = table.getValueAt(row, 0);

  • Selecting The First Row Of h:dataTable after getting new DataModel f

    Hi Friends
    If any one knows how to select first row of a dataTable on change of a DataModel it is representing please give the reply

    I haven't understood the answer of UlrichCech
    I am not using IRDA for my development purpose.
    and abt your reply ..
    I have already tried tht way but the problem is that
    whenever I selects different row page gets postback and selects
    the first row again and there is no way to trap posback event in JSF
    like ASP.NET
    I think my answer is some where related to DataModelListener..

  • Query result area on the first row instead of last row

    Hi BI experts,
    I want to display the result of  the BI query at the first row instead of last row.Could somebody help me how to accomplish this.
    I tried with the layout->move result area but could not be possible.
    I need your kind suggetion.
    Thanks.

    hi jyuoti,
    Further, open your query in query designer, look at the query property,  under the tab "rows/columns"
    change the result position of rows to "Below", you can see the preview there itself.
    This should solve your problem.
    Best Regards,
    *Assign points if you find the answer useful.

  • Getting the index to the first row of an Internal table

    Hi,
            Assume that i have inserted five rows(fiiling contents for some fields) in an ITAB using Append statement.. Now if i want to fill the content for the remaining field from the first row , then how will i get it back to first row, If i still use Append it starts inserting from the 6th row, Instead of 6th row i want it from first row... Plss Help!!!!!!
    Thanks In Advance,
    Sanu

    lets say itab contains fields as f1,f2,f3,f4.
    one method is :
    LOOP AT itab into is.
      if sy-tabix = 1.
        is-f2 = 'asdasd'.
        is-f3 = 'gggg'.
        is-f4 = 'kjhjk'.
        modify itab from is.
        exit. " to come out of loop.
      endif.   
    ENDLOOP.
    another method is:
    clear is.
    read table itab into is INDEX 1.
    IF sy-subrc = 0.
      is-f2 = 'asdasd'.
        is-f3 = 'gggg'.
        is-f4 = 'kjhjk'.
      MODIFY itab from is INDEX 1. 
    ENDIF.

  • Get only the first row for duplicate data

    Hello,
    I have the following situation...
    I have a table called employees with column (re, name, function) where "re" is the primary key.
    I have to import data inside this table using an xml file, the problem is that some employees can be repeated inside this xml, that means, I'll have repeated "re"'s, and this columns is primary key of my table.
    To workaround, I've created a table called employees_tmp that has the same structed as employees, but without the constraint of primary key, on this way I can import the xml data inside the table employees_tmp.
    What I need now is copy the data from employees_tmp to employess, but for the cases where "re" is repeated, I just want to copy the first row found.
    Just an example:
    EMPLOYEES_TMP
    RE NAME FUNCTION
    0987 GABRIEL ANALYST
    0987 GABRIEL MANAGER
    0978 RANIERI ANALYST
    0875 RICHARD VICE-PRESIDENT
    I want to copy the data to make employees looks like
    EMPLOYEES
    RE NAME FUNCTION
    0987 GABRIEL ANALYST
    0978 RANIERI ANALYST
    0875 RICHARD VICE-PRESIDENT
    How could I do this?
    I really appreciate any help.
    Thanks

    Try,
    SELECT re, NAME, FUNCTION
      FROM (SELECT re,
                   NAME,
                   FUNCTION,
                   ROW_NUMBER () OVER (PARTITION BY re ORDER BY NAME) rn
              FROM employees_tmp)
    WHERE rn = 1G.

Maybe you are looking for

  • Error while Reimporting the table

    Hi All, I am getting the following error while reimporting the target table. java.lang.NullPointerException. Can anybody tell me the solution plz. Regards, Siva

  • Home directory is full problem

    My Mac at work gets the following message now whenever I try to use Mail - "Mail cannot update your mailboxes because your home directory is full. You must free up space in your home folder before using Mail. Delete unneeded documents or move documen

  • Safari will not open despite restart and reinstallation of OS.

    When I open safari, I cant access any menus (I just see the color wheel when I hover over any menus).  On activity monitor, Safari uses increasingly larger amounts of memory.  I have to force quit to get out of the application.  Ive restarted numerou

  • Text Data Value of the Customer Master

    Dear All, I have created a new text types for the customer as Z001 and Z004. But now i want to create report with the customer and value maintain in those text fields. I'm little confuse how take the values. I know i have to go to the table STXH but

  • N900 - Emails not shown correctly

    Mails received via a POP3 account configured on my N900 do not show up correctly as paragraphs containing Danish characters (æ, ø and å) are displayed as Japanese. Any suggestions for a fix?