Results 1 to 2 of 2
  1. #1
    Join Date
    May 2009
    Posts
    4

    Unanswered: Select / Group by question

    Hello.

    I have the following table:

    ID | SubId | Param1 | param2 | Time
    ------------------------------------------------------
    1 | 1 | A | 2 | 2009-12-06 11:20:05.000
    1 | 1 | A | 3 | 2009-12-06 13:11:30.000
    1 | 2 | B | 1 | 2009-12-06 13:06:56.000

    The output I need is row for each SubId with the max time:
    From the above example its the second and third rows.
    In the output I need all the data from the row.

    Thank you very much for your help.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT t.ID
         , t.SubId
         , t.Param1
         , t.param2
         , t.Time
      FROM ( SELECT SubId
                  , MAX(Time) AS latest
               FROM daTable
             GROUP 
                 BY SubId ) AS m
    INNER
      JOIN daTable AS t
        ON t.SubId = m.SubId
       AND t.Time = m.latest
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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