I have a transaction fact table and product,time and location as a dimension tables. This fact table is sparse so if no Pizzas sold in January there is no record for Pizza in fact table.
When I drill down by product aggregated results for Pizza which is not in the result. But I want it with 0 values as units_sold = 0.
A solution is to join product table to fact table with a left outer join. Then I can get the desired result.
But when I cut with another dimension such as location or time, again those products are missing in the result.
Outer join provides empty column for other dimensional foreign keys so WHERE clause will remove them again.
Use join condition is a good idea as some people answered. But I need more general solution.
For example,
person birth year death year
a 1950 2006
b 1952 2008
c 1960 2007
d 1953 1990
I want to get year by year count of the people that born between 1950-1953 and died in 2006-2008.
Like
birth = 1950 death = 2006 count = 1
birth = 1951 death = 2006 count = 0
...
Can we handle this scenario by using join conditions and where conditions appropriately.
You want LEFT JOIN
, and then LEFT JOIN
again. Then conditions go in the on
clause. Something like this:
select . . .
from products p left join
fact f
on p.product_id = f.product_id left join
timedim td
on f.time_id = td.time_id and
td.month = 'January'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.