select *, case when net_in < net_in_prev then (power(2, 64) - net_in_prev + net_in) else (net_in - net_in_prev) end as net_in_rollfixed, case when net_out < net_out_prev then (power(2, 64) - net_out_prev + net_out) else (net_out - net_out_prev) end as net_out_rollfixed from (select "timestamp", extract(epoch from ("timestamp" - lag("timestamp") over (order by "timestamp" asc))) as "timestamp_delta", net_in, lag("net_in") over (order by timestamp asc) as net_in_prev, net_out, lag("net_out") over (order by timestamp asc) as net_out_prev from (select t1."timestamp", t1."net_in", t2."net_out" from (select "timestamp", "resource_id", "value" as "net_in" from "snmp_resources:statuses" where "resource_id" = {{id}} and "oid" = 'ifHCInOctets' order by "timestamp" ASC) as t1 inner join (select "timestamp", "resource_id", "value" as "net_out" from "snmp_resources:statuses" where "resource_id" = {{id}} and "oid" = 'ifHCOutOctets' order by "timestamp" ASC) as t2 on (t1."timestamp" = t2."timestamp") where t1."timestamp" > now() at time zone 'utc' - interval '{{interval}}') as t3) as t4