Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2013
    Posts
    6

    Post Unanswered: How to find max date for datetime field

    Dear all,

    I want to unique id and max datetime in timestamp field.
    for example below result i am getting.

    col1 col2 col3 col4(yyyy-mm-dd)
    ---- ---- -------- -----------------------
    1 7878 2014-1-5 2014-01-05 15:24:02.078
    2 6485 2014-1-3 2014-01-03 09:14:02.078
    3 3265 2014-1-4 2014-01-04 15:24:02.078
    4 7878 2014-1-2 2014-01-02 18:10:29.078
    5 3265 2013-12-22 2013-12-06 12:14:02.078
    6 7878 2013-12-22 2013-12-08 15:24:02.078

    But, I want to below result:

    col1 col2 col3 col4(yyyy-mm-dd)
    ---- ---- -------- -----------------------
    2 6485 2014-1-3 2014-01-03 09:14:02.078
    3 3265 2014-1-4 2014-01-04 15:24:02.078
    4 7878 2014-1-8 2014-01-02 18:10:29.078
    Pls help....

    Regards,
    Tamil

  2. #2
    Join Date
    Apr 2006
    Location
    Belgium
    Posts
    2,514
    Provided Answers: 11
    maybe a group by can help combined with max
    see infocenter for syntax
    Best Regards, Guy Przytula
    Database Software Consultant
    Good DBAs are not formed in a week or a month. They are created little by little, day by day. Protracted and patient effort is needed to develop good DBAs.
    Spoon feeding : To treat (another) in a way that discourages independent thought or action, as by overindulgence.
    DB2 UDB LUW Certified V7-V8-V9-V9.7-V10.1-V10.5 DB Admin - Advanced DBA -Dprop..
    Information Server Datastage Certified
    http://www.infocura.be

  3. #3
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    But, I want to below result:

    col1 col2 col3 col4(yyyy-mm-dd)
    ---- ---- -------- -----------------------
    2 6485 2014-1-3 2014-01-03 09:14:02.078
    3 3265 2014-1-4 2014-01-04 15:24:02.078
    4 7878 2014-1-8 2014-01-02 18:10:29.078
    How to get the value of col3 in the row with col1 = 4?

  4. #4
    Join Date
    Dec 2007
    Location
    Richmond, VA
    Posts
    1,328
    Provided Answers: 5
    Code:
    select columns
       from table a
    where date_col = (select max(date_col)
                                 from table b
                              where a.key = b.key)
    ....

  5. #5
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    3,483
    I thought that I had posted sometimes for this kind of issues.

    Anyhow,
    another way might be...
    Code:
    SELECT col1 , col2 , col3 , col4
     FROM  (SELECT s.*
                 , ROW_NUMBER() OVER(PARTITION BY col2
                                         ORDER BY col4 DESC) AS r_num
             FROM  <table> AS s
           )
     WHERE r_num = 1
     ORDER BY
           col1
    ;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •