Set a tag with into command?

Hello,

can I add a tag to all values in a measurement by command.

I have exported all values without the tag “item” to temp:

select * into temp from hz_mqtt_aussentemp where item = '‘

and then wrote these values back:

select * into hz_mqtt_aussentemp from temp

Can I set a tag item = ‘hz_mqtt_aussentemp’ for each value here?

Thank you

@itob Once a value is written as a field, you can’t change it to a tag unless you rewrite the data to another database. To set a column as a tag with INTO, you need to include a GROUP BY clause and group by all the columns you want to write as tags.

Hello,

how can I use group with

select * from hz_mqtt_aussentemp where...

change:

time                item value

1755372000774000000      15.7

to:

time                item               value

1757096400905000000 hz_mqtt_aussentemp 13.3

?

Oh, I see. The tag doesn’t already exist. InfluxQL doesn’t let you set arbitrary columns values (ex: SELECT 'foo' as bar ...), so you won’t be able to do this with InfluxQL. If you’re using InfluxDB v1, I think the only option would be to query the data out, transform back into line protocol but include the new tag, then rewrite it back to InfluxDB, but in a new db or measurement.

Hello,

in influxdb 1.8 the export and import works in the line protocol.

Does this also work in influxdb2?

Thank you.

If you’re using InfluxDB v1.8, it does support Flux, you just have to enable it. You could perform this type of operation with Flux. Flux requires time bounds on your query, so you may need to run this query in time-based batches if you have a lot of old data. The query itself would look something like this:

from(bucket: "example-db/example-rp")
    |> range(start: -30d)
    |> filter(fn: (r) => r._measurement == "hz_mqtt_aussentemp")
    |> map(fn: (r) => ({r with item: "hz_mqtt_aussentemp"}))
    |> group(columns: ["_time", "_value"], mode: "except")
    |> to(bucket: "example-db/example-rp")

To define a time-based batch, you’d update the range() call. For example:

|> range(start: -60d, stop: -30d)

Thank you very much. I’ll have to take a closer look at that.

Is there also a possibility in influxdb2 to export and import data in the line protocol?