The SQL Query Editor Tab

In the SQL Query Editor tab, you can transform and aggregate the datasets in a project with SQL and save the result as a new dataset. The saved dataset is a Derived Dataset defined by the query.1

Running and Saving a Query

In the SQL Query Editor tab, running a query and saving its result as a dataset are separate operations. Open the tab with SQL Query Editor in the Data menu and write a query in the SQL Query editor. When you press Execute Query, the tab runs the query and shows the result. At this point, no dataset has been created yet. When you enter a dataset name in Output Name and press Save as Dataset, MIDAS creates a Derived Dataset.

The query result and the save controls

When you save with the same name as an existing Derived Dataset created in the SQL Query Editor, you can choose to replace the query of the existing dataset with the new query instead of creating a new dataset. MIDAS keeps treating it as the same dataset after the replacement, so the models and reports that use the dataset use the result of the new query.

Table and Column Names

MIDAS matches the table names in the FROM and JOIN clauses against dataset names without distinguishing uppercase and lowercase ASCII letters. For example, FROM iris refers to the dataset named Iris.

Enclose dataset names and column names that contain spaces or symbols in double quotes. For example, write FROM "Palmer Penguins" for the dataset named Palmer Penguins.

When you rename a dataset, MIDAS rewrites the table names in the FROM and JOIN clauses of the saved queries that refer to the dataset to the new name. Whether the old name is enclosed in double quotes does not affect the rewrite. MIDAS gives a table name without an alias the old name as its alias, so a column reference written with the old name, such as sales.x, still points to the same column without being rewritten. The column name of an expression without an alias, such as sales.x + 1, does not change either. For example, renaming sales to sales2024 turns SELECT sales.x FROM sales into SELECT sales.x FROM "sales2024" AS sales. A column reference qualified with a schema or catalog name, such as main.sales.x, becomes sales.x, because an alias cannot be qualified. The column name of an expression without an alias that contains such a reference changes. MIDAS does not rewrite a query that you are still writing in the editor of an SQL Query Editor tab. The Derived Datasets whose queries are rewritten, and the Derived Datasets and models that depend on them, are recomputed.

Only Primary Datasets and Derived Datasets can be referred to by table name. The dataset that a Filtered Data tab shows is an Ephemeral Dataset, so it cannot be referred to by table name. To use the rows of a Filtered Data tab in a query, save the rows as a Derived Dataset and refer to the saved dataset by its name.

Supported SQL

The SQL Query Editor tab runs only a single SELECT statement.

Functions and syntax follow DuckDB's SQL. The list of available functions and syntax is in the DuckDB documentation. MIDAS loads only three DuckDB extensions, ICU, JSON, and Parquet, so functions from other extensions are not available.

Date and time functions compute values in UTC. For example, hour() returns the hour in UTC as an integer from 0 to 23, regardless of the browser's time zone. Data Table shows datetime values in the browser's time zone. Therefore, in an environment where the browser's time zone differs from UTC, hour() returns an integer other than 9 for a value that Data Table shows as 9:30.

MIDAS relies on a query always returning the same result. If you create a Derived Dataset with a query whose result changes on every run, such as one using random() or USING SAMPLE, the result changes every time MIDAS evaluates the dataset, and there is no way to fix the result. Queries whose results are not guaranteed to be the same on every run are not recommended.

Data Types of Result Columns

When creating a Derived Dataset, MIDAS determines the data type of each result column from the type of the values DuckDB returns, as shown in the following table. Columns with the same name as an enum column of an input dataset follow the rules in the paragraphs after the table.

DuckDB typeMIDAS data type
Integer types such as INTEGER, BIGINT, and HUGEINT, except UHUGEINT, and DECIMAL with zero digits after the decimal pointint64
Floating-point types DOUBLE and FLOAT, and DECIMAL with one or more digits after the decimal pointfloat64
BOOLEANboolean
DATEdate
TIMESTAMP, TIMESTAMPTZ, TIMESTAMP_S, TIMESTAMP_MS, TIMESTAMP_NSdatetime
Types not in the rows above, such as VARCHAR, TIME, INTERVAL, LIST, STRUCT, and UHUGEINTstring

When creating a Derived Dataset, if an input dataset has a column with the same name as a result column, MIDAS carries over that column's Enum and measurement scale to the result column. What is carried over depends on whether the input dataset's column is an enum column. When two or more input datasets have a column with the same name, MIDAS carries over from the column of the dataset referred to first in the FROM and JOIN clauses.

When the input dataset's column is an enum column, the result column also becomes an enum column with the same Enum and carries over the measurement scale. However, when the result column contains values that are not in the Enum definition, MIDAS makes the result column a string column.

When the input dataset's column is not an enum column, MIDAS carries over the measurement scale only if the result column has the same data type as that column. When the data types differ, MIDAS determines the measurement scale from the data type of the result column.

Row Numbers

The row number (Row #) is a number that identifies a row of a dataset. If the query result has exactly one column named "Row #", MIDAS uses that column as the row numbers. A column created in the query, such as row_number() OVER (ORDER BY ...) - 1 AS "Row #", also becomes the row numbers. Otherwise, MIDAS assigns row numbers automatically.

The row numbers of an input dataset can be read in a query as a column named "Row #", so each row in the result of SELECT * FROM t WHERE ... has the same row number as in the input dataset.

Editing Derived Datasets

Besides saving again with the same name, you can also rewrite the query of a Derived Dataset created in the SQL Query Editor in an SQL Query Editor tab in edit mode. To open the edit-mode tab, press Edit Query in the tab that View SQL Query in the Data Table table menu opens. The Project Overview tab and the Project Lineage tab also have items that open the edit-mode tab.

The SQL Query Editor tab in edit mode

In edit mode, when you check the result with Execute Query and then press Update Query, MIDAS replaces the dataset's query with the new query.

When you press Update Query, MIDAS discards the row data of the Derived Datasets that depend on the updated dataset and re-estimates the models that depend on it. The extent of the recomputation is explained in Recomputation When a Dataset Changes.

When you press Update Query, MIDAS reapplies the measurement scales and display formats that the user set before the update to the columns with the same names after the update. However, when a column's data type changes, MIDAS does not reapply the measurement scales and display formats that cannot be selected for the new type until the data type changes back.

To create a separate dataset with a modified query while keeping the dataset whose query View SQL Query shows, use Copy to New Editor in the tab that View SQL Query opens.

Footnotes

  1. The Agent API's datasets.derive() also runs queries under the rules described on this page and saves the result as a Derived Dataset. ↩