0
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週目になります

パラメータで理解できるように、この値を動的に取得する必要があります

4

1 に答える 1

0
Declare @d Date = '2015-01-01'

    select 
        --Date
        @d
        --WeekYear of Date
        , DatePart(WEEK, @D)
        --Last day of 2014
        , DATEADD(DAY, -1, @D) 
        --Last WeekYear of 2014
        , DatePart(WEEK, DATEADD(DAY, -1, @D))

ここに画像の説明を入力

于 2015-12-18T03:12:05.970 に答える