databasetime-seriesquestdb

Math operations with lag()


I have a questdb 8.3.0 and i want to graph a typical snmp counter containing network incoming traffic (ifHCInOctets).

If I run this query:

SELECT 
    timestamp,
    ifDescr,
    ifHCInOctets,
    (LAG(ifHCInOctets,1)) OVER (PARTITION BY ifDescr ORDER BY timestamp) as previous
FROM snmpInterface
WHERE ifDescr = 'ether8' AND timestamp > 1745792550000000;

I get the current value and the previous one:

![image|690x477](upload://5tyimCZ73QQEB8Pvf6uuponNjD3.png)

However, I want to get the substracted value (ifHCInOctets - LAG(...)) and I can't get it :(

this query works with InfluxDB 3.0:

SELECT ("ifHCInOctets" - LAG("ifHCInOctets", 1) OVER (PARTITION BY "ifDescr" ORDER BY time
  )) AS in, ("ifHCOutOctets" - LAG("ifHCOutOctets", 1) OVER (PARTITION BY "ifDescr" ORDER BY time
  )) AS out, time FROM "snmpInterface" WHERE "ifDescr" == 'ether8' AND "time" >= $__timeFrom AND "time" <= $__timeTo ORDER BY "time" ASC 

is it possible to have the same query with QuestDB ? How? thanks.


Solution

  • It should work using a subquery around the query with the window function:

    SELECT timestamp, ifDescr, ifHCInOctets - previous AS difference FROM (
        SELECT 
            timestamp,
            ifDescr,
            ifHCInOctets,
            (LAG(ifHCInOctets,1)) OVER (PARTITION BY ifDescr ORDER BY timestamp) as previous
        FROM snmpInterface
        WHERE ifDescr = 'ether8' AND timestamp > 1745792550000000
    );