title: Obtain record from sqlite by most recent date
body: <p>I have an Objective-C app that utilizes CoreData and the backing store is a sqlite database.</p>
  
  <p>I'm trying to formulate a query for the <a href="http://ccgus.github.io/fmdb/html/index.html" rel="nofollow noreferrer" title="FMDatabase">FMDatabase</a> to access the most recent record according to the MediaDate.</p>
  
  <p>Here is the table I'm querying, called <code>MediaAccy</code> which has a <code>MediaDate</code> field which is of type <code>timestamp</code> (pictured here)</p>
  
  <p><a href="https://i.stack.imgur.com/dzhu0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dzhu0.png" alt="enter image description here"></a></p>
  
  <p>Here's a query that simply ORDERS the MediaDate field.  Notice the <code>ZMEDIADATE</code> field has 2 identical <code>timestamp</code> values, and notice the order of the guids.
  According to this query, the most recent record has guid <code>6BBF7...</code></p>
  
  <p><a href="https://i.stack.imgur.com/womi8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/womi8.png" alt="enter image description here"></a></p>
  
  <p>Now, notice THIS query, where I ask for the record with the MAX date from the given set of records above, and notice the result is for guid <code>2D8AC...</code> which according to the previous query, that ordered that same data by date, is the 2nd to most recent, not the most recent record.  </p>
  
  <p>Will the real most recent record please stand up!</p>
  
  <p><a href="https://i.stack.imgur.com/qeoCR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qeoCR.png" alt="enter image description here"></a></p>
  
  <p>According to sqlite's documentation for the <a href="https://www.sqlite.org/lang_aggfunc.html#maxggunc" rel="nofollow noreferrer" title="max&#40;&#41;">max()</a> function...</p>
  
  <blockquote>
    <p>The max() aggregate function returns the maximum value of all values in the group. The maximum value is the value that would be returned last in an ORDER BY on the same column</p>
  </blockquote>
  
  <p>But according to my results, this isn't what in fact is happening.</p>