0

I have the following table Fact_Sales :

ProductSK DateSK SalesAmount SalesNumber 
1         2019   300         150 
2         2019   500         190 
.....

and the following table DimProduct :

ProductSK CategoryLabel 
1         ABC 
2         ABC 
....

I want to calculate the sales by category label but when joining like below the result of the query is generating a cartesian product :

SELECT CategoryLabel, SUM(SalesAmount)
FROM    Fact_Sales,     DimProduct  
Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60

2 Answers2

1

You should use INNER JOIN ON instead:

SELECT CategoryLabel, SUM(F.SalesAmount)
FROM Fact_Sales AS F INNER JOIN DimProduct AS D ON F.ProductSK = D.ProductSK
GROUP BY D.CategoryLabel 
Liza
  • 33
  • 6
0

You can LEFT join to the distinct values of your Product table :

SELECT DP.CategoryLabel, SUM(FS.SalesAmount)
FROM Fact_Sales AS FS
LEFT JOIN (
SELECT DISTINCT ProductSK,CategoryLabel FROM DimProduct
) AS DP
ON DP.ProductSK=FS.ProductSK
GROUP BY DP.CategoryLabel
Amira Bedhiafi
  • 8,088
  • 6
  • 24
  • 60