How to handle template variable when the result is empty

i am using postgresql and elasticsearch. In one variable i am getting a field with query. and need to use the result in another query ( postgresql ) to filter out values.
this works fine unless there is atleast one value from lucene query.
but fails when query results empty data.

How to handle this.

Can lucene query add default value when resultant data is empty.

I am facing same issue with Mysql too. @gokuls Did you got a solution for it?