Sum of a product returning in a column the total

Asked

Viewed 68 times

1

Sum of a product returning in a column the total

Guys good night, I wonder how I can make a SELECT that returns exactly the same as what is below (I modified in F12 to see if you can understand better)

inserir a descrição da imagem aqui

i made a SELECT that is returning the total value but it is grouping the products see below how it is being returned

sql = SELECT *, SUM(qty_product) FROM quintada_management total GROUP BY name_product 

inserir a descrição da imagem aqui

how could solve this "problem"?

  • You wouldn’t have to group id_m_product instead of name_product?

  • You want to take all the fields of a random line of that product, among all the lines of that product, and at the end add the total amount of that?

  • @anonimo That’s right, I want to add the products, and create a column with the total, but I don’t want the same products to be grouped together

  • @Heitorscalabrini I have already tried to do this, but the total column does not return only the quantity of the product and does not sum

2 answers

2


You can, if you do this using sub-query

SELECT Q.*, (SELECT SUM(qty_product) FROM quintada_management as SQ where SQ.name_product = Q.name_product) as total FROM quintada_management as Q
  • 1

    Here where SQ.name_product = SQ.name_product had a typo, the correct is where SQ.name_product = Q.name_product.

  • This right your comment, I’m correcting

0

From what I understand, you want to group by id_m_product and not by the name of the product. It looks like this.

sql = SELECT *, SUM(qty_product) FROM quintada_management GROUP BY id_m_product

Browser other questions tagged

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