1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Rep Power

    Return Last Row by Each ContactID

    Hi All,

    I dont know if this is possible.
    I am trying to run a query that will show me the last row in a table for each individual contactid in a given account.

    something like:

    SELECT contacitd, completeddate FROM activities where accountid = 'AAXA-SZMY8K' ORDER BY contactid DESC LIMIT 10;

    The problem is i dont always know how many contactid's will be in a given accountid, so the DESC LIMIT 10 wont work for me.
    Also, the above query returns the last 10 rows of a given accountid regardless of contactid, so it usually returns the last 10 rows for a single contactid rather than show the time/date of the last activity for each contactid.

    Im running this to export to a csv file, so any suggestions would be welcome.

  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Rep Power
    If you only need contactit and completedate you should be able to get it with this statement (not tested):
    SELECT contacitd, MAX(completeddate)
    FROM activities 
    WHERE accountid = 'AAXA-SZMY8K'
    GROUP BY contactid
    If you need more information, you will have to make a (self-)join based on the id and date.

IMN logo majestic logo threadwatch logo seochat tools logo