3

I've created a Grafana dashboard variable (multi-select enabled) based on a MySQL query:

SELECT DISTINCT(my_field) from my_table

This field has about 12 options, one of them being a NULL value. When editing my dashboard widgets, I include a SQL constraint similar to:

... WHERE my_field IN ($my_variable)

This almost works well, except for the NULLs. Grafana is translating the NULL to '', and NULL isn't supported in an IN (...) statement anyway.

Any recommendations on how to handle this to work with NULLs?

radicand
  • 6,068
  • 3
  • 27
  • 22

1 Answers1

3

You could extend condition:

WHERE my_field IN ($my_variable) OR my_field IS NULL;

Grafana is translating the NULL to ''

WHERE COALESCE(my_field, '') IN ($my_variable)
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
  • Thanks, the trouble with this approach is that NULL isn't always selected in the dashboard as an option for the variable. I essentially need the IS NULL clause to be conditional if a null value is selected in the list of options in the dashboard drop down. – radicand Aug 02 '19 at 20:34
  • @radicand There are other options – Lukasz Szozda Aug 02 '19 at 20:40