title: How to loop SELECT SUM of daily MAX and MIN
body: <p>I have 2 tables that I'm trying to select, unit and feed.</p>
  
  <p>Table <code>unit</code> is a metadata table that contains</p>
  
  <pre><code>unitid, userid, guid, fields, createdat
  </code></pre>
  
  <p>and the other table, <code>feed</code>, contains</p>
  
  <pre><code>feedid, unitid, value, createdat
  </code></pre>
  
  <p>Sample data for table unit:</p>
  
  <pre>
  1,1000,'0b243396-a015-11e6-8001-9cf387d90940','a','2016-11-04 21:36:32.71281'
  2,1000,'0b243396-a015-11e6-8001-9cf387d90941','b','2016-11-04 21:40:32.71281'
  3,1001,'0b243396-a015-11e6-8001-9cf387d90942','b','2016-11-04 21:47:32.71281'
  4,1001,'0b243396-a015-11e6-8001-9cf387d90943','c','2016-11-04 21:50:32.71281'
  </pre>
  
  <p>and table feed:</p>
  
  <pre>
  1,1,'0.0021','2016-11-05 00:50:32.71281'
  2,1,'0.0025','2016-11-05 01:00:32.71281'
  3,2,'0.0008','2016-11-05 00:51:22.71281'
  4,2,'0.0018','2016-11-05 01:01:22.71281'
  ...
  24,1,'0.0800','2016-11-05 23:55:32.71281'
  25,2,'0.0540','2016-11-05 23:55:32.71281'
  </pre>
  
  <p>What I'm trying to get is select total of MIN and MAX of every unit registered by userid = n.</p>
  
  <p>I already tried this:</p>
  
  <pre><code>SELECT SUM(value::DOUBLE PRECISION)::text FROM 
  ((SELECT f.value FROM feed f, unit u WHERE f.unitid = unit.id AND f.createdat &gt;= now()::date + interval '1h' ORDER BY f.createdat DESC LIMIT 1) 
  UNION ALL 
  (SELECT f.value FROM feed f, unit u WHERE f.unitid = unit.id AND f.createdat &gt;= now()::date + interval '1h' ORDER BY f.createdat ASC LIMIT 1)) t
  </code></pre>
  
  <p>But I'm not sure where I should put the userid = n clause.</p>
  
  <p>So how can I select total of MIN and MAX of every units registered by spesific userid? (daily interval)</p>