Sql guru please help!!!!!

Results 1 to 8 of 8

Thread: Sql guru please help!!!!!

  1. #1
    Ed Guest

    Default Sql guru please help!!!!!

    Is there a way to select the first occurrence of a record which is also the LATEST record???<BR><BR>For example, I have the following records:<BR>comments date_time user<BR>-------- --------- --------<BR>blah 5/3/2001 4:10pm a<BR>my comments 5/3/2001 4:20pm b<BR>a&#039;s message 5/3/2001 4:30pm a<BR>b&#039;s message 5/3/2001 4:31pm b<BR>....<BR><BR>I only want to get user A&#039;s lastest comment. How???? Any help would be greatly appreciated!!!<BR><BR><BR>

  2. #2
    Brian Ellertson Guest

    Default RE: Sql guru please help!!!!!

    You could select all of user A&#039;s records and order by the date (descending). The first record in the recordset would be his latest. <BR><BR>

  3. #3
    Join Date
    Dec 1969
    Los Angeles, CA

    Default Yeah...AND

    use "top". You do not want all the records and only the top one why waste time/memory selecting ALL the records. If you only want one record use TOP 1.

  4. #4
    Ed Guest

    Default RE: Sql guru please help!!!!!

    I&#039;ll try that. Thanks Brian!

  5. #5
    Ed Guest

    Default RE: Yeah...AND

    Like this? <BR>SELECT TOP 1 comments WHERE user=&#039;a&#039; <BR>and it&#039;ll automatically select the LATEST record? <BR><BR>I&#039;ll try it though. Many Many thanks Akhilesh!!!

  6. #6
    Jeremy_D Guest

    Default RE: Yeah...AND

    You need to include an ORDER BY clause in your TOP query, or SQL Server won&#039;t know what the top is:<BR><BR>SELECT TOP 1 comments FROM YourTable WHERE user = &#039;a&#039; ORDER BY date_time DESC<BR><BR>Alternatively, you can use:<BR><BR>SELECT comments FROM YourTable WHERE user = &#039;a&#039; AND date_time = (SELECT Max(date_time) FROM YourTable WHERE user = &#039;a&#039;)<BR><BR>(for instance on SQL Server 6.5 which doesn&#039;t support the TOP clause)<BR><BR>Or:<BR><BR>SET ROWCOUNT 1<BR>SELECT comments FROM YourTable WHERE user = &#039;a&#039; ORDER BY date_time DESC<BR>SET ROWCOUNT 0<BR><BR>(also good for SQL Server 6.5 or lower).<BR><BR>Cheers,<BR>Jeremy_D<BR>

  7. #7
    Ed Guest

    Default THANK YOU!!!


  8. #8
    Jeremy_D Guest

    Default You're welcome!


Posting Permissions

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