Sugar BI: How to customize the SQL view in the visualization page?

If you have complex data sources and data model requirements in data visualization work, for example, you need to use subqueries, unions, complex data filtering, etc., you can create custom SQL views through SQL statements.

This article takes Sugar BI as an example

Users can write SQL statements to create a custom SQL view. After the creation is successful, it will appear in the custom SQL view on the lower left side of the page. After that, it can be dragged to the center of the page to interact with the Data tables in the data source or other custom SQL views for association analysis.

You can preview the corresponding data structure by moving the mouse into the custom SQL view or clicking the view button on the data table.

Embed user mailboxes in custom SQL views

Sugar BI supports embedding the mailbox of the currently logged-in user in the custom SQL statement (in this way, you can allow different users to see different data), you can use the following syntax:

Note : If you share the big screen or report page publicly , and the custom SQL of a chart on the page uses the above embedded login user's mailbox, there will be problems at this time, because the publicly shared page is It can be browsed directly without the user logging in to the Sugar BI account, so the custom SQL cannot get who the current browsing user is, so an error will occur. In short, if your visualization page needs to be shared publicly, then do not embed the currently logged-in user mailbox in the custom SQL.

Embed macro definition variables in custom SQL views

Embed data macro definitions in custom SQL views

Sugar BI supports embedding data macro definition variables in custom SQL view statements (in this way, you can embed dynamic parameters in the where clause), and you can use the following syntax:

Create a custom SQL view in the data model, the syntax of the associated macro definition variable in SQL is detailed in the macro definition variable

注意:这里必须使用宏定义变量的默认值语法${_MACRO || default_value}为宏定义变量赋上默认值,防止宏定义变量不存在时引起sql语句报错

在图表中绑定该数据模型,并定义宏定义变量 _MACRO的新值为'东北':

点击图表「调试」,可以看到:sql 中的宏定义变量${_MACRO || '华北'}被替换为'东北', 如果未配置宏定义变量,宏定义变量${_MACRO || '华北'}将被替换为默认值'华北'。

Guess you like

Origin blog.csdn.net/Foolforuuu/article/details/129244425