How can I group many values for a variable in grafana?

1.5k Views Asked by At

I have an SQL table with columns: name and user type.


Name type
John student
Tom teacher
Peter teacher
Steve student

I want to have a grafana variable where I can select a user "type" and the variable passes the value of all the "names" corresponding to that "type" to the dashboard.

I found this example - https://grafana.com/blog/2019/07/17/ask-us-anything-how-to-alias-dashboard-variables-in-grafana-in-sql/ which lets me alias a one to one mapping, but I am looking for a one to many mapping.

2

There are 2 best solutions below

0
On

For this you need to create 2 variables and link them in next order:

  1. Create variable "type", "select distinct type from my_table"
  2. Create variable "Name", "select distinct name from my_table where type = $type", where $type is already your variable from grafana.
0
On

You should do it in two steps:

First, per your example create variable, that will contain types.

Than, in the same way create variable, that will contain names, and will use firs variable as parameter for select.