Filter query A using query B result

Hi, I’m new to Grafana but having looked around quite a bit, I haven’t found an answer to this yet. I do believe it is a pretty common requirement and having seem some other questions that might have been similar, I suspect this might not entirely be possible.

  • What Grafana version and what operating system are you using?
    Grafana v10.3.3 (252761264e)

  • What are you trying to achieve?
    I have a table on one PostgreSQL database that stores (among other things) a start and end time for a specific occurrence.
    In a different database altogether, I have a table that captures some metrics every few minutes depending on traffic/load/whatever else.
    I want to query the intervals from the first database and use it to exclude metrics found from the second database therefore gauging performance of a service outside of these occurrences.

  • How are you trying to achieve it?
    I have tried several suggestions I have found elsewhere in the forum, most of which suggested using some form of the variables. I set up a query variable that concats the start and end times as follows:

  (extract (epoch from starttime))::int8 || ';' || (extract (epoch from endtime))::int8
FROM interval_table;

I then attempted to use this variable referenced in my metrics query, splitting it up again and joining on the split variables.

  • What happened?
    Selecting the variable actually creates different columns instead of different rows, which causes a duplicate column error in Grafana and also doesn’t allow me to join on these rows.

    select $interval_variable

  • What did you expect to happen?
    I would assume selecting the variable would give me several rows, each with the concatted start and end times. I could then extract them in a CTE and join my metrics table outside of these intervals.

This is the route that I have taken thus far, but as I mentioned above, I’m not sure if this is actually the way to solve the actual issue I’m looking at.

Thanks for your time and effort.