SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Oct 2001
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Latest create time on distinct column

    Hi, I was looking to get distinct values from a column, but wanted to get the latest create time.

    Code:
    ID   CREATETIME   EVENT   UPDATEDBY
    1    2013-1-1     1       Bob
    2    2013-1-3     1       John
    3    2013-1-6     3       Dave
    4    2013-1-8     2       Jim
    5    2013-1-15    3       Jess
    I would be looking unique event numbers, but with the createtime and its updatedby field. I don't think the distinct will be any use, because when attempting to do that it comes back with everything (as all rows are distinct).

    Instead, rather have this:
    Code:
    ID   CREATETIME   EVENT   UPDATEDBY
    2    2013-1-3     1       John
    4    2013-1-8     2       Jim
    5    2013-1-15    3       Jess
    Any ideas greatly appreciated.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT t.id
         , t.createtime
         , t.event
         , t.updatedby
      FROM ( SELECT event
                  , MAX(createtime) AS latest
               FROM daTable
             GROUP
                 BY event ) AS d
    INNER
      JOIN daTable AS t
        ON t.event = d.event
       AND t.createtime = d.latest
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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