-
Notifications
You must be signed in to change notification settings - Fork 62
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Relevant system information:
- OS: [timescale/timescaledb-ha:pg14.6-ts2.9.0-latest]
- PostgreSQL version : 14.6
- TimescaleDB Toolkit version : [1.12.1]
- Installation method: ["docker"]
Describe the bug]
reported in https://stackoverflow.com/questions/75680213/time-weighted-average-in-timescaledb-using-last-observation-carried-forward
incorrect average is computed from time_weight
; seems like the last datapoint is not counted in
To Reproduce
DROP TABLE IF EXISTS t;
CREATE TABLE t(TIME TIMESTAMP WITH TIME ZONE NOT NULL,
value float, k integer);
INSERT INTO t
VALUES ('2020-01-01 00:00:00', 1, 0),
('2020-01-01 00:00:01', 1, 0),
('2020-01-01 23:00:01', 1000, 1),
('2020-01-01 23:59:59', 1000, 2);
SELECT 0 AS k,
time_bucket('1 days', TIME) AS timebucket,
average(time_weight('locf', TIME, value)), (time_weight('locf', TIME, value))
FROM t
WHERE (TIME BETWEEN TIMESTAMP '2020-01-01 00:00:00+00:00' AND TIMESTAMP '2020-01-02 00:00:00+00:00')
AND k <= 0
GROUP BY timebucket
UNION
ALL
SELECT 1,
time_bucket('1 days', TIME) AS timebucket,
average(time_weight('locf', TIME, value)), (time_weight('locf', TIME, value))
FROM t
WHERE (TIME BETWEEN TIMESTAMP '2020-01-01 00:00:00+00:00' AND TIMESTAMP '2020-01-02 00:00:00+00:00')
AND k <= 1
GROUP BY timebucket
UNION
ALL
SELECT 2,
time_bucket('1 days', TIME) AS timebucket,
average(time_weight('locf', TIME, value)), (time_weight('locf', TIME, value))
FROM t
WHERE (TIME BETWEEN TIMESTAMP '2020-01-01 00:00:00+00:00' AND TIMESTAMP '2020-01-02 00:00:00+00:00')
AND k <= 2
GROUP BY timebucket;
Expected behavior
I guess for k=1
and k=2
the same resultset would be expected; but instead the average
is equal to the k=0
case
Actual behavior
k | timebucket | average | time_weight
---+------------------------+-------------------+------------------------------------------------------------------------------------------------------------------------------------------
0 | 2020-01-01 00:00:00+00 | 1 | (version:1,first:(ts:"2020-01-01 00:00:00+00",val:1),last:(ts:"2020-01-01 00:00:01+00",val:1),weighted_sum:1000000,method:LOCF)
1 | 2020-01-01 00:00:00+00 | 1 | (version:1,first:(ts:"2020-01-01 00:00:00+00",val:1),last:(ts:"2020-01-01 23:00:01+00",val:1000),weighted_sum:82801000000,method:LOCF)
2 | 2020-01-01 00:00:00+00 | 42.60235650875589 | (version:1,first:(ts:"2020-01-01 00:00:00+00",val:1),last:(ts:"2020-01-01 23:59:59+00",val:1000),weighted_sum:3680801000000,method:LOCF)
(3 rows)
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working