title: how can I get the last value for each key in a set of postgres hstore records?
body: <p>I have a table of sensor readings as postgres <code>hstore key-value pairs</code>.
  Each record has a timestamp at 1 sec intervals.
  Not all sensors record every second.</p>
  <p>The table is essentially:</p>
  <pre><code>create table readings (
      timer timestamp primary key,
      readings hstore
  );
  </code></pre>
  <p>the hstore comprises (&lt;sensor_id&gt; ) key/value pairs for readings taken at the time specified in the timestamp.</p>
  <p>eg:  &quot;67&quot; &quot;-45.67436&quot;, &quot;68&quot; &quot;176.5424&quot; could be key/value pairs representing latitude &amp; longitude, with a timestamp in the timer column.</p>
  <p>There would be several lat/lon hstore pairs in a given minute, the query I want would return the last one in the timeseries for that minute (for each key).</p>
  <p>I want to retrieve the latest value for each sensor in the hstore at 1 minute intervals.</p>
  <p>Essentially:
  Get the set of timestamps &amp; hstores for that minute.
  Get the keys from that set.
  Get the value for that key from the key-value pair with the latest timestamp in the set
  Return the 1 min timestamp &amp; resulting hstore
  Repeat for the next minute</p>
  <p>I'm happy with a pl/pgsql function if that is the best approach.</p>