我的桌子有四列,如下所示
select * from Table A order by ID , SUB_ID
将包含以下数据
ID SUB_ID REVENUE PAY
100 1 10 8
100 2 12 9
100 3 9 7
100 4 11 11
101 1 6 5
101 2 4 4
101 3 3 2
101 4 8 7
101 5 4 3
101 6 3 3
我的LIMIT值不变 20 。现在,当使用每个ID的SUB_ID(递增顺序)进行连续SUM时,我需要找到收入越过LIMIT的SUB_ID,然后找到总的Pay ##。在这个例子中
基本上我需要找到超过极限的行。
答案 0 :(得分:2)
小提琴: http://sqlfiddle.com/#!4/4f12a/4/0
with sub as
(select x.*,
sum(revenue) over(partition by id order by sub_id) as run_rev,
sum(pay) over(partition by id order by sub_id) as run_pay
from tbl x)
select *
from sub s
where s.run_rev = (select min(x.run_rev)
from sub x
where x.id = s.id
and x.run_rev > 20);