Variable query options: text + value in SQL

  • What Grafana version and what operating system are you using?

  • What are you trying to achieve?

I want to have nice-looking variables - with country names in this example.
In the SQL query, I’d like to use the respective country_id to optimize the SQL query.

  • How are you trying to achieve it?
    I have created an SQL query returning one column (<country_id>-<name>) and used the Regex option to split it into two pars: /(?<value>\d+)-(?<text>.*)/:
SELECT country_id || '-' || name FROM country

  • What happened?

It was a little complex to build the regex, but it worked.

  • What did you expect to happen?

I hoped that I could have something like the following, which is NOT working (it returns values from both columns, such as 1, 2, Germany, …):

SELECT country_id AS value, name AS text FROM country

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.

no errors (when SQL was correct ;-))

  • Did you follow any online instructions? If so, what is the URL?

I’m wondering, if there is a way around using the regex, if I provide the correct column names.

Thanks a lot!

Doc is your good friend: PostgreSQL | Grafana documentation


SELECT hostname AS __text, id AS __value FROM host

1 Like

Oops… that was a very clear RTFM case :upside_down_face: I skipped over that part somehow.

Sorry, I’m wondering how to mark the topic as solved :frowning: