Hello,
We've the following SQL that we use to calculate total costs.
SELECT
DailyProduction.CustomerId as CustomerId,
SUM(Resource.CostPerUnit * DailyProduction.UnitsToStorage + Resource.CostPerUnit * DailyProduction.UnitsToMarket) AS TotalCost
FROM
dbo.hgm_ResourceTypes Resource
JOIN
dbo.hgm_ResourceDailyProduction DailyProduction
ON
Resource.ResourceId = DailyProduction.ResourceId
GROUP BY
DailyProduction.CustomerId
As is evident, we gather data from two different tables to calculate the total cost of production for each customer.
We now have a requirement to add another table to this mix. This new table identifies the discount that applies to each resource per customer. Furthermore, multiple rows may refer to the same customer and the same resource. In such a case, all discounts must be summed together to identify the total discount.
Eg:
CUSTOMER: 1 RESOURCE: 1 DISCOUNT: 1
CUSTOMER: 1 RESOURCE: 1 DISCOUNT: 3
CUSTOMER: 1 RESOURCE: 2 DISCOUNT: 5
So, we have to identify the total discount per customer for each resource (that's fairly easy to do for me). And then use that discount in the SQL above and deduct it from the CostPerUnit for that particular resource when calculating the TotalCost column (hopefully that makes sense). I've been trying all sorts of joins and I'm hoping someone here can help me with this. Any help is much appreciated.
Thanks!