如何更改Metabase中日期过滤器的默认值?

发布于 2025-01-27 12:15:55 字数 629 浏览 2 评论 0原文

我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带刺的爱情 2025-02-03 12:15:56

终于找到了解决问题的解决方法。

在UI设置中,根据需要定义date_filter,并分配静态默认值 - 例如01.01.2015如果我的表包含2016年开始的数据。

在SQL Query-我写下:

select * from my_table where date > case when year({{date_filter}}) = 2015 then dateadd(DAY, -1, GETDATE()) else {{date_filter}} end

这意味着:

如果我不选择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:

select * from my_table where date > case when year({{date_filter}}) = 2015 then dateadd(DAY, -1, GETDATE()) else {{date_filter}} end

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文