Q:

How to loop SELECT SUM of daily MAX and MIN

I have 2 tables that I'm trying to select, unit and feed. 
Table unit is a metadata table that contains
unitid, userid, guid, fields, createdat

and the other table, feed, contains
feedid, unitid, value, createdat

Sample data for table unit:

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'

and table feed:

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'

What I'm trying to get is select total of MIN and MAX of every unit registered by userid = n.
I already tried this:
SELECT SUM(value::DOUBLE PRECISION)::text FROM 
((SELECT f.value FROM feed f, unit u WHERE f.unitid = unit.id AND f.createdat >= 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 >= now()::date + interval '1h' ORDER BY f.createdat ASC LIMIT 1)) t

But I'm not sure where I should put the userid = n clause. 
So how can I select total of MIN and MAX of every units registered by spesific userid? ( daily interval)

A:

Is this what you are looking for? 
SELECT f.unitid,
       min(f.value) + max(f.value)
FROM unit u
   JOIN feed f ON u.id = f.unitid
WHERE u.userid = 1000
GROUP BY f.unitid;

┌────────┬──────────┐
│ unitid │ ? column?  │
├────────┼──────────┤
│      1 │   0.0821 │
│      2 │   0.0548 │
└────────┴──────────┘
(2 rows)

