cozzymotto
cozzymotto

Reputation: 103

Grafana create query variable from table

So I am new to Grafana, I have a table and a bar chart:

enter image description here

The bar chart query is as follows:

select coalesce(group_name) as group_name,
  sum(sales) filter (where device_type = 'HEAD_PHONES') as HEAD_PHONES,
  sum(sales) filter (where device_type = 'GUITAR') as guitar,
  sum(sales) filter (where device_type = 'XBOX') as xbox,
  sum(sales) filter (where device_type is null ) as other
from test
where area = 'AREA1' <--make this grafana variable
group by coalesce(group_name);

I would like if every time the user clicks a row within the table, the bar chart is updated using the same query but the area changed depending on the row clicked. How do I make it a Grafana variable supposing there are more than a 100 areas?

Here is db:

enter image description here

Upvotes: 0

Views: 19818

Answers (1)

Jonibek
Jonibek

Reputation: 98

In grafana there is "variables", go to "dashboard settings" --> variables, from there create new variable with type "query", name it like "area" and put sql query(select area from test), save it, and then in your select query: select ... where area in ($area).

reference https://docs.timescale.com/timescaledb/latest/tutorials/grafana/grafana-variables/#using-grafana-variables

Upvotes: 2

Related Questions