skip to Main Content

I am trying to get some graph about different entries in a column in Grafana, but Grafana tries to recognize the antry as a column instead of a column value.

I have the following setup:

A PostgreSQL Database (v11).

A table called People.
This table contains 3 columns: id, name, age.

I want to get the average age for each name contained within the name column.

I have defined a query variable called firstname in Grafana, which is returned by the query:

SELECT DISTINCT(name) from People;

It shows then a dropdown list containing all the unique names.

To get the average age for each name, i write the following query in Grafana:

SELECT AVG(age), pit_date FROM People WHERE name = $firstname GROUP BY pit_date LIMIT 15;

But i get the error:
db query error: pq: column "selena" does not exist

My understanding is that Grafana is trying to locate teh column "selena" instead of "selena" within column "name".

Do you have any idea where i am wrong and how to solve this problem? I would appreciate it very much.

2

Answers


  1. I think that in Grafana, you need to use curly braces {} to indicate template variables within your query. Try this sintax

    SELECT AVG(age), pit_date FROM People WHERE name = ‘${firstname}’ GROUP BY pit_date LIMIT 15

    I hope it helps bro

    Login or Signup to reply.
  2. Problem is caused by lack of quotes around you variable substitution.

    After Grafana substituted firstname variable where clause of you query looks like this: name = selena. From SQL perspective selena is expected to be column name. To make it a string you need to wrap substitution with single quotes:

    SELECT AVG(age), pit_date 
    FROM People 
    WHERE name = '$firstname' 
    GROUP BY pit_date 
    LIMIT 15
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search