日期:2014-05-17 浏览次数:20439 次
select itemno , accper , bgnqty , proqty from invtory
union all
select itemno , convert(int,accper)+1 , bgnqty , proqty from invtory where accper =' 201301'
select * into #t from (
select 100 itemno,'201301' accper,0 bgnqty, 10 proqty
union all select 110, '201301', 0, 10
union all select 110, '201302', 0, 5
union all select 110, '201303', 0, 6
)a
select itemno,accper,sum(bgnqty)bgnqty,sum(proqty)proqty from (
select * from #t
union all
select itemno,'201302'accper,proqty as bgnqty,proqty from #t where accper='201301'
)a
group by itemno,accper
order by itemno,accper
/*
itemno accper bgnqty proqty
100 201301 0 10
100 201302 10 10
110 201301 0 10
110 201302 10 15
110 201303 0 6
*/
;with cte as (
select itemno,accper,sum(bgnqty)bgnqty,sum(proqty)proqty f