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
here is my output i need prequantity to come from the week of year - 1 week which would make it the 52nd week of 2014
i need this value to come dynamically so that it can be understood by parameters