Oracle SQL查找行交叉限制
我有一个表,它有四列,如下所示Oracle SQL查找行交叉限制,sql,oracle,oracle11g,lag,Sql,Oracle,Oracle11g,Lag,我有一个表,它有四列,如下所示 身份证 子单元ID。一个ID将有多个子单元ID 收入 薪酬价值始终小于或等于收入的薪酬 从表A中选择*按ID排序,子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
从表A中选择*按ID排序,子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
我有恒定的极限值20。现在,我需要在对每个ID使用SUB_ID(递增顺序)进行连续求和时,找到收入超过限制的SUB_ID,然后找到总工资。在这个例子中
对于id 101,你是怎么得到10和12的?我猜是打字错误。他是说100和101对。。现在更正它。
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);