All Products
Search
Document Center

Quick BI:Custom SQL

Last Updated:Jan 14, 2025

In data analysis, custom SQL enables the creation of data tables with complex logic or models. It also supports parameter passing to accommodate more sophisticated and variable analysis scenarios.

Prerequisites

You have obtained the data. For more information, see Connect to a data source.

Enter SQL code

You can add custom SQL using one of the following methods:

  1. Navigate to the custom SQL page.

    • Entry 1: On the data source page, click Create Dataset By SQL in the upper-right corner. image

    • Entry 2: On the dataset editing page, select the data source in the left panel. If there is no data table on the canvas, click Create The First Table By Using SQL Code on the canvas or Create table by SQL code in the left panel. image

  2. After entering the SQL code, click Run.

    image.png

    Sample SQL statement:

    SELECT  report_date,
            order_level,
            shipping_type,
            area,
            price,
            order_number
    from    company_sales_record
    where   $expr{report_date :report_date}
    and     $expr{order_level :order_level}
    and     $expr{order_number :order_number}

    After a successful run, you can preview the results on the Run Result tab. image.png

  3. Click Confirm Edit.

    Save the dataset you created with the custom SQL statement.

Modify SQL code

To modify the SQL code, you can:

  • Hover the mouse over the table on the canvas and click the Edit Code icon. image.png

  • Click the target table on the canvas. In the right panel, click Edit Code. image.png

Placeholders

Quick BI allows for parameter passing using placeholders. This feature enables flexible data analysis during report viewing and data analysis by passing placeholder values to SQL through a query control. Quick BI supports both value and expression placeholders, which you can configure as needed.

Type

Description

Scenarios

Format

Value placeholder

You can pass a value or a set of values through the query control on the dashboard.

Note

If the placeholder indicates a date value, you must specify the date format to determine the format of the input value.

Applicable to most scenarios where parameters need to be passed.

'$val{placeholder_name}'

Expression placeholder

You can pass a condition through the query control on the dashboard.

When users can freely modify the filter method in the filter conditions on the dashboard, you need to use an expression placeholder to pass the entire filter condition to SQL.

For example, in the following dashboard, users can set profit>50 or profit<50, and you need to pass it to SQL along with the operator. 参数传参

$expr{physical_field_name:placeholder_name}

Configuration method

Compose SQL to meet business requirements and incorporate placeholders.

  1. On the SQL code editing page, click Placeholder Management.

    image.png

  2. In the Placeholder Management panel, configure the placeholders as shown and click OK.

    image.png

    For configuration items, refer to Placeholder Management.

    Note

    If a placeholder is used after the select statement, you must assign a globally effective default value.

    For instance, when creating a dataset, include the placeholder "profit_range" after the select statement as follows:

    SELECT report_date, order_level, shipping_type,price,order_number,area,
     case when profit_amt< ${profit_range} then'Loss' 
     when profit_amt> ${profit_range} then'Profitable'
     else 'Flat'
     end 'order_level'
    from company_sales_record
    where $expr{report_date :report_date}
    and $expr{order_level :order_level}
    and $expr{order_number :order_number}

    Without a default value, the code will not run correctly.

    image.png

    Set the query default value for "profit_range" in Placeholder Management.

    image.png

    After setting the default value, the code will run correctly, as shown:

    image.png

  3. Click Save to store the dataset. image

  4. In the top menu bar, select Start Analysis > Create Dashboard.

  5. On the dashboard editing page, add query conditions and bind placeholders.

    For instance, to set query conditions, use a drop-down single selection to filter area, bind the SQL placeholder, and pass in one value. image.png

    Use text filtering to filter order_number, bind the SQL parameter, and pass in a condition. image.png

  6. When the dashboard is queried, it will transmit the specified content to the value and expression placeholders based on the query conditions.

    For example, the dashboard query conditions are illustrated below. image.png

    The corresponding SQL is as follows:

    SELECT * FROM company_sales_record
    WHERE area = 'North' 
    AND order_number > 0

    An example of using placeholders is as follows:

    SELECT * FROM tablename
    WHERE area in ('$val{area_ph}') -- Text type, multiple selection
    AND name = '$val{name_ph}' -- Text type, single selection
    AND number = $val{number_ph}  -- Numeric type
    AND report_date > '$val{report_date_ph.get(0)}' -- Date type, obtain the start date of the date range control
    AND report_date < '$val{report_date_ph.get(1)}' -- Date type, obtain the end date of the date range control

For scenarios where placeholders are applied, refer to Placeholders.

Note

Compatible with historical writing:

Historical writing of value placeholders (original placeholders): ${placeholder_name}

Historical writing of expression placeholders (original parameters): ${physical_field_name:parameter_name}

What to do next

If you need to analyze fields from different data tables, Quick BI enables you to associate data tables. For more information, see Join a dataset with a table.