如何更改Metabase中日期过滤器的默认值?
我正在尝试在metabase-中写动态SQL查询 - 我添加了这样的日期过滤器(使用Snowflake进行查询):
select * from my_table where date > {{date_filter}}
之后,我需要设置昨天至date_filter的默认值,所以我写了这篇文章:
select * from my_table where date > [[{{date_filter}} #]]dateadd(DAY, -1, GETDATE())
它奏效了,当我运行此查询时,我得到了正确的输出。
但是 - 当我更改Metabase为日期类型提供的日历中的日期时 - 我遇到了此错误:
SQL compilation error: syntax error line 1 at position 142 unexpected '#D'. syntax error line 1 at position 156 unexpected '-'. syntax error line 1 at position 172 unexpected '('.
如何在不遇到此错误的情况下更改date_filter默认值?
I am trying to write dynamic sql query in metabase-
I added date filter like that (using snowflake for query):
select * from my_table where date > {{date_filter}}
after that I needed to set default value of yesterday to date_filter, so I write this:
select * from my_table where date > [[{{date_filter}} #]]dateadd(DAY, -1, GETDATE())
and it worked, when I ran this query- I got the right output.
BUT- when I changed the date in the calendar that metabase provided for date types- I got this error:
SQL compilation error: syntax error line 1 at position 142 unexpected '#D'. syntax error line 1 at position 156 unexpected '-'. syntax error line 1 at position 172 unexpected '('.
How can I change date_filter default value without getting this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
终于找到了解决问题的解决方法。
在UI设置中,根据需要定义date_filter,并分配静态默认值 - 例如01.01.2015如果我的表包含2016年开始的数据。
在SQL Query-我写下:
这意味着:
如果我不选择date_filter calender中的任何日期-date_filter年度值将为2015年(我定义了date_filter = 01.01.2015),因此我将将日期与dateadd(day,-1,getDate())进行比较。
但是,如果我要在date_filter calender中选择估值,那么Year(Date_filter)将与2015年不同(正如我所说,我不会在2016年之前选择日期,因为我在表格中没有数据) {{date_filter}}我刚选择的值。
finally found a workaround that solve the problem.
in UI settings, define date_filter as REQUIRED and assign static default value- for example 01.01.2015 if my table contains data starting of 2016.
in sql query- I wrote that:
which means:
if I won't select any date in date_filter calender- date_filter year value will be 2015 (as I defined date_filter = 01.01.2015) so I will compare DATE against dateadd(DAY, -1, GETDATE()).
but if I WILL select value in date_filter calender, then year(date_filter) will be different than 2015 (as I said, I won't choose date before 2016 because I have no data before 2016 in my table) so I will compare DATE against {{date_filter}} value which I just selected.