Results 1 to 3 of 3

Thread: select top 3

  1. #1
    Join Date
    Oct 2003
    Posts
    2

    Unanswered: select top 3

    I have a table that holds comment records. The columns are :
    user_id, comment, date

    How can I write a select statement that returns the 3 most recent comment records for each user, all in one recordset.

    thanks for any help.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    select user_id
         , comment
         , thedate
      from yourtable zzz
     where ( select count(*)
               from yourtable 
              where user_id = zzz.user_id
                and thedate > zzz.thedate ) < 3      
           )
    order
        by user_id
         , thedate desc
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Oct 2003
    Posts
    2
    thanks r937, did the trick.

Posting Permissions

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