Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2009
    Posts
    23

    Unanswered: write a right sql statement to get this job done

    Hi guys, may I have your attention and help on this. Thanks!

    The table "message(id, userId, content, createdTime)". now I need to find out those records of the latest message (namely the max createdTime) for each user, the returned results should be ordered by createdTime.
    e.g
    data in the table:
    1 'eric' 'hello' '2009-12-30'
    2 'lisa' 'yes' '2010-01-21'
    3 'eric' 'thanks' '2009-10-06'
    4 'lisa' 'hey' '2010-09-12'

    I got one: select msg. * from (select m.* from message as m order by m.createdTime) as msg group by msg.userId order by msg.createdTime DESC. but I think it's not good due to low performance.

    the expected results:
    4 'lisa' 'hey' '2010-09-12'
    1 'eric' 'hello' '2009-12-30'
    Last edited by chiefman; 03-12-10 at 12:21.

  2. #2
    Join Date
    Dec 2007
    Location
    Richmond, VA
    Posts
    1,328
    Provided Answers: 5
    What's going on? This same type question is popping up on all of the DB boards. The SQL is fairly simple. You want a correlated subquery, where you join the table to itself looking for the max date/time for that person.
    Dave

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    Quote Originally Posted by dav1mo View Post
    What's going on? This same type question is popping up on all of the DB boards. The SQL is fairly simple.
    Lecturer sets homework. Students post their homework on their chosen forums. We spend half our day doing the students homework. Students then finish their course but are mysteriously incapable of doing anything. No one wants to employ them as they know nothing. Parents wonder why they spent their money on the course fees etc.

    Does that explain things?

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by mike_bike_kite View Post
    No one wants to employ them as they know nothing.
    other than this part, your synopsis of the situation is bang on

    homework time comes in cycles, and i've noticed on several boards we are in another cycle

    the part above isn't quite right -- they ~do~ get hired, and then, from the comfort of a paid job, post questions like "halp!!! how do i remove all but one of the duplicates in my table???"
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Now come on Rudy, when Mike wrote that "No one wants to employ them" I think he was spot on in his analysis... That doesn't mean that someone might not be forced to employ them, that's a very different thing!

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    forced? how forced?

    anyhow, my point was that the employer might not know that the candidates are under-qualified, simply by virtue of the fine marks that they pulled off for their assignments

    of course, rigourous testing as part of the interview process should take care of that...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  7. #7
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    I'll give you a call to share some horror stories about our recent interviews. You are hands down a better administrator than some of the folks that have presented resumes lately.

    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  8. #8
    Join Date
    Jan 2009
    Posts
    23
    Quote Originally Posted by mike_bike_kite View Post
    Lecturer sets homework. Students post their homework on their chosen forums. We spend half our day doing the students homework. Students then finish their course but are mysteriously incapable of doing anything. No one wants to employ them as they know nothing. Parents wonder why they spent their money on the course fees etc.

    Does that explain things?
    I am shamed at the simple sql question. right, your words do mean something to me. to be honest, I am fresh to such stuff like database. I sense that you all are experts. Anyway, thank you

Posting Permissions

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