欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

超级集 SQL 模板的使用

最编程 2024-10-16 22:19:38
...

使用背景

有时想让表的时间索引生效,而不是在最外层配置报表时,再套多一层时间范围。这时可以使用SQL模板

参考官方文档

https://superset.apache.org/docs/configuration/sql-templating/#:~:text=SQL%20Lab%20and%20Explore%20supports%20Jinja

我的实践

1、定义一个dataset,其中我的dt是由外围dashboard的筛选项传进来的

select dt, team, sum(xx) xx
from t_test
where dt in ({{ "'" + "','".join(filter_values('dt')) + "'" }})
group by 1,2

为了防止保存dataset报错,你的dataset dt字段可以设置一个无效filter。比如我设置了no filter
在这里插入图片描述

2、dashboard定义filter。
在这里插入图片描述

保存后,superset会自动将用户选择的dt变量带进sql。生成的sql将是:

select dt, team, sum(xx) xx
from t_test
where dt in where dt in ('2024-10-07', 'No filter')
group by 1,2

这样便可以实现在内层让数据源使用索引