Sum mysql decimal values

Asked

Viewed 605 times

0

I made a query that adds a decimal field, only I’m having trouble, because when I put the clause group by t1.id_sell it lists me sales but no longer sums correctly and without the correct sum clause but only returns me a line.

query:

SELECT Sum(t1.preco_sell) AS result_sum, t1.*, t2.name, t3.name_produto 
FROM   tb_sell t1 
       INNER JOIN tb_user t2 
               ON t1.id_user = t2.id_user 
       INNER JOIN tb_produtos t3 
               ON t1.id_produto = t3.id_produto 
WHERE  t1.status = 2 
GROUP  BY t1.id_sell 
  • It would be nice for you to create tables with minimal structure and content that reproduce the problem and post along with the question to facilitate the work of those who propose to answer.

1 answer

0


The problem lies in yours in the fields you display in your Select, at its junction on Group By

If you wanted to display the sum of all items sold in a new column, what you should do is a Subquery for that reason

SELECT t1.*, t2.name, t3.name_produto,
(SELECT Sum(preco_sell)
    FROM   tb_sell
    WHERE  status = 2
)AS result_sum
FROM   tb_sell t1
       INNER JOIN tb_user t2
               ON t1.id_user = t2.id_user
       INNER JOIN tb_produtos t3
               ON t1.id_produto = t3.id_produto
WHERE  t1.status = 2

When you order display all items from t1 the query will not be able to group, since all fields probably have different values and this is not returning what you want

  • Jeferson this query also did not work :/

  • vc could inform what you really expect in the return of the query, along with the fields

  • I want to return the sum of the sales values together with all sales only separated by the status Where clause = 2 (from sold in the application)

  • that is only the beginning, because then the intention is to put some filters

  • In case you want q show all information from your sales chart and a field next to the total generated from all your sales, without being grouped by nd?

  • in vdd I’m new using group by Joseph so for me it’s as young as still use it.. so I’m picking up a few rs.. but the intention is that

  • In this case, what you need is a subquery that will return you this sum, qnd vc uses the group by it will group by its fields that are in select, then n has to display this 2 information this way, I will edit the query and you try with it

Show 3 more comments

Browser other questions tagged

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