How to select rows with min value on a specific column

I have a query:
select
  m.x1,
  round (to_date (l.y1, 'mm/dd/yyyy HH:MI:SS AM') - m.x2, 0) as numofdays
from
  table1 m,
  table2 l
where
  l.x3 = m.x3 and
  to_date (l.y1, 'mm/dd/yyyy HH:MI:SS AM') >= TO_DATE('01012013','MMDDYYYY') and
and I got this result table:
x1 (ID)
numofdays
001
5
001
10
002
2
003
3
003
1
004
0
005
66
several ID's have multiple values on the second column, I want to have only distinct IDs with smallest "numofdays" like this:
x1 (ID)
numofdays
001
5
002
2
003
1
004
0
005
66
Any ideas?

Hi,
The most general and versatile way is a Top-N Query:
WITH   got_r_num AS
    select
            m.x1,
            round (to_date (l.y1, 'mm/dd/yyyy HH:MI:SS AM') - m.x2, 0) as numofdays
    ,       ROW_NUMBER () OVER ( PARTITION BY  m.x1
                                 ORDER BY      to_date (l.y1, 'mm/dd/yyyy HH:MI:SS AM') - m.x2
                               )  AS r_num
    from 
          table1 m,
          table2 l
    where
          l.x3 = m.x3 and
          to_date (l.y1, 'mm/dd/yyyy HH:MI:SS AM') >= TO_DATE('01012013','MMDDYYYY') and
SELECT  x1, numofdays
FROM    got_r_num
WHERE   r_num   = 1
If you'd care to post CREATE TABLE and INSERT statements for your sample data, then I could test it.
Notice that the sub-query (got_r_num) is exactly what you posted, only with a new column (r_num) added to the SELECT clause.

Similar Messages

  • How to merge rows with similar values in alv grid display in webdynpro

    Hi experts,
                   i want to know about how to merge rows with similar values in alv grid display of webdynpro.grouping rows is possible in table display in webdynpro but i am not able to do row grouping in the alv grid display in webdynpro.
    kindly suggest.
    thanks ,
    Anita.

    Hi Anita,
    did you find a solution for this? I have opened a Thread, if you know the answer maybe you could help me out:
    Is there an ALV function similar to the TABLE Row grouping?
    Thanx in advanced!!!
    Kind Regards,
    Gerardo J

  • How to select rows with Empty Column Entry

    I am trying to select all rows with an empty column
    select row1 from table where row2=' ';
    Any suggestions. I tried a space and no space in between the ''.

    An (theoretical) argument could be made that an empty string is not NULL (uknown), however Oracle's SQL and PL/SQL engines make no such distinction for VARCHAR2 and CHAR data types. As stated, you must use NULL as the comparison.
    Michael

  • How to select  rows with duplicate column values

    hi,
    i have a table property_details which has these 2 columns customerno and propertyno a customer can have many properties and property number has to be unique. but somehow these property number has been duplicated at an earlier stage so i have for a customer with many properties the same property number
    how i will select such records whose asset numbers are the same
    for ex
    customer no property no
    a1300 1
    a1300 1
    a1300 1
    a2330 10
    a2330 10
    a2330 10
    kindly suggest me a solution

    this example might be of help.
    SQL> select * from employees;
    YEAR EM NAME       PO
    2001 02 Scott      91
    2001 02 Scott      01
    2001 02 Scott      07
    2001 03 Tom        81
    2001 03 Tom        84
    2001 03 Tom        87
    6 rows selected.
    SQL> select year, empcode, name, position,
      2         row_number() over (partition by year, empcode, name
      3                            order by year, empcode, name, position) as rn
      4    from employees;
    YEAR EM NAME       PO         RN
    2001 02 Scott      01          1
    2001 02 Scott      07          2
    2001 02 Scott      91          3
    2001 03 Tom        81          1
    2001 03 Tom        84          2
    2001 03 Tom        87          3
    6 rows selected.
    SQL> Select year, empcode, name, position
      2    From (Select year, empcode, name, position,
      3                 row_number() over (partition by year, empcode, name
      4                                    order by year, empcode, name, position) as rn
      5            From employees) emp
      6   Where rn = 1;
    YEAR EM NAME       PO
    2001 02 Scott      01
    2001 03 Tom        81
    SQL>

  • Selecting rows with null values in an sql 3 table

    Hi everyone I use oracle database 11g and have the following SQL statement:
    SELECT Oprema.Opr_Id, Oprema.Datum_Nabavke, Oprema.Datum_Zaduzenja, Oprema.Dobavljac, Oprema.Jedinica_Mjere,
         coalesce ((Oprema.Zaduzio), 0), Oprema.Vrijednost, Oprema.Kolicina_Nabavna, Oprema.Kolicina_Otpisana, Oprema.Kolicina_Trenutna, Oprema.Status, Oprema.Konto,
         Oprema.KontoIsp, Oprema.Broj_Naloga, Oprema.Sifra_Objekta, Dobavljaci.Naziv AS DobNaz, coalesce(to_char(Uposlenici.Prezime), 'hm'), Objekti.Objekat_ID
    FROM OPREMA Oprema, UPOSLENICI Uposlenici, DOBAVLJACI Dobavljaci, OBJEKTI Objekti
    WHERE  Oprema.Dobavljac=Dobavljaci.Dob_Id AND Oprema.Zaduzio=Uposlenici.UposlenikId AND Oprema.Sifra_Objekta=Objekti.Objekat_IDWhat I need is to show all the rows in the database. The problem is that some of the foreign keys Zaduzio and Objekti are NULL. This means the databse won't show those and only shows me rows where both have values. In all of these rows except for 3 only one of the two foreign keys has a value.
    So what I need is an sql that would show me all 3. As you can see I tried with Coallesce but it didn't work, still only 3 rows show.
    I also tried with UNION
    SELECT Oprema.Opr_Id, Oprema.Datum_Nabavke, Oprema.Datum_Zaduzenja, Oprema.Dobavljac, Oprema.Jedinica_Mjere,
         Oprema.Zaduzio, Oprema.Vrijednost, Oprema.Kolicina_Nabavna, Oprema.Kolicina_Otpisana, Oprema.Kolicina_Trenutna, Oprema.Status, Oprema.Konto,
         Oprema.KontoIsp, Oprema.Broj_Naloga, Oprema.Sifra_Objekta, Dobavljaci.Naziv AS DobNaz, Uposlenici.Prezime, Objekti.Objekat_ID
    FROM OPREMA Oprema, UPOSLENICI Uposlenici, DOBAVLJACI Dobavljaci, OBJEKTI Objekti
    WHERE  Oprema.Dobavljac=Dobavljaci.Dob_Id AND Oprema.Zaduzio=Uposlenici.UposlenikId AND Oprema.Sifra_Objekta=Objekti.Objekat_IDBut then the rows that have both Zaduzio and Objekti values are multiplied and selected twice.
    So any ideas on how to do this?

    Hi,
    If those foreign keys are NULL, then an inner join won't produce any results. Maybe you need to make it an outer-join, like this:
    SELECT  Oprema.Opr_Id
    ,      Oprema.Datum_Nabavke
    ,      Oprema.Datum_Zaduzenja
    ,      Oprema.Dobavljac
    ,      Oprema.Jedinica_Mjere
    ,     COALESCE ( Oprema.Zaduzio
               , 0
               )          AS zaduzio_or_0
    ,      Oprema.Vrijednost
    ,      Oprema.Kolicina_Nabavna
    ,      Oprema.Kolicina_Otpisana
    ,      Oprema.Kolicina_Trenutna
    ,      Oprema.Status
    ,      Oprema.Konto,
         Oprema.KontoIsp
    ,      Oprema.Broj_Naloga
    ,      Oprema.Sifra_Objekta
    ,      Dobavljaci.Naziv     AS DobNaz
    ,      COALESCE ( TO_CHAR (Uposlenici.Prezime)
               , 'hm'
               )          AS prezime_or_hm
    ,      Objekti.Objekat_ID
    FROM            OPREMA
    JOIN            DOBAVLJACI  ON   Oprema.Dobavljac      = Dobavljaci.Dob_Id
    LEFT OUTER JOIN      UPOSLENICI  ON       Oprema.Zaduzio     = Uposlenici.UposlenikId
    LEFT OUTER JOIN  OBJEKTI     ON       Oprema.Sifra_Objekta  = Objekti.Objekat_ID
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
    Simplify the problem. For example, if you really need to display 18 columns, but 15 of them have nothing to do with the problem, then post a question that just includes e or 4 of those columns.
    Explain, using specific examples, how you get those results from that data.
    Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
    See the forum FAQ {message:id=9360002}

  • How to select rows based on values from other table?

    I need to select all rows from TABLE1 whose time column is NOT between fromTime and toTime from TABLE2.
    For example, those table contain following rows:
    TABLE1
    |id |time |...
    |1 | 10 |...
    |2 | 20 |...
    |... | ... |...
    TABLE2
    |fromTime|toTime|
    | 3 | 6 |
    | 10 | 16 |
    | 20 | 25 |
    So, in the case above the first and second rows shouldn't be returned by the query since 10<=10<=16 AND 20<=20<25.

    Try
    -- code #1
    SELECT id, time
    from TABLE1 as T1
    where not exists (SELECT * from TABLE2 as T2
    where T1.time between T2.fromTime and T2.toTime);
    José Diz     Belo Horizonte, MG - Brasil

  • Deleting  rows with missing values in field in start routine of update rule

    Hello experts,
    how can I delet rows with missing values in a specific field in the start routine of update rules?
    I think ABAP code should look something like this:
    delete ...  from DATA_PACKAGE where Z_NO = ''.
    thanks in advance for any suggestions!
    hiza

    Write:
    delete data_package where field = value.
    Hope it helps.
    Regards

  • How to delete the selected rows with a condition in alv

    dear all,
    i am using the code in object oriented alv.
    WHEN 'DEL'.
    PERFORM delete_rows.
    FORM delete_rows.
    DATA : lv_rows LIKE lvc_s_row.
    data : wa_ROWs like LVC_S_ROW.
    FREE : gt_rows.
    CALL METHOD alv_grid->get_selected_rows
    IMPORTING
    et_index_rows = gt_rows.
    IF gt_rows[] IS INITIAL.
    MESSAGE s000 WITH text-046.
    EXIT.
    ENDIF.
    loop at gt_rows into wa_ROWs .
    if sy-tabix ne 1.
    wa_ROWs-INDEX = wa_ROWs-INDEX - ( sy-tabix - 1 ).
    endif.
    delete gt_sim INDEX wa_ROWs-INDEX .
    endloop.
    the rows to be deleted from int.tab gt_sim not in the alv display.
    all the rows should not be deleted if one of the field in gt_sim eq 'R'.
    how to check this condition

    dear jayanthi,
            ok if i am coding like that as u mentioned ,
              it will exit the loop when first time the field value is 'R'.
      if any of  the selected rows contains  field value 'R'. it shold not delete all the selected rows.
    as u suggested it will not delete after first time the field value is r.
    i am deleting it by tab index so,
    suppose if i am selecting the row without field value R say its tabix is 1.
      the next row with tabix 2 with field value R.
      it deletes the first row and exits , it should not delete the first row also.

  • Return to selected row with refresh after update in edit  form

    Hi,
    I created a database view.When selected a single row and click on button i can edit the row in a form for update.What i want, when doing some update and click on the commit button i return back on the initial row with a refresh to see the update done on this row.I succeeded to modify the form and commit.But when i return on the selected row my update is not visible.How can i return to the same selected row with a refresh.In oracle form i can do a go record with execute_query, but how to do this in ADF.Please someone can help so that i do not spend many days on it.
    Thanks
    Soodesh

    [click for the tutorial|http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=6&ved=0CFQQFjAF&url=http%3A%2F%2Fandrejusb.blogspot.com%2F2013%2F03%2Fadf-rollback-and-keep-current-row.html&ei=Tnl3Ucn3BoXIrQeemICQAw&usg=AFQjCNHdYcJL8kJKymqbWBT9XDGTWmeUvQ&bvm=bv.45580626,d.bmk&cad=rja]
    here you go :)

  • Updating multiple rows with different values

    Hi!
    I have a problem. I need to update more then 1000 rows with different values. How can I do it?
    For exsample i have table:
    id; color, date,
    1 red
    2 green
    3 white
    I need to update date field.
    Update table
    set date='01.02.03'
    where id=1
    Update table
    set date='01.03.03'
    where id=2
    Maybe there is way how to update multiple rows at one query?
    Sorry for my bad english.
    Thanks!

    Hi,
    You can try this
    UPDATE TABLE SET DATE = CASE
                        WHEN ID = 1 THEN TO_DATE('01-02-03','DD-MM-RR')
                        WHEN ID = 2 THEN TO_DATE('01-03-03','DD-MM-RR')
                        ENDcheers
    VT

  • Returning 250 rows with 1000 Values in "IN" Clause Oracle 10g On IBM AIX !!

    Hi,
    Recently we have done the OS migration of Oracle 10g Server from Windows Server to IBM AIX. Everything is fine, But today we came across one crucial bug in the code, i.e In the Select Query, though we're expecting 1000 rows with 1000 values in "IN" Clause , It's returning Only 250 rows. Where as it's returning 1000 rows in Windows Environment with 1000 values in "IN" Clause. I have browsed throgh Google for the resolution but failed to get that.
    This is something like,
    In Oracle 10g On windows :-
    select * from emp
    where dept_id in (1,2,3,...................1000);
    Assuming there  are the dept_id values in Emp table from 1 ... 1000, It's returning 1000 rows.
    In Oracle 10g On IBM AIX ,
    select * from emp
    where dept_id in (1,2,3,...................1000);
    Assuming there  are the dept_id values in Emp table from 1 ... 1000, It's returning 250 rows. Pls help me, what could be the reason for this. and what needs to be checked to fix this.
    Pls suggest !!!
    Raja

    mmmh. Did you compared the select count(*) from your_table; in the two cases.
    If the result is not good and nobody has deleted rows between migration and your test, you migration need to be replayed.
    Which migration did you select, Transportable database or exp/imp...?
    Edited by: Dba Z on 16 août 2009 08:56

  • Show rows with empty values

    Hello,
    We are creating a cross tab report, with products as the columns, and relationship managers with direct and shared revenue displayed across the rows.
    For Eg
    Relationship Manager    Coverage         Product1   Product2   Product3   Product4
    Bob                                Direct                 100,           0,               35,            50
                                          Shared                0,              0,               15,            0
    Alex                               Direct                  15,            25,             40,            10
                                          Shared                5,              0,               5,               0
    George                          Direct                  0,               0,              0,               30
                                          Shared                0,               0,              0,               5
    The problem lies when either one of the products has no values, or one of the relationship managers has no direct or shared revenues. If one of the products has no revenues, it disappears, and also when one of the RMs has no direct or shared revenues, the Direct or Shared row disappears.
    I have tried to create a second query just included the Coverage variable, and using this variable in Query 2 with the RM variable from query 1, along with checking the "Show Rows with Empty Measure values", "Show Rows with Empty Dimension Values" and "Show when empty", but it still does not appear to work.
    It only shows rows with values in them, and shows the rows with empty values at the end with no RM.
    I have not tried to solve for empty columns yet.
    Help with this would be much appreciated!!!!!!!!
    Thank you

    Hi ,
    I think you can resolve this issue following ways ,
    you need create another query  ( Ex :Qauery2 ) add Product object and RM (Direct,shared) object only without  conation. So now query2 result wil have all product and RM(Direct,shared).
    Merge the Product object and RM  between query1 and query2
    Now create table using Product and RM from query2 and measure value from query1.  Now you will get row even there is no data.
    If you want try this sample report using efasion unvierse.
    1) Add year ,state and Discount objects  and apply condition Year Not in list "2004"  and state Not in list "California "
    2) run this query and create the cross tab table . Now you will not get 2004 column  and California row in table.
    3) Create query2 add Year and State only without any condition , run the query. Now query 2 will display all state and year .
    4) Merge the column Year and State between query1 and query2
    4) Create the cross table ussing Year and state from query2 and Discount from Query1
    Now cross table will show 2004 and California ,even there is not in query 1.
    I hope this will help you.
    Ponnarasu
    Edited by: ponnarasuk on Dec 7, 2011 12:48 PM

  • How to select textFrames with tracking different to 0 (zero)?

    How to select textFrames with tracking different to zero (greater or smaller than zero)?
    Thanks.

    Hi All,
    Thanks for your help. The orignal clips are from Sony PMW F3 camera and I have got Dimensions: 1920 × 1080, Codecs: MPEG-2 Video, Linear PCM, Timecode, Color profile: HD (1-1-1), Total bit rate: 36,623.
    The other camera has given me .MTS files which I have converted in MPEG-2 also. I used Wondershare Video Converter to convert second cameras clip.
    I don't think the clips are matching with first camera's clips because still that is saying the same thing.
    Regards,
    Jai

  • How to display time with min ,sec in select lis lov - 0-23 hrs and 0-59 min

    Hi,
    How to use time between 0-23 hrs,0-59 min in select list and insert selected value into single column.Like if i select 20:00 hrs,34 min ,value should be insert into single database column.
    Please help me to get the answer of this question.
    Kind Regards,
    Harish Sharma

    One select list would have 24 x 60 entriies. You should use two. One for hours and the other one for minutes. The SQL could be:
    SELECT     LEVEL d, LEVEL r
          FROM DUAL
    CONNECT BY LEVEL < 25;
    SELECT     LEVEL d, LEVEL r
          FROM DUAL
    CONNECT BY LEVEL < 61;The source is static. The actual column (for example :P1_TIME_COLUMN) with source database column is hidden. You would have a computation (PL/SQL Function) on submit which would then bring the two selected values together:
    DECLARE
       v_time   VARCHAR2 (40);
    BEGIN
       v_time := :p1_select_list1 || ':' || :p1_select_list2;
       RETURN v_time;
    END;Denes Kubicek
    http://deneskubicek.blogspot.com/
    http://www.apress.com/9781430235125
    http://apex.oracle.com/pls/apex/f?p=31517:1
    http://www.amazon.de/Oracle-APEX-XE-Praxis/dp/3826655494
    -------------------------------------------------------------------

  • Report Painter - how to insert sub header rows with no values

    In a report painter, I am designing a report with several rows with formulas. I need to insert a sub-header row in the middle of the rows. That will not have a value. It shall contain no value, but I am given only options of either insert a row of characteristics or a formula. I need neither of them. Can we have something excel  like feature in report painter (4.6c) just to insert a sub header row? If so how do we get that.
    e.g
    ADMINISTRATIVE EXPENSES (how to insert this row?)
    Account 1 - 10

    Hi,
    try to add a formular row containing a formular like = +x -x.
    Maybe you can use "formatting / row" and use overscores / underscores to get a blank row (underscore one line, overscore the line thats following).
    No other idea...
    best regards, Christian

Maybe you are looking for