Add values from a table field

Asked

Viewed 768 times

2

I have a table where I store the freight values, but when the cart is updated with the freight value, this value is also updated in the table. But if order #1111 has 3 products in the cart, it will include in this table the updated shipping values. See:

inserir a descrição da imagem aqui

I would like to add the values of the Freight, but with the care of not adding 02 times the freight of the same order, which in case is 16.10. I tried that query, but it didn’t work:

SELECT SUM(ValorFrete) AS ValorTFrete FROM loja_carrinho WHERE StatusCompras = 'aguardando' GROUP BY ValorFrete

It returns me only a first value and does not sum the values.

inserir a descrição da imagem aqui

When grouping by Request, returns this way:

inserir a descrição da imagem aqui

  • Do you want the overall sum of all orders? but, do not repeat by numpedido?

  • 1

    That. I want to add the value of the freight, but not repeating if it is the same Numpedido, ie, in the first image, I would add 16.10 + 17.20 and not 16.10 + 16.10 + 17.20

  • Test the SQL I’ve changed now

1 answer

2


First you must select the repeating items, and then add up the freight value, example:

SELECT SUM(ValorTFrete) FROM
(
    SELECT DISTINCT NumPedido, ValorTFrete           
    FROM loja_carrinho 
    WHERE StatusCompras = 'aguardando'
) AS t
  • Hello Virgil. I also tried this, but it returns me the way the image I just edited in the post. Only the first value 56.20

  • @Fox.11 I’m going to fix SQL

  • @Fox.11 think it’s with distinct! then.

  • 1

    It worked!! Thank you very much Virgil.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.