All Products
Search
Document Center

Quick BI:Custom SQL

Last Updated:Mar 31, 2026

In data analysis, you can use an ad hoc query to create a dataset for complex logic or models. Ad hoc queries also support parameter passing, which enables more complex and dynamic analysis scenarios.

Prerequisites

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

Limitations

API data sources do not support ad hoc queries.

Enter SQL code

You can create an ad hoc query in the following ways.

  1. Go to the ad hoc query page.

    • Entry 1: On the Data Sources page, click Ad Hoc Query in the upper-right corner.image

    • Entry 2: On the relational model canvas of the dataset editor, select a data source. If no data tables are on the canvas, click Click to use SQL code to create a dataset on the canvas or Create table with SQL code in the left-side panel.image

    • Entry 3: On the physical modeling canvas of the dataset editor, click Create table with SQL code in the left-side panel.

      image

  2. After you enter the SQL code, click Run.

    image

    The following code provides a sample SQL statement:

    SELECT  report_date,
            or*******el,
            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 :or********er}

    After the query runs successfully, you can preview the results on the Result tab.image

  3. Click Create Dataset to save the dataset created from the ad hoc query.

Modify SQL code

You can modify SQL code in three ways:

  • Click the image icon to the right of the target table.image

  • Click the target table on the canvas. In the Table Details section at the bottom, click Edit SQL.image

  • Click the image icon to the right of the target table and select Edit SQL.

    image

SQL syntax

  • Ad hoc queries mainly support SELECT statements. INSERT, UPDATE, DELETE, and stored procedures are not supported.

  • The query syntax must conform to the database syntax of the data source you are using. For example, if you use a MySQL data source, the ad hoc query must use MySQL query syntax. Some specific database syntaxes, such as find() and findone() in MongoDB, are not supported in Quick BI. You must use SELECT statements to query data in ad hoc queries.

  • To add comments to an ad hoc query, use the -- Comment format. A space is required after the two hyphens (--).

    image

    The following code provides a sample SQL statement:

    SELECT  area,  -- Area
            price  -- Price  
    from    company_sales_record
  • If you join multiple tables in an ad hoc query, make sure that you use table aliases and column aliases correctly. Alias syntax can vary across databases.

  • An ad hoc query has no row limit, but the query itself cannot exceed 65,536 characters. If necessary, you can split or optimize the query, or create multiple datasets and then join them.

Note
  • Quick BI sends the dataset SQL to the database for execution and retrieves the results. The query timeout period is 5 minutes. If no data is returned within 5 minutes, a timeout error occurs.

  • If a query times out in less than 5 minutes, the cause might be a timeout setting on your network devices, such as an SLB. Another possible cause is a network interception policy on a firewall that blocks the query request.

Placeholders

Quick BI uses placeholders to pass parameters to your SQL. This enables flexible data analysis. You can use two types of placeholders: value placeholders and expression placeholders.

Type

Description

Use cases

Format

Value placeholder

Passes a single value or a set of values from a query control on a dashboard.

Note

When the placeholder is for a date, you must select a specific date format to control the format of the passed value.

Suitable for most scenarios that require parameter passing.

'$val{placeholder_name}'

Expression placeholder

Passes a condition from a query control on a dashboard.

Use an expression placeholder to let users change the filter method on a dashboard. This passes the entire filter condition to the SQL statement.

For example, in the dashboard shown in the following figure, a user can set the condition to Profit > 50 or Profit < 50. The entire condition, including the operator, needs to be passed to the SQL statement.参数传参

$expr{physical_field_name:placeholder_name}

Configuration

Write your SQL statement and add placeholders based on your business requirements.

  1. On the SQL editor page, click Parameter Settings.

    image

  2. In the Parameter Settings panel, configure the placeholders and click OK.

    image

    For more information about the configuration items, see Create a dataset.

    Note

    If a placeholder is used in a SELECT clause, you must set a global default value for it.

    For example, you add a placeholder named profit_range in the SELECT clause when creating a dataset, as shown in the following code:

    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 'Profit'
     else 'Break-even'
     end as 'Or*er Level'
    from company_sales_record
    where $expr{report_date :report_date}
    and $expr{order_level :order_level}
    and $expr{or**********************er}

    If you do not set a default value, the query fails to run.

    image

    You must set a default query value for profit_range in the parameter settings.

    image

    After you set the default value, the query can run successfully. The following figure shows the result:

    image

  3. Click Save to save the dataset.image

  4. In the top menu bar, click the image icon or click the image icon and select Create Dashboard.

    image

  5. On the dashboard editor page, add a query condition and bind it to a placeholder.

    For example, when setting a query condition, you can use a single-select drop-down list to filter area and bind it to a value placeholder to pass a single value.image.png

    Use a text filter on or********er and bind it to an expression placeholder to pass a condition.image.png

  6. When you query data on the dashboard, the values from the query controls are passed to the corresponding placeholders in your SQL.

    For example, the following figure shows the query conditions on a dashboard.image.png

    The corresponding SQL statement is as follows:

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

    Placeholder usage example:

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

For more information about placeholder use cases, see Placeholders.

Note

Backward compatibility:

Legacy format for value placeholders (formerly placeholders): ${placeholder_name}

Legacy format for expression placeholders (formerly parameters): ${physical_field_name:parameter_name}