I'm trying to calculate the gross margin and gross margin %. I have the gross margin calcualting. I'm now trying to calculate the %. It keeps coming back with 0 which doesn't make sense to me. Basically I'm taking the sum of the gross margin and dividing it by the sum of the sales. Since in the results I have values in both the sales and what I calculate to be gross margin I do not expect 0. Based on the results I would expect .42 What am I doing wrong.
Here's the results:
net_sales cost_Of_sales gross_margin gross_margin_percent
----------- ------------- ------------ --------------------
6315935 3674110 2641825 0
(1 row(s) affected)
Here's the query:
SELECT
sum(dbo.WeeklySalesHistory.SalesRetailVATInclusive) as net_sales,
sum(dbo.WeeklySalesHistory.CostofSales)as cost_Of_sales,
sum(dbo.WeeklySalesHistory.SalesRetailVATInclusive - dbo.WeeklySalesHistory.CostofSales) as gross_margin,
sum(dbo.WeeklySalesHistory.SalesRetailVATInclusive - dbo.WeeklySalesHistory.CostofSales)/sum(dbo.WeeklySalesHistory.SalesRetailVATInclusive)as gross_margin_percent
FROM
dbo.WeeklySalesHistory
WHERE
(
( (dbo.WeeklySalesHistory.WeekEndingDate
IN(SELECT DISTINCT(weeks.isodate)
FROM dbo.Weeks , dbo.MonthEndingDates JOIN dbo.YearParameters
ON dbo.MonthEndingDates.MonthStartDate >= dbo.YearParameters.Start_Date
and dbo.MonthEndingDates.MonthEndDate <=dbo.YearParameters.End_Date and
dateadd(dd, -1, getdate()) >= dbo.YearParameters.Start_Date and
dateadd(dd, -1, getdate()) <= dbo.YearParameters.End_Date
WHERE (dbo.weeks.Isodate >=dbo.MonthEndingDates.MonthStartDate
AND dbo.weeks.Isodate <=dbo.MonthEndingDates.MonthEndDate
And dbo.MonthEndingDates.MonthNum = 1 ) )) ))
Here's the results:
net_sales cost_Of_sales gross_margin gross_margin_percent
----------- ------------- ------------ --------------------
6315935 3674110 2641825 0
(1 row(s) affected)
Here's the query:
SELECT
sum(dbo.WeeklySalesHistory.SalesRetailVATInclusive) as net_sales,
sum(dbo.WeeklySalesHistory.CostofSales)as cost_Of_sales,
sum(dbo.WeeklySalesHistory.SalesRetailVATInclusive - dbo.WeeklySalesHistory.CostofSales) as gross_margin,
sum(dbo.WeeklySalesHistory.SalesRetailVATInclusive - dbo.WeeklySalesHistory.CostofSales)/sum(dbo.WeeklySalesHistory.SalesRetailVATInclusive)as gross_margin_percent
FROM
dbo.WeeklySalesHistory
WHERE
(
( (dbo.WeeklySalesHistory.WeekEndingDate
IN(SELECT DISTINCT(weeks.isodate)
FROM dbo.Weeks , dbo.MonthEndingDates JOIN dbo.YearParameters
ON dbo.MonthEndingDates.MonthStartDate >= dbo.YearParameters.Start_Date
and dbo.MonthEndingDates.MonthEndDate <=dbo.YearParameters.End_Date and
dateadd(dd, -1, getdate()) >= dbo.YearParameters.Start_Date and
dateadd(dd, -1, getdate()) <= dbo.YearParameters.End_Date
WHERE (dbo.weeks.Isodate >=dbo.MonthEndingDates.MonthStartDate
AND dbo.weeks.Isodate <=dbo.MonthEndingDates.MonthEndDate
And dbo.MonthEndingDates.MonthNum = 1 ) )) ))