Query Creation
About Creating Queries
When you initiate the query creation process, you can choose to select the query sources (entity and relationship families) and query columns (family fields) using either of the following options:
- Design workspace: Provides a visual representation of a query, and lets you manually add sources, criteria, and links.
- SQL workspace: Provides a workspace into which you can directly enter SQL code to build a query. The SQL View is intended for more advanced Query users.
About Running Queries
Queries are available in many places throughout APM. In some cases, you might create a query manually and save it for future use. In other cases, you might run a query that another user constructed or that is delivered with the baseline content.
When you run a query, you will see the results in the Results workspace, which will make it obvious that you are looking at query results. For example, if you open a query from the Catalog or select a dashboard hyperlink that references a stored query, the results will be displayed on the Results workspace.
In other cases, however, you might run a query and see the results in a different format on a different screen. In these cases, you might not realize that you are looking at query results. Inspection Management, for example, provides a customized workflow that allows you to execute queries that return specific information, such as equipment that can have bundle inspections. In addition, queries provide a customized form where you can query the database for records in families that meet specific criteria.
When a query is created, it can be configured to return raw data stored in the database or reformatted data. This means that you might run different queries that return the same data in different formats. In addition, queries can be configured to prompt you for information before returning any results. It is a good practice to use parameters instead of literal values. This helps in both performance and reuse.
About Saving Queries
After you create a query, you can save it so that you or other users can access it later. Queries are saved as Catalog items to the APM Catalog.
Details
After a query has been saved to the Catalog, whenever you make changes to it, you will need to re-save the query to retain your changes.
Saving a query is similar to saving any other item to the Catalog, but consider the following query-specific considerations:
- If you try to save a query with invalid SQL syntax, an error occurs.
- Query names must not contain / or \.
About SQL Code
If you are familiar with SQL syntax, instead of designing the query, you can enter SQL code directly and run the query to view the results.
SQL Details
APM supports the use of Oracle and SQL server databases, all of which can be queried using SQL statements. While the same basic SQL code can be used to query any type of database, there are some differences in the syntax that is supported by each database server. Therefore, APM uses a proprietary version of SQL that is constructed automatically and translated at runtime by the system into the SQL syntax that is appropriate for the type of database you are using. We call this form of SQL Meta-SQL.
In most cases, Meta-SQL syntax is the same as standard SQL syntax. This means that in most cases, you can type the SQL syntax that you are familiar with. If, however, you use functions that are specific to one database server (e.g., Oracle), when you run the query on a different database server (e.g., SQL Server), an error appears, and you will be unable to run the query and view its results until you correct the SQL code. It is a good practice to use Meta-SQL whenever possible.
In other words, when you type SQL code directly and select , the following events occur:
- APM reads the syntax and determines whether or not it is valid Meta-SQL.
- APM translates the Meta-SQL into SQL that can be interpreted by the type of database you are using.
- The database executes that translated code and returns results to APM.
- The query results appear.
Throughout the APM documentation, we use the term SQL when referring to the SQL code that appears when you select SQL in the content header.
All tasks that you can perform when designing a query write Meta-SQL code that can be viewed when you select the SQL tab. Not all SQL code that you enter directly, however, can be interpreted by the Design workspace. This means that in some cases, you can write SQL code that will cause the Design workspace to be unavailable. If you write a query using SQL code and want other users to be able to modify the query design, first try to access the Design workspace to make sure that it is available before saving the query.
Access the Query Page
Procedure
The Query page appears, displaying a list of queries.
Access the Design Workspace
Access the SQL Workspace
Procedure
- To access the SQL workspace for a new query:
- To access the SQL workspace for an existing query:
Modify a Query
Procedure
Modify the Query Type
About This Task
Procedure
Include or Exclude a Field in the Query Results
Before You Begin
When you create a query, all the fields that you added are selected by default to be included in the query results and displayed in the Results workspace. In some cases, you might want to add a field to the grid in the Conditions section so that you can define criteria for that field without including it in the query results.
About This Task
Procedure
Results
- When you clear the Include check box for a field, the Display check box for that field is automatically cleared. Fields can be displayed in the results only if they are also included in the query results.
- If you exclude a field from the query results, you must define content in the Criteria cell, the Sort cell, or the Total cell in that column for the field to be saved with the query code. In other words, if the field is not included in the query results, it must be included in SQL in some other way to be saved with the query. Otherwise, when you open or run the saved query, the excluded field will not be displayed in the Design, Results, or SQL workspaces.