Downsampling data into another bucket

Hello all,
With “Tasks” in InfluxDB v2.0, is it now possible to downsample to another bucket while retaining the measurement name?

@t481 Yes, it’s totally possible. The following Flux task downsamples the data by hour and preserves the measurement and all tags on each data point:

option task = {
  name: "ds-1h",
  every: 1h
}

from(bucket: "example-bucket")
  |> range(start: -task.every)
  |> filter(fn: (r) => r._measurement == "foo" and r._field == "bar")
  |> mean()
  |> to(bucket: "downsample-bucket", org: "example-org")

Thank you @scott. That’s really good to hear. I’m completely new to Flux and I’m struggling with the syntax to be honest.
Is there a way to select all? like

|> filter (fn: (r) => r.*)

You can just leave out the filter function. That will return everything in the queried time range. There’s no way to query InfluxDB without a time range however.

Fair warning on a query like this… it can be VERY “heavy.”

Hopefully one final thing? Isn’t “aggregateWindow” the right function to use in place of GROUP BY time (60m)?

If I run it it says "aggregateWindow: missing time column “_time”


    option task = {
      name: "ds-1h",
      every: 1h
    }

    from(bucket: "example-bucket")
      |> range(start: -task.every)
      |> mean()
      **|> aggregateWindow(every: 1h, fn: mean, createEmpty: false)**
      |> to(bucket: "downsample-bucket", org: "example-org")

I managed to figure out the solution

option task = {
      name: "ds-1h",
      every: 1h
    }

    from(bucket: "example-bucket")
      |> range(start: -task.every)
      |> aggregateWindow(every: 1h, fn: mean, createEmpty: false)
      |> to(bucket: "downsample-bucket", org: "example-org", timeColumn:"_stop")

Edit:
Just wanted to add that the above works for buckets with numbers. The same task does not work for buckets with strings as the mean function cannot run against these. Still trying to figure out how to get it to select the latest string value for none int fields