日期:2014-05-17 浏览次数:20476 次
with t as
(
select 'A01' as prod,100 as prodqty,'A' opr, 5 qty,'2012/1/1' [date] union all
select 'A01' as prod,100 as prodqty,'B' opr, 25 qty,'2012/2/1' [date] union all
select 'A01' as prod,100 as prodqty,'C' opr, 35 qty,'2012/3/1' [date] union all
select 'A01' as prod,100 as prodqty,'D' opr, 15 qty,'2012/4/1' [date] union all
select 'A01' as prod,100 as prodqty,'E' opr, 15 qty,'2012/5/1' [date] union all
select 'A01' as prod,100 as prodqty,'F' opr, 10 qty,'2012/6/1' [date] union all
select 'A01' as prod,100 as prodqty,'G' opr, 12 qty,'2012/7/1' [date]
)
select top 1 b.*
from t as b
where (select sum(qty) from t as a where a.date<=b.date)>prodqty
order by [date]