Sql - presentage from total

Hey Guys,

I have a table caled "Sales":

I wrote a query that returns the total sales in the sports and music departments in the first 3 months of 2016:

SELECT COUNT (*) AS TOTAL_SALES, Department
FROM Sales
WHERE YEAR(DATE) = 2016
AND
MONTH(DATE) IN (1, 2, 3)
AND DEPARTMENT IN (SELECT Department FROM Sales WHERE Department IN ('SPORT', 'MUSIC'))
GROUP BY Department

But now I want to calculate the their relative percentage (MUSIC&SPORT) out of the department's total sales.

I know that I need to calculate it this way, but It returns me an error:
CAST((CAST(COUNT(TOTAL_SALES_MUSIC_2016_Q1) AS FLOAT) / CAST(COUNT(TOTAL_SALES_MUSIC_2016) AS FLOAT) *100) AS VARCHAR) + '%' AS cnv_rate

Thanks,

Michal

First I think you need to adjust the original query:


SELECT SUM(Total_Price) AS TOTAL_SALES, Department
FROM Sales
WHERE DATE >= '20160101' 
AND DATE < '20160104'
AND DEPARTMENT IN ('MUSIC','SPORT')
GROUP BY Department
1 Like