I have developed a data warehouse that follows a star schema. Within this schema I have multiple dimension tables connected to my fact table. However, I have a bridge table between one of these dimensions and the fact table.
i.e. example of my warehouse schema
Product <- ProductGroup <- Fact
Fact table contains: ProductGroup_SK...
ProductGroup table contains: ProductGroup_SK, Product_SK
Product table contains: Product_SK, product_fields...
I will be using Oracle Data Miner to carry out analysis on my data warehouse, and have been trying to determine how to return one row for each fact, containing the product details in the product table, based on the ProductGroup_SK which connects the Fact table to the ProductGroup bridge table.
So far I've been able to join and return one row successfully, however this only works if there are only two products in the productGroup. Also, if only one product exists in the group, this query will return the same product twice as I specify two joins in the query itself using MIN and MAX.
Select * from Fact f
join Product p ON p.Product_SK = (SELECT MIN(Product_SK) FROM ProductGroup pg WHERE pg.ProductGroup_SK = f.ProductGroup_SK)
join Product p ON p.Product_SK = (SELECT MAX(Product_SK) FROM ProductGroup pg WHERE pg.ProductGroup_SK = f.ProductGroup_SK);
I have read that a PIVOT query should be used here, but I do not need to perform any aggregation, and the number of rows in each productGroup will not be the same for each row in the fact table i.e. some groups may have only 1 product, some may have 6.
Basically I was hoping to figure out a way to get the same result as I get by this hack way I've done above, i.e the result instead of looking like this:
... ProductGroup_SK Product_SK Product_Name Product_Category .. etc
123 1 Apple Fruit
123 2 Banana Fruit
Should look like this:
... ProductGroup_SK Product_SK Product_Name Product_Category Product_SK_1 Product_Name_1 Product_Category_1 .. Product_SK_N Product_Name_N Product_Category_N
123 1 Apple Fruit 2 Banana Fruit ........... .......... ..........
The query I demonstrated above produces this desired result, but as you can see it is not good code and is impractical for productGroups that contain less than or more than two products.
Any suggestions would be greatly appreciated, I've been trying to figure out how to do this properly but could not find any good examples of reading data via a bridge table in one row. Is this possible?