Help please make a request. I need to get the sum of the prices of all values ​​where id does not repeat

Let's say

id|price|status 1 | 2 | 1 2 | 3 | 1 2 | 3 | 1 3 | 3 | 1 4 | 3 | 4 

As a result, you should get the sum of prices of statuses with unique id.

 status|summ| 1 | 8 | 4 | 3 | 

I do GROUP BY status. But I can not write next GROUP BY id. Can somehow through DISTINCT possible?

  • How to deal with the same id for different statuses? (id, status) - (2, 1), (2,2) - Anton Shchyrov
  • It is necessary to consider the sum of the price, and not id. - Radik Kamalov

1 answer 1

It's not entirely clear what

As a result, you should get the amount of statuses with unique id.

But the result will lead such a query

 SELECT status, SUM(id) AS summ FROM my_table GROUP BY status 
  • Cited in the example. id = 2 is repeated 2 times. those. the amount of status 1 should be 8. In status 4, the amount is = 3. Your sql will give in status 1 an amount equal to 11. - Radik Kamalov
  • one
    @ RadikKamalov Sorry, but again I did not understand anything. Well, id = 2 is repeated 2 times. But why "the sum of the status 1 should get 8"? What is the status amount and where does 8 come from? Ваш sql даст в статусе 1 сумму равную 11 - nonsense! It turns out 8 - Anton Shchyrov
  • It is necessary to consider the price, not id. - Radik Kamalov