Percentage of sales for the current month

0

I want to get the percentage of sales for the month of the current year. In other words, I sum the total sales of the month of each month and I divide them by the total sales of the year.

SELECT
    *,
    sum(total_venta) as total_venta 
FROM compra 
GROUP BY MONTH(fecha_factura) /
    (SELECT
         sum(total_venta) * 100 as porcentaje
     FROM compra)

But it's not coming out.

    
asked by Magnolia Eve 01.10.2017 в 02:11
source

2 answers

1

I found this example just check the names of your table and columns, modify these in the following query.

applying OVER () to get summary total and percent on base.

SELECT   MONTH = MONTH(OrderDate), SUM(TotalDue) AS SalesByMonth, 
         100.0 * ((SUM(TotalDue)) / (SUM(SUM(TotalDue)) 
                                       OVER())) AS PctSalesByMonth 
FROM     AdventureWorks2008.Sales.SalesOrderHeader 
WHERE    YEAR(OrderDate) = 2003 
GROUP BY MONTH(OrderDate) 
ORDER BY MONTH;

Result

    
answered by 01.10.2017 в 08:41
0

Assuming that your query is in mysql, you can do the query by doing the sum and the subquery directly within the main query. You can use the following code:

SELECT 
    *, 
    sum(total_venta) as total_venta, 
    sum(idTipoProducto)/(SELECT SUM(total_venta) FROM compra ) * 100 as porcentaje_venta 
FROM compra 
GROUP BY MONTH(fecha_factura);
    
answered by 19.10.2018 в 05:06