Wednesday, 4 January 2017

Constant Selection in SAP HANA Using Dynamic Join

In SAP BW, there is this concept of “Constant Selection” where you can mark a selection in the Query Designer as constant. This means that navigation and filtering will have no effect on the selection at runtime.

In SAP HANA, there is no feature that directly supports this functionality. We have to model it ourselves. One way to implement it is through “self joins” and “dynamic joins”.
One application of “Constant Selection” is when calculating the market share of a product of a company against the same product of other companies. The problem arises when there are additional attributes (e.g. Country) that a user can select that can affect the result.

Let’s take this table as an example:

Constant Selection in SAP HANA Using Dynamic Join

As you can see from this table, companies A, B, and C have products in the US.  And only companies A and B have products in Canada and not company C.

If we want to calculate the “market share” of a product of a company, in BW we should make the “sales by company” as the constant because we want to relate the sales of the product of the individual company to the total sales of the product of the whole group of companies.

To implement this in SAP HANA, we need to create a calculation view and use the same table in two “aggregation” nodes.  Create the first aggregation node as below:

Constant Selection in SAP HANA Using Dynamic Join

In the second aggregation node, we will use the same table but will not expose “COMPANY” because we want to total sales by company.  When adding the aggregated measure “SALES” from this node, rename it as “TOTAL_SALES”.

Constant Selection in SAP HANA Using Dynamic Join

Now create an inner join between these two aggregation nodes on “COUNTRY” and “PRODUCT”. This effectively is a “self join” as we are joining a table to itself.

Constant Selection in SAP HANA Using Dynamic Join

Lastly, to get the market share percentage, we need to create a “calculated column” in the final aggregation node and name it “MARKET_SHARE” using the formula “SALES” / “TOTAL_SALES”.

Constant Selection in SAP HANA Using Dynamic Join

Constant Selection in SAP HANA Using Dynamic Join

Now let’s run the following query including all attributes “PRODUCT”, “COUNTRY”, and “COMPANY”:

Constant Selection in SAP HANA Using Dynamic Join

The total sales are of a particular product totaled for all companies in a country which is correct.  But now, suppose we want to see the market share of a company’s product in North America (both US and Canada).

To do this, we remove “COUNTRY” from the query:

Constant Selection in SAP HANA Using Dynamic Join

But since Company C does not have products in Canada, the total sales for C is only for US.  This is not what we want since we want the total sales to be across both US and Canada.

To solve this problem, we need to change the join between the two aggregation nodes to a “dynamic join”.

Constant Selection in SAP HANA Using Dynamic Join

Now let’s rerun the same query without the “COUNTRY”.

Constant Selection in SAP HANA Using Dynamic Join

Problem solved! The total sales is across both US and Canada for all companies.  The dynamic join takes into consideration the attributes you use in your query.  Since “COUNTRY” is not part of the query, HANA does not execute the join on “COUNTRY” which allows the total sales to remain “constant” across all companies.

3 comments:

  1. Great article thanks for sharing At Pearl’s - The best Bridal Makeup Parlour in Chennai, we take personal responsibility in making sure that you look as flawless and beautiful and the marriage that you have been dreaming of. With around 16,000 successful brides in our books, you can be confident that we know our art intimately and deep.

    ReplyDelete
  2. Excellent blog thanks for sharing While choosing your perfect ride for driving, Accord Cars comes with and the best packages for you to pick from. Car rentals for self drive in Chennai are done the easier. Just pick out your plan from hourly, daily, weekly and even monthly plans available.

    ReplyDelete