Itt tartok:
select u.hour, MAX(u.val) - MIN(u.val) AS diff
from (
(select date_trunc('hour', to_timestamp(ts / 1000)) as hour, val::numeric
from ts_string
where id = 2
order by 1, 2 asc)
) as u
group by u.hour order by u.hour;
bar nem tudom, hogy ez mennyire elegans...