Given a table with the fields id, product name, user_id and the amount of a single purchase. It is necessary to choose the user_id total amount of purchases, which are more than 1000, I have already broken my head,

SELECT user_id FROM table1 WHERE SUM(sum)>=1000; 

- does not work. Thanks in advance

  • one
    1. @IlyaZhilenkov, This is a question on the HashCode forum .2. SUM read about him: he does not work this time. Two, here you need to use either 2 requests or combine them into one. - Artem

1 answer 1

let there be columns: id, user_id, price

then your sample may look like this:

 select * from (select user_id, sum(price) as total from table1 group by user_id) as p where total > 1000; 

or in one request:

 select user_id, sum(price) as total from table1 group by user_id having total > 1000;