Conditional in Postgres Data Source WHERE Query

Hi All!

I am new to using Grafana and I am on the Grafana Cloud platform using the most recent version as I just signed up a couple days ago.

I am having an issue with a query in my Postgres Data Source. The gist of it is I have a table I am building from my database and I have a template variable that I want to use in my query, but conditionally. So basically I am trying to do this:

SELECT 
  table.first_name AS 'Name'
FROM table
WHERE
  table.id = $variable OR $__timeFilter(table.created_at))
ORDER BY 1

So basically, if an ID has been selected for the $variable, use that to filter, and if not, allow it to be filtered by the date range.

The problem is that the “default value” of my template variable is currently “Staging”, and I cannot find a way to change it, so I keep getting an error of
db query error: pq: column "staging" does not exist
I have been trying to find a solution for a while and I am at a complete loss at the moment so I was hoping this community could help. Thanks!

1 Like