How to use date filter in sql query

Start simple - remove that batch conditions and when you have working examole then add more complexity, e. g. batch conditions. You will see which step generates an error, so you will know what wrong.

Also use query inspector to check generated SQL syntax How To Use Grafana's Query Inspector to troubleshoot issues