mySQL - using SUM ()

0

I need to add the total value of an order with more than 1 product included with different unit values.

follows tb_pedido:

id_pedido    | id_produto    | valor_unit | qtd  
-------1-----|-------3-------|----10.00---|--2--  
-------1-----|-------5-------|-----9.00---|--6--  

My select:

   select pedido.id_produto, sum(pedido.qtd),
   ((select sum(valor_unit) from tb_pedido where id_produto = 
   pedido.id_produto) * pedido.qtd) 
   from tb_pedido pedido 
   where id_pedido = 1

The result should be 74.00 , but all select I make the result is (10.00 * 8 = 80.00) or (9.00 * 8 = 72.00).

% w / o takes 1 of both value_unit and multiplies by the sum of qtd     

asked by anonymous 20.07.2018 / 17:07

1 answer

1

You can group by order and add the multiplication, like this:

select id_pedido,
       sum(valor_unit*qtd) as total
  from tb_pedido
 group by id_pedido

See the example working: link

    
20.07.2018 / 17:18