Query multiple occurances of min value from table using group by

Hello all,
I am using Oracle 10.2 on Windows 2003
I am attempting to select the min value from a table, and if there are multiple occurances of a min value, to list all not just one row. For example, the following query
with test1 as(
select to_date('2009-11-01','YYYY-MM-DD') t_date, 't_1' t_name, '1' t_value from dual
union all select to_date('2009-12-01','YYYY-MM-DD') t_date, 't_1' t_name, '2' t_value from dual
union all select to_date('2010-01-01','YYYY-MM-DD') t_date, 't_1' t_name, '1' t_value from dual
union all select to_date('2010-02-01','YYYY-MM-DD') t_date, 't_1' t_name, '3' t_value from dual
union all select to_date('2010-03-01','YYYY-MM-DD') t_date, 't_1' t_name, '4' t_value from dual
union all select to_date('2010-04-01','YYYY-MM-DD') t_date, 't_1' t_name, '5' t_value from dual
union all select to_date('2010-05-01','YYYY-MM-DD') t_date, 't_1' t_name, '6' t_value from dual
union all select to_date('2010-06-01','YYYY-MM-DD') t_date, 't_1' t_name, '1' t_value from dual
union all select to_date('2010-07-01','YYYY-MM-DD') t_date, 't_1' t_name, '1' t_value from dual
select trunc(t_date,'YYYY') t_date, min(t_value) min_value
from test1
group by trunc(t_date,'YYYY') gives the following results
t_date         min_value
01-JAN-09    1
01-JAN-10    1so I looked at the forums and tried the following query
with test1 as(
select to_date('2009-11-01','YYYY-MM-DD') t_date, 't_1' t_name, '1' t_value from dual
union all select to_date('2009-12-01','YYYY-MM-DD') t_date, 't_1' t_name, '2' t_value from dual
union all select to_date('2010-01-01','YYYY-MM-DD') t_date, 't_1' t_name, '1' t_value from dual
union all select to_date('2010-02-01','YYYY-MM-DD') t_date, 't_1' t_name, '3' t_value from dual
union all select to_date('2010-03-01','YYYY-MM-DD') t_date, 't_1' t_name, '4' t_value from dual
union all select to_date('2010-04-01','YYYY-MM-DD') t_date, 't_1' t_name, '5' t_value from dual
union all select to_date('2010-05-01','YYYY-MM-DD') t_date, 't_1' t_name, '6' t_value from dual
union all select to_date('2010-06-01','YYYY-MM-DD') t_date, 't_1' t_name, '1' t_value from dual
union all select to_date('2010-07-01','YYYY-MM-DD') t_date, 't_1' t_name, '1' t_value from dual
select t_date,min_value
from (select t_date,min(t_value) min_value,
rank() over (order by min(t_value) ASC) RN
from test1 group by t_date)
where rn=1I get the desired results with this query, which are
t_date           min_value
01-NOV-09     1
01-JAN-10      1
01-JUN-10      1
01-JUL-10       1the problem is, when I change the values in the test1 table to the following
with test1 as(
select to_date('2009-11-01','YYYY-MM-DD') t_date, 't_1' t_name, '123' t_value from dual
union all select to_date('2009-12-01','YYYY-MM-DD') t_date, 't_1' t_name, '2' t_value from dual
union all select to_date('2010-01-01','YYYY-MM-DD') t_date, 't_1' t_name, '21' t_value from dual
union all select to_date('2010-02-01','YYYY-MM-DD') t_date, 't_1' t_name, '13' t_value from dual
union all select to_date('2010-03-01','YYYY-MM-DD') t_date, 't_1' t_name, '24' t_value from dual
union all select to_date('2010-04-01','YYYY-MM-DD') t_date, 't_1' t_name, '15' t_value from dual
union all select to_date('2010-05-01','YYYY-MM-DD') t_date, 't_1' t_name, '26' t_value from dual
union all select to_date('2010-06-01','YYYY-MM-DD') t_date, 't_1' t_name, '100' t_value from dual
union all select to_date('2010-07-01','YYYY-MM-DD') t_date, 't_1' t_name, '2' t_value from dual
select t_date,min_value
from (select t_date,min(t_value) min_value,
rank() over (order by min(t_value) ASC) RN
from test1 group by t_date)
where rn=1i get the following results
t_date          min_value
01-JUN-10     100I expected to get the results
t_date         min_value
01-DEC-09     2
01-JUL-10      2any help would be appreciated
Cheers

Because t_value is character.
You should To_NUMBER(t_value)
with test1 as(
select to_date('2009-11-01','YYYY-MM-DD') t_date, 't_1' t_name, '123' t_value from dual
union all select to_date('2009-12-01','YYYY-MM-DD') t_date, 't_1' t_name, '2' t_value from dual
union all select to_date('2010-01-01','YYYY-MM-DD') t_date, 't_1' t_name, '21' t_value from dual
union all select to_date('2010-02-01','YYYY-MM-DD') t_date, 't_1' t_name, '13' t_value from dual
union all select to_date('2010-03-01','YYYY-MM-DD') t_date, 't_1' t_name, '24' t_value from dual
union all select to_date('2010-04-01','YYYY-MM-DD') t_date, 't_1' t_name, '15' t_value from dual
union all select to_date('2010-05-01','YYYY-MM-DD') t_date, 't_1' t_name, '26' t_value from dual
union all select to_date('2010-06-01','YYYY-MM-DD') t_date, 't_1' t_name, '100' t_value from dual
union all select to_date('2010-07-01','YYYY-MM-DD') t_date, 't_1' t_name, '2' t_value from dual
select t_date,min_value
from (select t_date,min(t_value) min_value,
rank() over (order by min(To_NUMBER(t_value)) ASC) RN
from test1 group by t_date)
where rn=1

Similar Messages

  • How to find max(time) from table using group by

    how to find max(time) from table using group by
    select var max(time)
              from table
              into (var1, time1)
               where .....
                 group by var.
    it is fetching record which is top in table.
    if u can help?
    regards.

    No this will fetch the maximum time from teh table.
    select var max(time)
    from table xxxx
    into (var1, time1)
    where .....
    group by var.
    Refer this code
    TABLES SBOOK.
    DATA:  COUNT TYPE I, SUM TYPE P DECIMALS 2, AVG TYPE F.
    DATA:  CONNID LIKE SBOOK-CONNID.
    SELECT CONNID COUNT( * ) SUM( LUGGWEIGHT ) AVG( LUGGWEIGHT )
           INTO (CONNID, COUNT, SUM, AVG)
           FROM SBOOK
           WHERE
             CARRID   = 'LH '      AND
             FLDATE   = '19950228'
           GROUP BY CONNID.
      WRITE: / CONNID, COUNT, SUM, AVG.
    ENDSELECT.

  • Get Field Values from Table using Java api

    I am using the example java code "RetrieveLimitedRecords" that can be found at :
    https://help.sap.com/javadocs/MDM71/current/API/index.html
    The code give the expected result and retrieves the record count for the main table
    Now I want to get the values of the fields for one record
    I added the lines:
    Record[] records = recordResultSet.getRecords() ;
    FieldId[] fields = records[0].getFields();
    System.out.println ("Field Length = "+fields.length);
    and the output is::
    Field Length = 0
    How can I get the fields of the record and read their values?
    Thanks
    Nicolas

    Assuming you want every field, the equivalent of "SELECT *" in SQL, you can use the RepositorySchema object to get a TableSchema, and with that get all FieldIds for the table.
    If your RepositorySchema variable is rs it would be something along the lines of:
    TableSchema mainTableSchema = rs.getTableSchema(mainTableId);
    ResultDefinition rd = new ResultDefinition(mainTableId);
    rd.setSelectFields(mainTableSchema.getFieldIds());
    Hope this helps,
    Greg

  • SAP Query - Additional field that collects information from table RESB

    Hello gurus. I have a question.
    I want to create a SAP Query that shows me the stock level of a list of materials, and also show me the total quantity of order reservations in an additional field.
    I created an InfoSet with table MARD, which is the one that holds the Stock information in a plant. Then I created an additional field which would read information from table RESB, the table that holds order reservations per material.
    So I wrote this piece of code:
    SELECT * FROM RESB
    WHERE MATNR EQ MARD-MATNR and
           WERKS EQ MARD-WERKS.
    ENDSELECT.
    if ( sy-dbcnt NE '0').
          MOVE RESB-BDMNG to ZQTY.
    ELSE.
          MOVE '' to ZQTY.
    ENDIF.
    This works fine. However, this is currently just catching the first record in table RESB that matches my condition.
    What I would like is to collect every instance of RESB-BDMNG and add them to field "ZQTY", have it loop in RESB until it finishes finding every record that match the MATNR and WERKS. With this I could get the total number of order reservations that this material has in that table.
    Could someone share some coding that would help me achieve this?

    Yes! That did it. That's what I needed to do. Thank you so much.
    While I'm at it, let me ask you a related question.
    When I execute the query, in the first records of the query where there's no value from RESB to transfer, the value of field ZQTY appears empty. Once it finds the first record in RESB and it populates ZQTY with a value, then the rest of the records with no hit get the proper value of 0.
    Do you know why the first records in the query appear empty and not with a 0? Is there anything I should add to the coding to fix this?

  • Fetch value from Table with respect to APP_USER.

    Hi,
    i want to fetch value from Table with respect to APP_USER.
    i am using
    SELECT ID,NAME,FILE_OBJ_ID,MIME_TYPE,DOC_SIZE,BLOB_CONTENT,DESCRIPTION,UPLOAD_DATE,'Download' FROM DUMY_FILE where CREATED_BY =:APP_USER;
    Show me error
    failed to parse SQL query:
    ORA-00911: invalid characterThanks
    Edited by: 805629 on Jan 13, 2011 2:09 AM

    Hello,
    I too have the same problem.
    I am using this query:
    select manager from leave_mgmt where support_engg=:APP_USER
    and source type is SQL Query(return single value)
    but nothing is coming up in the text field. Its empty.
    Please advice. Thanks in advance.

  • Deleting the values from table control

    HI,
    I need to remove the selected line from the Table control on my screen.
    The point is that the Internal table does not have any records,.
    for example in the Customer Create screen,
    while entering the bank details we will enter it in a tbale contraol.
    but the records doesnt exist in the database. but the record gets deleted from the table control only if we press delete button.
    please guide me.
    please note : I want to delete the record from Table Control on the screen and I dont have any corresponding database records..
    regards

    Hi..
    The solution to your problem - deleting values from table control.
    Here: it_wizard is the internal table which is holding the value of table control.
             wa_wizard is the work are of the internal table it_wizard.
             it_delete is the internal table which holds the deleted record of table control.
             wa_delete is the work area of the internal table it_delete
    Also here the field ZSEL is the character field which is used to select the entire record in the table control.
    LOOP AT it_wizard into wa_wizard WHERE zsel = 'X'.
           MOVE-CORRESPONDING wa_wizard TO wa_delete.
           APPEND wa_delete TO it_delete.
           delete table it_wizard from wa_wizard.
    DELETE  FROM zfin_goods WHERE ZFG = WA_delete-ZFG.
    ENDLOOP
    if sy-subrc eq 0.
    Message 'Delete Successful' type 'S'.
    endif.
    The above code will delete the record from both the table control.internal table and the database table.
    I think this will help you to great extent.
    Ward regards,
    Bhuvaneswari
    Edited by: BHUVANESWARI THIRUNAVUKKARASU on Jan 7, 2009 10:39 AM

  • How to populate a text field by some value from table in Oracle seeded page through Personalization?

    I have to populate Description field by default in AGIS page (Advanced Global Intercompany System).
    Field Description:
    messageTextInput: Description
    VO: FunTrxHeaderVO
    VO Attribute: Description
    VO is entity based.
    Now if the field is null then I have to populate this value by some value from table for selected batch_id.
    Example:
    (SELECT DESCRIPTION FROM fun_trx_batches WHERE batch_id =
    211061)
    Also, this value is being populated in the Batch Information region in the same page. So the problem is also be treated as to copy a value from one messageStyledText to messageTextInput in a same page.
    Can this be done through personalization, w/o coding?
    Please suggest if you any idea how to resolve..
    Regards,
    Pahari

    Not sure why it is not displying in the front page list??
    I have to search it.
    Admin team, please help.
    Regards,
    Pahari

  • How to take Purchase order Pending quantity value from Table. ?

    Hai SAP Gurus,
                             Now Using ME2N, I am taking Pending Purchase Order Value against Plant wise & Material Type wise. Its takes lomg time to see this report.. Kindly guide me  how to takes those value from table..?
    ex:  Material Type     Plant 1
           RM                     1716294228
           HIBE                         63586533
           ERSA         27143712
           VERP      7551982745
           Components      7375781142
           Total                    Rs. 16734788360

    Hi
    It is possible through ME2N plantwise pend PO report.
    Put plant and WE101 in selection parameters

  • Select values from table%ROWTYPE variable into a cursor

    I have a stored procedure which has an OUT parameter of table1%ROWTYPE.
    In future we might have to add more OUT parameters of table2%ROWTYPE etc. But at any point of time only one will have values.
    So instead of having table%ROWTYPE as OUT parameter, can I send these single row ( with variable values) in a cursor, so that declaration part atleast will not change.
    Is it possible to select values from table%ROWTYPE variable into a cursor.
    cursorOUT IS
    SELECT * FROM varREC;
    where varREC is table.ROWTYPE variable.
    Or which is the better solution in this situation.
    Thanks.

    SQL> var a refcursor
    SQL> declare
      2   bb emp%ROWTYPE;
      3  begin
      4   select * into bb from emp where rownum = 1;
      5   open :a for select bb.ename ename, bb.empno empno from dual;
      6  end;
      7  /
    PL/SQL procedure successfully completed.
    SQL> print a
    ENAME                                 EMPNO
    SMITH                                  7369Rgds.

  • How to remove default value from table definition?

    Hi,
    running 9.2
    Following problem :
    I need to remove default value from table.
    Example
    CREATE TABLE MY_TABLE(
    NAME VARCHAR2(50) DEFAULT 'NAME',
    AGE NUMBER DEFAULT 0
    Now even when I use MODIFY like
    ALTER TABLE MY_TABLE MODIFY (NAME VARCHAR2(10) );
    The default is still there:
    SELECT DATA_DEFAULT FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'MY_TABLE';
    Is there any way?
    Thanks

    Thanks,
    found the answer. It is not possible!!!!
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_3001.htm
    Note:
    If a column has a default value, then you can use the DEFAULT clause to change the default to NULL, but you cannot remove the default value completely. That is, if a column has ever had a default value assigned to it, then the DATA_DEFAULT column of the USER_TAB_COLUMNS data dictionary view will always display either a default value or NULL

  • Function module to find out the changed values from tables

    What is the standard function module to find out the changed values (the old & new values) from tables in SAP ?

    Hi Harish,
    Please elaborate your requirement...
    Please have a look on CDHDR AND CDPOS tables... it contains changed data... but all chages are not being tracked using it..
    Try Below FMs as well..
    For Header Level...
    CALL FUNCTION 'CHANGEDOCUMENT_READ_HEADERS'
        EXPORTING
          date_of_change    = cdhdr-udate
          objectclass       = cdhdr-objectclas
          objectid          = cdhdr-objectid
          time_of_change    = cdhdr-utime
          username          = cdhdr-username
        TABLES
          i_cdhdr           = icdhdr
        EXCEPTIONS
          no_position_found = 1
          OTHERS            = 2.
    For Item Level
    CALL FUNCTION 'CHANGEDOCUMENT_READ_POSITIONS'
          EXPORTING
            changenumber      = icdhdr-changenr
          IMPORTING
            header            = cdhdr
          TABLES
            editpos           = icdshw
          EXCEPTIONS
            no_position_found = 1
            OTHERS            = 2.
    Hope it will solve your problem..
    Thanks & Regards
    ilesh 24x7
    ilesh Nandaniya

  • How to delete a single data from table using control file

    I want delete a single row data from table using sql loder control file
    Edited by: 977940 on Dec 19, 2012 9:00 PM

    977940 wrote:
    I want delete a single row data from table using sql loder control file
    Edited by: 977940 on Dec 19, 2012 9:00 PMWhy?
    And how do you imagine this happening with sqlloader?
    The entire purpose of sqlloader is to load data (hence, the name sql*loader*) into a table from an external source. If you want to delete rows from a table, you use the sql DELETE statement.
    What is the business problem you are trying to solve?

  • How to get sum amount (wrbtr) from table BSIS group by gjahr hkont monat

    Hello! I am new to this forum, and to ABAP. Hope my question is not obvious.
    I want to get sum amount from table BSIS, group by year, period an account. Normally I will try a SQL select like this:
    SELECT gjahr monat hkont sum( wrbtr ) as wrbtr INTO CORRESPONDING FIELDS OF TABLE itab FROM BSIS WHERE (itabcond) GROUP BY gjahr monat hkont .
    The problem is that the amount in field wrbtr is all positive. The result gets wrong. The debet/credit indicator shkzg determines what is positive/negative. How to solve this in the most efficient way? Should I read all accounting documents into an internal table, and loop through them, changing the sign, and then make some new select, or is there a better way?
    Regards Dag-Egil
    Message was edited by:
            Dag-Egil Bull Sletholt

    Here are the objects in the trace list:
    SKB1
    USR05
    TRDIR
    SKA1
    SKAT
    SKB1
    BSIS
    PRPS
    BSAS
    V_LTDX
    USR02
    RFBLG
    V_LTDX
    LTDX
    RTXTH
    RTXTF
    RTXTT
    TRDIR
    The most of this call to this tables returns 1-2 records, apart from BSIS, where it returns 10 records. This is the same as accounting documents in the transaction FBL3N. When i try this again with more records returned I still have no call to table GLT0.

  • Radio Buttons - returning individual values from an exclusion group possible?

    Using LC Designer 7.1
    Does anyone know if it is possible to return individual values from an exclusion group of radio buttons?  My xml data file gives one value for the entire group, e.g.,
    2
    ...where the second radio button was selected.  But I'd prefer an output something like this...
      0
      1
      0
    etc...
    Is something in this format possible?

    You might be better off to use checkboxes and script them to act like radio buttons (as an exclusion group). That way they'd each have an on/off value.
    Regards,
    Dave

  • How to Get ZERO and Non-ZERO Values in rowcount using Group by?

    Dear All,
    How can I get Non-ZERO and ZERO row count values in SQL using Group by? I can get non-zero values but when I want NULL should be returned to non-zero values it is ignoring ZERO values in output?
    Any hint?
    Thanks
    GQ

    Hi,
    Something like
    select count(case col
                    when 0
                    then 1
                  end) zero_count,
           count(case nvl(col,1)
                    when 0
                    then null
                    else 1
                  end) nonzero_countRegards
    Peter

Maybe you are looking for