I have a problem, I am trying to make a query in which I need to know the sales amount of each product and what percentage it represents in total.
This is the code that I have used (gives an error)
SELECT P.ProductName as 'Nombre del Producto',
SUM(P.UnitPrice * P.UnitsOnOrder) as 'Importe de Ventas'
SUM(('Importe de Ventas') / 100) as 'Porcentaje del total'
FROM Northwind..Products as P
GROUP BY P.ProductName;
go
The problem is that I need to know the total sum of the sales amount to then calculate the percentage that corresponds to each one, but I can't find a way to achieve that.
If I run the code without the third line it gives me this result
attached the model
The first thing you do is a subquery , where you are going to get the name of the product and its total sales amount (for each product). Later you make a general query on this subquery , and this is where you would obtain the percentage for each product using this expression: