1

I am using influxQL to query data from influxdb in order to see visualisations on Grafana. There are columns in the table , those are basically fields and tags, I want to count the distinct values of a tag column by grouping it by hourly time frame. this is my query

SELECT COUNT(DISTINCT("tag")) FROM "measurement" GROUP BY time(1h)

I am not getting output after running this total unique tags = 10

Time         tag
1:00:00       1
1:10:10       4
1:30:10       3
1:45:00       1
2:00:00       3
2:50:00       3
3:00:00       10
3:13:00       5

What I am expecting -

Time         tag
1st hour      3  ( because 1,4,3 are 3 unique ids in that hour frame)
2nd hour      1
3rd hour      2
Jan Garaj
  • 25,598
  • 3
  • 38
  • 59

1 Answers1

1

COUNT DISTINCT cannot be used directly on tag (InfluxQL is not a SQL!), therefore a subquery is needed, e.g.:

SELECT COUNT(DISTINCT "tag") as "counttag"
FROM (
    SELECT "time","tag","Value(field)"
    FROM "measurement"
    WHERE $timeFilter
)
GROUP BY time(1h)

Make sure you have used UTC for timestamp in the InfluxDB and keep in mind that Grafana "move" results to your local browser timezone by default.

Jan Garaj
  • 25,598
  • 3
  • 38
  • 59