I have a table
create table emp
(
id, int,
salary int
)
I need to return all rows having a rolling sum less than a given input X order by id.
Query I have thought of is:
Select *
From
(select id, salary, sum(salary) over (order by id ASC) as rollingSum
from emp) as temp_view
where rollingSum < X
It is able to provide correctly what I need.
But subquery will do a rolling sum till end even though sum is achieved. All the rows till end will be searched .
Is there a way we can stop the rolling sum as soon as the sum is achieved?