日期:2014-05-17 浏览次数:20466 次
with tb(nos,datatime,money,nos3,symoney) as (
select 'A00001','20130101',200,'D0001',800 union all
select 'A00002','20130102',200,'D0001',null union all
select 'A00003','20130103',200,'D0001',null union all
select 'B00001','20120101',200,'D0002',null union all
select 'B00002','20120102',200,'D0002',null union all
select 'B00003','20120103',200,'D0002',null )
,tbb(nos,datatime,money,nos3)as(
select 'YSA00001','20130101',1000,'D00001' union all
select 'YSA00002','20130105',1000,'D00002'
)
select a.nos,a.datatime,a.money,a.nos3,c.money-sum(b.money)/2 as symoney from tb a,tb b,tbb c
where b.datatime<=a.datatime and a.nos3=b.nos3
group by a.nos,a.datatime,a.money,a.nos3,c.money
select zmoney-(select sum(money) from tb where nos3=a.nos3 and nos<=a.nos group by nos3)
from tb as a