In EQL, you can define statements and then refer to these statements from other statements.
Multiple EQL sub-queries can be specified within the context of a single navigation query, each corresponding to a different analytical view, or to a subtotal at a different granularity level. Expressions also can use values from other computed statements. This is often useful when coarser subtotals are required for computing analytics within a finer-grained bucket.
For example, when computing the percent contribution for each sales representative in a given year, you must also calculate the overall total for the year. You can use a lookup table to create these types of queries.
A lookup expression is a simple form of join. It treats the result of a prior statement as a lookup table.
<LookupExpr> ::= <statement-name>[<LookupList>].<attribute-name>The square bracket operators are literal and are used to identify the record set and grouping attribute, while the dot operator is also literal and is used to identify the field.
<LookupList> ::= <empty> ::= <SimpleExpr> [,<LookupList>]In this BNF syntax, the square brackets indicate the optional use of a second LookupList.
The lookup list corresponds to the grouping attributes of the specified statement. The result is NULL if the lookup list does not match target group key values, or the target column is NULL for a matching target group key values.
DEFINE YearTotals AS SELECT SUM(SalesAmount) AS Total GROUP BY Year ; RETURN AnnualCategoryPcts AS SELECT SUM(SalesAmount) AS Total, Total/YearTotals[Year].Total AS Pct GROUP BY Year, Category ; RETURN YoY AS SELECT YearTotals[Year].Total AS Total, YearTotals[Year-1].Total AS Prior, (Total-Prior)/Prior AS PctChange GROUP BY Year
For example, suppose we want to compute the percentage of sales per ProductType per Region. One aggregation computes totals grouped by Region, and a subsequent aggregation computes totals grouped by Region and ProductType.
This second aggregation would use expressions that referred to the results from the Region aggregation. That is, it would allow each Region and ProductType pair to compute the percentage of the full Region subtotal represented by the ProductType in this Region:
DEFINE RegionTotals AS SELECT SUM(Amount) AS Total GROUP BY Region RETURN ProductPcts AS SELECT 100 * SUM(Amount) / RegionTotals[Region].Total AS PctTotal GROUP BY Region, ProductType
This example computes for each quarter the percentage of sales for each product type.
This query requires calculating information in one statement in order to use it in another statement.
DEFINE QuarterTotals AS SELECT SUM(Amount) AS Total GROUP BY Quarter ; RETURN ProductPcts AS SELECT 100 * SUM(Amount) / QuarterTotals[Quarter].Total AS PctTotal GROUP BY Quarter, ProductType