Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2009
    Posts
    3

    Unanswered: MAX(DATE) Shows Irregulare Values

    Hi All

    I am running the below query to try and extract the last date and time that a user entered an activity.

    The list returns the correct date and time BUT
    the Subject is a random row from the database.

    2013-11-19 05:20:45 Dominic,Doyle Battery: 4.05V 30408880

    If a run a complete list of activities from the user and manuall select the latest row i see:

    2013-11-19 05:20:45 Dominic,Doyle Low Battery 34989316

    As you can see the Date/Time is identical, but the Subject and RowID are different.
    Can anyone see what im doing wrong or suggest a better way to do it?

    Thanks,

    PHP Code:
    SELECT 
    max
    (smsreceived) as Date,
    contacts.firstname as First
    contacts.lastname as Last,
    activities.visiondescription as Subject,
    activities.RowID,
    FROM activitiescontacts 
    WHERE activities
    .contactid=contacts.contactid 
    AND activities.accountid=contacts.accountid
    AND activities.contactid 'AAXA-TGZQY62301'

  2. #2
    Join Date
    Nov 2003
    Posts
    2,933
    Provided Answers: 12
    That's because your (non-)use of GROUP BY is invalid SQL - only MySQL chooses to return random data instead of rejecting the query as every other DBMS does.

    Please read these links in order to understand why your aggregation is invalid:

    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags: http://www.dbforums.com/misc.php?do=bbcode#code

    Tips for good questions:

    http://tkyte.blogspot.de/2005/06/how...questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html

Posting Permissions

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