Defining filters for logical tables in a semantic view¶
In a semantic view, you can define a filter for a logical table. A filter is a fact or dimension that you can also use as a condition in the WHERE clause of a query. The fact or dimension must resolve to a BOOLEAN value.
Cortex Analyst can use this filter when producing SQL queries.
Defining a filter¶
Define a filter in the DIMENSIONS or FACTS clause of the CREATE SEMANTIC VIEW command. When you define a filter, you specify LABELS = (FILTER) to indicate that the dimension or fact is also a filter:
where sql_expr is a conditional expression for the filter.
For example, suppose that you want to create a semantic view for the following tables, which contain information about customers and orders:
Suppose that you want to set up the semantic view so that you can query the view and filter the results, based on the following criteria:
- Orders that are completed
- High value orders (orders that amount to more than $100)
- Important customers that have 150 or more loyalty points and that have placed orders of more than $1000
- Customers that are active
- Customers in the United States
When you create the semantic view, you can define filters for these criteria:
In the example above, LABELS = (FILTER) indicates that the item is a fact or dimension that can also be used as a filter. The example defines the following filters:
-
Filters that are facts:
orders.completed_onlyis TRUE if an order has been completed.orders.high_value_orderis TRUE if the value is more than $100.customers.vip_customeris TRUE if the customer has 150 or more loyalty points and has placed orders of more than $1000.
-
Filters that are dimensions:
customers.active_onlyis TRUE if the customer is active.customers.region_usis TRUE if the customer is in the United States.
Specifying a filter in a query¶
When you query the semantic view, you can specify the name of the filter as a condition in the WHERE clause.
For example, to return rows for completed orders, specify the orders.completed_only filter in the WHERE clause. The following
example uses a SEMANTIC_VIEW clause:
The following example uses standard clauses in a SELECT statement to query the view:
You can use these filters with the standard logical operators (AND, OR, NOT) as well as with other criteria. For example:
Viewing information about filters¶
When you run the DESCRIBE SEMANTIC VIEW, SHOW SEMANTIC FACTS, and SHOW SEMANTIC DIMENSIONS commands, the output includes information about the filters:
-
For facts and dimensions that are filters, the output of the DESC SEMANTIC VIEW command includes the
LABELSproperty, which is set to["filter"]. TheEXPRESSIONproperty is set to the SQL expression for the filter. -
For facts and dimensions that are filters, the output of the SHOW SEMANTIC FACTS and SHOW SEMANTIC DIMENSIONS commands include the
labelscolumn, which contains["filter"].
In addition, the INFORMATION_SCHEMA SEMANTIC_DIMENSIONS and
SEMANTIC_FACTS views contain an additional VARIANT column named labels. The
column contains "filter" if the fact or dimension is a filter. For example: