Fixes the bug in the sql for case when tag is selected