WeekOfYear -1 SQL HELPP?

时间:2015-08-21 12:39:09

标签: sql-server visual-studio week-number product-quantity

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

enter image description here

这是我的输出 我需要优先来自一周 - 一周 这将使它成为2014年的第52周

我需要动态调整这个值,以便参数可以理解

1 个答案:

答案 0 :(得分: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))

enter image description here