kuzyn
kuzyn

Reputation: 1995

Segment aggregate value by time while applying function to previous data points

Some basic stuff: I'm trying to create a "running" query where for every datapoint, I get the mean & count up to that point, alongside the value. For instance for these measurements:

hunger=1 1001
hunger=3 1002
hunger=5 1003
hunger=20 1004

I want to produce a csv that looks like:

timestamp, hunger, mean, count,
1001, 1, 1, 1
1002, 3, 2, 2,
1003, 5, 3, 3
1004, 20, 7.25, 4

Since this is a mix of aggregate & selector query, I have "hunger" in a separate query then mean & count. But if I group both by time(1s), my mean and count will only be relative to the group size; I need it to be relative to the whole set up to that point... Any suggestions?

Upvotes: 0

Views: 38

Answers (1)

Yuri Lachin
Yuri Lachin

Reputation: 1500

You can try smth like this:

SELECT last(hunger), cumulative_sum(last(hunger)) / cumulative_sum(count(hunger)), cumulative_sum(count(hunger)) FROM hungerMeasurement group by time(1s)

Upvotes: 2

Related Questions