Run a SQL analysis with parameters

Write a read-only SQL query when you want to control the calculation or let a reader choose a value at run time. This example starts with a fixed channel report and adds an optional channel selector to the saved analysis.

Run the query in the editor

  1. Open Source data → Sales orders → orders using the SQLite connection from Getting started. Choose SQL mode above the query steps. The current connection determines which tables and SQL dialect are available.
  2. Paste the query below. You can also run a visual query first, switch modes and click Use SQL from the visual query to start with the statement Topbase generated.
  3. Click Run SQL. Check that the two columns are called channel and revenue. The result should show Web 300, Partner 200 and Store 180, in that order.
SELECT channel, SUM(amount) AS revenue
FROM orders
WHERE status = 'paid'
GROUP BY channel
ORDER BY revenue DESC

To investigate one order, replace the query temporarily with SELECT * FROM orders WHERE order_id = 1003. Its status is refunded, so its amount of 50 is excluded from the paid total. Restore the grouped query before continuing.

Give the reader an optional channel filter

  1. In the SQL editor, insert the following line between the existing WHERE and GROUP BY lines. Keep the AND inside the square brackets so the statement is valid when the whole condition is skipped.

    [[AND channel = {{channel}}]]
    
  2. Click Save analysis, enter Paid sales by channel (SQL), choose a writable group and confirm. Use the saved analysis to supply parameters. The table editor's Run SQL executes SQL without a parameter form.

  3. In the saved analysis, choose Query parameters, enter Web without quotes and run. You should see one row with 300. Reopen Query parameters, clear the value and run again. All three channels should reappear.

  4. To require a channel on every run, use AND channel = {{channel}} without the optional brackets. A newly opened analysis with this required parameter waits for input. Use names such as channel or start_date. Leave the placeholder itself outside SQL string quotes so Topbase can bind the value.

In the saved SQL analysis, the channel parameter takes Web as its value.
In the saved SQL analysis, the channel parameter takes Web as its value.
Running the saved analysis with Web returns just that channel and revenue of 300.
Running the saved analysis with Web returns just that channel and revenue of 300.

Add a daily trend to the dashboard

To make the second chart in the dashboard guide, start a new SQL analysis of the same orders and run this query. The date is the grouping field. Sorting it in ascending order puts the points in chronological order.

SELECT order_date, SUM(amount) AS revenue
FROM orders
WHERE status = 'paid'
GROUP BY order_date
ORDER BY order_date

The result has three dates in October 2026. October 1 totals 200, October 3 totals 300 and October 4 totals 180. October 2 had only a refunded order, so there is no row for that day. Save as Paid sales by day. Set the chart to Line chart, with order_date on X and revenue on Y.

The daily query returns three dates. Compare revenue in the preview before choosing a line chart.
The daily query returns three dates. Compare revenue in the preview before choosing a line chart.

Reuse and troubleshoot a saved query

On this page

Build your first dashboard.

Follow six sample orders from a database to a sales dashboard on your own server.