select distinct
pos.DivNo
,WeekOfYear
,[ProductCode]
,sum(Quantity)[Quantity]
into #sales1
from cbis799p.dbo.pos pos
LEFT JOIN DataWarehouse.dbo.Calendar c on pos.PosDate = c.Datetime
Where pos.DivNo = 772 and ProductCode = '1020'
and WeekOfYear = 1
and YearWeek = 2015
group by pos.DivNo
,WeekOfYear
,[ProductCode]
select * from #sales1
drop table #sales1
select distinct
pos.DivNo
,Min(WeekOfYear) WeekOfYear
,[ProductCode]
,SUM(quantity) PreQuantity
into #sales2
from cbis799p.dbo.pos pos
LEFT JOIN DataWarehouse.dbo.Calendar c on pos.PosDate = c.Datetime
where pos.DivNo = 772 and ProductCode = '1020'
and WeekOfYear = 1
and YearWeek = 2015
group by pos.DivNo
,WeekOfYear
,[ProductCode]
select * from #sales2
drop table #sales2
これが私の出力です。年の週から来るには事前数量が必要です-1週間で、2014年の52週目になります
パラメータで理解できるように、この値を動的に取得する必要があります