COUNT function returns the number of unique values of an attribute within the given context.
COUNT function can be used in the following three syntax forms:
SELECT COUNT(Order ID)
SELECT COUNT(attribute, primary_key)
SELECT COUNT(Order ID, Campaign ID)
SELECT COUNT(attribute) USING primary_key
SELECT COUNT(Order ID) USING Campaign ID
The single-parameter version of COUNT dynamically gets the context of where to count from the insight it is used in.
In the two-parameter version, the context where to count the attribute is determined explicitly by the second parameter - the primary key of the dataset.
The primary key (connection point) connects the COUNT function’s first parameter to the dataset in which the count is to take place. For more information, see Connection Points in Logical Data Models.
Specifying COUNT Context Resolution with USING
You deploy the USING keyword in logical data models with ambiguous primary keys.
The context for computation of COUNT may be ambiguous if there are multiple fact tables which relate to a counted attribute. Imagine a model with fact datasets
Sales_Fact that are both connected to the
If you build an insight/report that displays the count of products per store with
SELECT COUNT(Product) and slice it with Store, it would not compute because it is ambiguous if the insight/report displays the number of unique products that have been purchased by store or the number of unique products that have been sold by the store.
In a metric, USING provides a hint for which context should be used. For example the report will show the number of unique purchased products per store if
Purchase_Fact attribute is placed into the USING clause
SELECT COUNT(Product) USING Purchase_Fact.
The attribute in the USING clause does not need to be from the actual fact table, it can also be another attribute which uniquely determines the correct context (e.g. use of the
Purchase Date attribute in the USING clause directs the use of the
Purchase_Fact dataset to join Product with Store because
Sales_Fact does not directly relate to Purchase Date).
The following chart shows the XAE3 usage of a single-parameter COUNT in SELECT COUNT(Order ID), sliced with Order Status, and stacked by Product Category.
The example is based on the logical data model outlined in GoodData Demo Workspace. As you can see,
SELECT COUNT(Order ID) does not need another parameter to work despite the fact that it is not directly connected the dataset with Product Category.
In a two-parameter setting, you would ‘connect’ the Order ID attribute with the rest of the model by specifying the second parameter in the COUNT function:
SELECT COUNT(Order ID, Order Line ID).
To learn more, see XAE - Extensible Analytics Engine.