仅当变量在元数据库中给出值时才添加过滤器

Add filter only if the variable is given with values in Metabase

在Metabase中,可以在查询中添加带有{{variable}}的变量,例如:

select *
from dashboard_demographic_view
where clinic_id = {{clinic}}

,其中dashboard_demographic_view是预先创建的视图。在上面的查询中,clinic_id{{clinic}} 中给出的内容匹配的行将被 returned.

不过,我很好奇如果我只在给 {{clinic}} 一个值时才需要那个过滤器怎么办,否则查询应该 return 所有行。我提到了 SQL add filter only if a variable is not null 但它对我的情况没有帮助。

select *
from dashboard_demographic_view
where ({{clinic}} is null or clinic_id = {{clinic}})

上面的查询给出 无法替换 'clinic':未指定参数。找到:("clinic") 当我没有指定 clinic.

理论上,这可以通过将变量 {{clinic}} 设置为字段过滤器来解决。但是,由于某些原因无法对视图进行操作(#8800 in metabase)。

除了制作真正的 table 来实现我的目的之外,我正在寻找解决方法。任何帮助将不胜感激。

可选条款对您有帮助吗?请参阅 Metabase Documentation: Sql Parameters 的结尾。

您的代码基本上会变成类似

select *
from dashboard_demographic_view
[[where clinic_id = {{clinic}})]]

(这个问题之前可能也有人问过——也许值得检查 Stack Overflow 中是否有重复项。)