Combine two or more select statements

Say I have time series data of the form
timestamp, <tag1, tag2>, value

I can do individual queries of the form

select mean(value) from db where tag1 = 'mytag' group by "tag2", time(5s)

This gives me avg of individual tag2 entries matching tag1 = mytag

Now I was to calculate the percentage distribution of above.

We can calculate the total (by removing grouping)

select mean(value) from db where tag1 = 'mytag' group by time(5s)

So theoretically I can calculate percentage by dividing query1 results by this query2 result.

How to combine these two queries in a single query statement?

Hello @maloy-ghosh,
Welcome!
May I ask why are you using InfluxDB 1.x? I’d suggest using 2.x and Flux.

You’d have to use subqueries:

Select query1/query2 from (Select mean(value) AS query2 from db where tag1 = 'mytag'), (select mean(value) AS query1 from db where tag1 = 'mytag' group by "tag2") group by time(5s)

I think thats right, but honestly I’m much more comfortable with Flux. I’d highly suggest upgrading to recieve better support and expand your capabilities.

1 Like