Optional variable in query

Hey guys,

I am having trouble using brackets for optional variable. Currently, I have a piece of code that uses “count of transaction” and before the end of the query I use having “count of transaction” > {{variable}}. Here is a snippet of the code without revealing sensitive information. Shown below:

Problem is, I got data without setting any filter but I got no data if I use that filter. How do I fix it?

Hi @justinOon
Please post “Diagnostic Info” from Admin > Troubleshooting. And which database you’re querying.
What is the Filter Type of {{min_value}} ?

Hi @flamber

sorry to say but I’m not an admin. Is there any other way I can help you get the information you need?

The db I’m querying from is Redshift and the filter type for min_value is number
04%20PM

@flamber
This is my select statement if it helps in any way
14%20PM

@justinOon I guess there’s something else in your query causing this. Difficult to tell, since you don’t show the entire query and I don’t know version info.
Try not using a variable and just input static query and see what you get.

@flamber using static query works so I guess the problem is in the structure of my query. I will share the screenshot of my entire query here.

@justinOon
I guess you also set {{country}}, when setting the min_value - that doesn’t look correct.
It should be like this if it is a normal Text filter:

WHERE 1=1
[[AND "country" = {{country}}]]
AND not (lower...
...

I would highly recommend reading these:
https://www.metabase.com/blog/sql-template-variables/index.html
https://www.metabase.com/blog/field-filters/index.html

@flamber it finally worked. I changed the order according to your recommendation.

Thanks for the help!