When I run this:
SELECT
R.RegionID AS Region, C.CountryName AS Country,
S.SegmentName AS Segment, SUM(SKPI.KPI) AS YearlySalesKPI,
ROUND(SUM(SOLI.SalePrice), 2) AS YearlySales
FROM
Country C
INNER JOIN
Region R ON C.CountryID = R.CountryID
INNER JOIN
Segment S ON R.SegmentID = S.SegmentID
INNER JOIN
SalesRegion SR ON R.RegionID = SR.RegionID
INNER JOIN
SalesPerson SP ON SR.SalesPersonID = SP.SalesPersonID
INNER JOIN
SalesKPI SKPI ON SP.SalesPersonID = SKPI.SalesPersonID
INNER JOIN
SalesOrder SO ON SR.SalesRegionID = SO.SalesRegionID
INNER JOIN
SalesOrderLineItem SOLI ON SO.SalesOrderID = SOLI.SalesOrderID
INNER JOIN
Product P ON SOLI.ProductID = P.ProductID
INNER JOIN
ProductCost PC ON PC.ProductID = P.ProductID
AND PC.CountryId = C.CountryID
GROUP BY
R.RegionID, C.CountryName, S.SegmentName
ORDER BY
C.CountryName ASC, R.RegionID ASC;
The query takes over 1 minute to complete, and when it does it gives me results that are larger than the real values.
I believe the additional tables are adding additional rows - when it shouldn't. But I am unsure on how to stop this from happening. I think their might be something wrong with the relationships but I'm unsure.
I have tried all methods. Any help is appreciated.
This is my database:
Expected Result: Expected results
Expected Results: Expected results
The expected results are the above columns shown in one query output.