Joining fact tables can be done but there are some inherent risks so you need to be careful when joining fact tables is required. In the following simple scenario, we have a fact of authors to articles and a separate fact of articles to pageviews. Our customer has asked for the ability to 1) find the authors who have provided the most content in a given time period, find the articles which have the greatest number of pageviews for a given time period and 3) find the authors with the highest number of pageviews in a given time period. Our data model can be seen here:
To answer the first two questions is done by using a simple select statement joining a single fact table with the date dimension and the associated other associated dimensions to retrieve the answer.
TOP 10 AUTHORS BY ARTICLE COUNT
TOP 10 ARTICLES BY MONTH
The third requirement will compel us to join two fact tables to get the answer. Remember to join a fact table you need at least one common dimension shared between the facts to accomplish this task. In this scenario, DIM_ARTICLE will be the common dimension which we will use.
TOP 10 AUTHORS FOR 2019
These simple joins work well on small and medium sized fact tables but as the fact tables increase in size your performance may suffer and therefore you may want to be prepared to address these challenges.
There are two other specific issues to be aware of and avoid, Chasm Traps and Fan Traps.
Chasm traps are queries that require measures from more than one fact table. In the data model below we see 2 separate fact tables, one for sales and one for returns. A common request may be to show the net profit (sales – returns) for the 4th quarter of 2018. We may be tempted to join these two dimensions on DIM_DATE and get the results of the sum of sales and the sum of returns.
Let’s walk through the example by running each query separately. First, let’s get the total sales for each store for the 3rd quarter of 2018 using the following query:
|Store ID||Store Name||Units Sold||Gross Sales|
Next, let’s get the total refunds for each store for the 3rd quarter of 2018 using the following query:
|Store ID||Store Name||Units Returned||Gross Returns|
Finally, let’s put both of these together into a single query with the following query:
The results are inflated due to the cross join which will occurs between the fact tables when trying to get a group function result.
To avoid the chasm trap you need to separate the functions into the base SQL statements we originally designed to verify our results, adding zeros for the columns from the other table, and then union the statements together, wrap them in a SQL statement.
|Store Name||Units Sold||Gross Sales||Units Returned||Gross Returns||Net Units Sold||Net Sales|
Fan traps occur when there is a that require measures from more than one fact table in a master detail relationship. An example of this is an order and order detail relationship.
For this example, our customer has given us a requirement to calculate the average order item. If we try to sum the order value from the fact order table and then sum the quantity of items ordered from the order detail to help us get the average the order value may be inflated due to the cartesian join between the fact tables. Let’s start with a simple select between the customer dimension and the FACT_ORDER table.
|Customer ID||Customer Name||Order Value|
|[email protected]||Emmy Cuevas||353.80|
But when we add in the FACT_ORDER_DTL to the query the result changes in the sum from the FACT_ORDER table.
|[email protected]||Emmy Cuevas||381.70||38||353.80|
Again, this will be exacerbated by the additional number of line items in the FACT_ORDER_DTL to the cross join between the fact tables.
The way to address this challenge can be done in multiple ways. The first is to ignore the FACT_ORDER (parent) when summing information from the FACT_ORDER_DTL (child). If you rely only on the detail table you can be assured your answer will be correct.
Another way to address this challenge is to adjust the design by removing the FACT_ORDER table and inserting the ORDER_ID as a degenerate dimensional value into the FACT_ORDER_DTL.