0
votes

I am writing a query in InfluxDB where my measurement have multiple tags. I would like to group by on a particular tag and time and do a mean on a field and also get the last value in that group for another tag value.

For example: I have 2 tags: tag1 and tag2 and a field field1. I would like to GROUP BY on tag1 and do a mean on field1 and take the last value of tag2.

For this when I write a query like this, I do not get the tag value: SELECT mean(field1), last(tag2) FROM "measurement" GROUP BY tag1

Please let me know if I am doing something wrong. My tag2 value is a string field.

2

2 Answers

1
votes

This is how I was finally able to make it work for me:

SELECT tagValue,newField FROM (SELECT mean(currentField) AS newField FROM forever.theData WHERE currentField > 0 AND time > now() - 100d GROUP BY time(1d),tagValue)

0
votes

I don't think it's possible with a single query. You cannot call last on a tag key: tags are bound to fields, therefore you must select a specific fields last value. Then you can also query a tag bound to that field value. Also, if you write mean(field1) and last(field1) in one SELECT statement you cannot retrieve tag2 for last(field1) (it cannot be determined if it's for mean or last). You should try something like this:

SELECT mean(field1) FROM "measurement" GROUP BY tag1
SELECT last(field1), tag2 FROM "measurement" GROUP BY tag1