可有人建議我該怎麼辦,我們認爲本週在週日開始,到星期六結束,而在像week1,week2 52周滾動報告向後編號他們.. week5252周與運行一週數據滾
我想從週日開始計算我的本週爲第一週,所以即使它的部分周仍是第一週,而上週的週日到週六是第二週,如此直到去年的第52周(大致在九月倒數)。我需要這個,因爲我正在處理日常報告,它將查找本週和過去51周(全周)的銷售情況。我的報告還應該返回沒有銷售'0'的任何一週而不跳過它。
可有人建議我該怎麼辦,我們認爲本週在週日開始,到星期六結束,而在像week1,week2 52周滾動報告向後編號他們.. week5252周與運行一週數據滾
我想從週日開始計算我的本週爲第一週,所以即使它的部分周仍是第一週,而上週的週日到週六是第二週,如此直到去年的第52周(大致在九月倒數)。我需要這個,因爲我正在處理日常報告,它將查找本週和過去51周(全周)的銷售情況。我的報告還應該返回沒有銷售'0'的任何一週而不跳過它。
這是一種方法。注意我創建了遞歸CTE
來填充某些日期。您不必執行此步驟,實際只需要YourWeekOrder = ...
部分。
declare @startDate date = dateadd(year,-1,getdate())
declare @endDate date = getdate()
;with cte as(
select @startDate as TheDate
union all
select dateadd(day,1,TheDate)
from cte
where TheDate < @endDate)
select
TheDate
,TheWeekOfYear = datepart(week,TheDate)
,YourWeekOrder = dense_rank() over (order by cast(datepart(year,TheDate) as char(4)) + case when len(datepart(week,TheDate)) = 1 then '0' + cast(datepart(week,TheDate) as char(2)) else cast(datepart(week,TheDate) as char(2)) end desc)
from cte
order by
TheDate
option(maxrecursion 0)
肯定的是,使用[日期部分(周,dateColumn)(https://docs.microsoft.com/en-us/sql/t-sql/functions/datepart-transact -sql)以及getdate()和一些簡單的數學運算 – scsimon
如果我在08/01和08/07之間的日期這樣做,這將只給我一年中的當前周,它會給我31號這是本週的本週一年,計數回來應該告訴我,這是第5周和前一週,因爲第6周等。 – Sanj
所以...你正在使用SQL Server? – Hogan