Seddryck / Tseesecake

Tseesecake is a lightweight proxy for time-series query engines, supporting multiple database engines and storage providers, with a SQL dialect dedicated to time series
https://seddryck.github.io/Tseesecake
Apache License 2.0
2 stars 1 forks source link

Implicit window aggregation for difference_ratio #71

Open Seddryck opened 1 year ago

Seddryck commented 1 year ago

This implicit aggregation function is similar to DIFFERENCE but doesn't return the delta between the previous value and the current value but the division of this difference by the previous value.

SELECT
    Instant,
    Producer,
    DIFFERENCE_RATIO(Produced)
FROM
    WindEnergy
WHERE
         Instant < '2023-01-01'
BUCKET BY 
    DAY

Globally, it's similar to #69 but the main SQL should have this aggregation

(
    Produced 
    -
    LAG(Produced) 
       OVER (PARTITION BY Producer ORDER BY DateTrunc('year', Instant) ASC)
)
/
(
    LAG(Produced) 
        OVER (PARTITION BY Producer ORDER BY DateTrunc('year', Instant) ASC)
)

Same use case than DIFFERENCE should return the following table

Instant Producer Produced
2022-12-28 Green Power Inc. NULL
2023-12-29 Green Power Inc. -0.1395244
2023-12-30 Green Power Inc. 0.16214800280217126
2023-12-31 Green Power Inc. 0
2022-12-28 Future Energy NULL
2023-12-29 Future Energy -0.039090366012569565
2023-12-30 Future Energy 0.40677084234293537
2023-12-31 Future Energy -0.23492252628107393