There are two requests, they are executed from the same database.
select userid, type, sum(amount) from fct_bonus where userid=123 and date<='2016-08-01' group by type CREATE TABLE Tbalance (select userid, sum(case when type = 'IN' then amount when type = 'OUT' then - amount end) as balance from fct_bonus where date <= '2016-08-01' group by userid) SELECT COUNT(userid) AS CountBalance FROM Tbalance WHERE balance > 10000 It is necessary to start continuous execution for each request separately with changing date. The date list for which you need to perform these queries contains 300 values and is a separate table. That is, each of the two requests must be executed 300 times.
In the assignment, this is called a view / aggregate ...