Custom queries
A custom query is a way to select data from an external data source in a data connection, with the use of your own custom database query. It is an alternative to selecting tables and columns in the point-and-click Select data or Views in connection dialogs. With a custom query, you can put your SQL skills to use and make a very specific data selection. The result of a custom query is a database table that you can access like a data table in Spotfire
On this page
- Parameters in custom queries
In a custom query in a data connection, if you want the query to retrieve different results depending on the value of one or more variables, you can set up the query to use parameters. You can map a parameter to, for example, a property control in a text area.
Adding a custom query in a data connection
In an installed Spotfire client, you can add a custom query to your data connection when you select data in the Views in connection dialog.
Before you begin
- To add and edit custom queries, you must use an installed Spotfire client.
- Not all data connectors support using custom queries.
- The Custom query in connections license is required to add and edit custom queries. To be able to trust custom queries, so that other users can open them, you must also be a member of the user group Custom Query Author.
- You write custom queries in the query language of the external database. To learn more, see Writing custom queries.
Procedure
Results
Writing custom queries
When you write a custom query, there are requirements, limitations, and general guidelines to keep in mind.
When you write a custom query, you write the query in the language of the external data source. This means that the way you write the query is dependent on the type of data source you are connecting to. For example, for a Microsoft SQL Server database, you would write your custom query in the Microsoft SQL Server dialect of SQL.
FROM clause of the final query that Spotfire sends
to the external data source. For example, below is a custom query for a
Microsoft SQL Server connection:
SELECT Country,AVG(Sales) AS AvgCountrySales FROM "World Sales" GROUP BY Country
FROM clause of the
SELECT statement:
SELECT [SpotfireCustomQuery1].[Country] AS [Country], [SpotfireCustomQuery1].[AvgCountrySales] AS [AvgCountrySales] FROM (SELECT Country,AVG(Sales) AS AvgCountrySales FROM "World Sales" GROUP BY Country) AS [SpotfireCustomQuery1]
The custom query is evaluated each time a regular query is performed from Spotfire. If you use the custom query for an in-database (external) data table, the custom query is run each time a visualization or filter is updated. For in-memory (imported) data tables, that is, when data is loaded into the Spotfire data engine, the query will only run when data is imported or explicitly refreshed.
Query requirements and limitations
- Queries are written in the language of the external database. For example, for a Microsoft SQL Server database, you would write your custom query in the Microsoft SQL Server dialect of SQL. For specific information on how to write queries for a certain data source type, always refer to the official documentation for that external data source. Capabilities, limitations and syntax rules is dependent on the type of data source you are connecting to.
- It must be possible
to run the custom query as a subquery in a
FROMclause. See the official documentation for the external data source for more information.Note: Subqueries inFROMclauses are sometimes referred to as derived tables. - The custom query must consist of a single statement. You cannot chain multiple statements together separated by a semicolon character ";".
- Stored procedures are not supported in custom queries.
- Common table
expressions (CTEs) are not supported, because they require the
WITHkeyword.
Data types
When you create a custom query, only include columns with data types that are actually supported by Spotfire. See the documentation for the connector you are currently using for a list of supported data types.
If an unsupported column is included in the query, it is marked as invalid in the verification step, and you cannot use the resulting table. To resolve such issues, either rewrite the query to exclude the unsupported columns, or remove the columns from the resulting table. You can always assign an arbitrary data type to a custom query column. However, there is no guarantee that the resulting Spotfire table for such a custom query will work.
Security — Trusting custom queries
No other user will be able to run a custom query that you create, unless two conditions are fulfilled:
- You must save the custom query to the library as part of an analysis, or as part of a data connection.
- You must be a member of the Custom Query Author group, which means that you are authorized to create and trust custom queries on behalf of other users.
It is still possible to share a custom query you have created with another user, but that user must review and trust the custom query. A user can trust a custom query by opening the Custom Query dialog, examining the custom query text, and then clicking OK.
>