I have this view that has a specific, relative week numbering column called WeekNr. The two columns (WeekNr and CURWEEK) in the source table are held with the usual calendar week numbering: 1, 2, 3... to 52.
This view is used in an Excel file, and business wants to see a relative week numbering from -12 to +6: the past 12 weeks and 6 weeks projection, relative to week Zero which is the Current Week (today, 13 January 2015, this is calendar week 2).
So, this code worked just fine, up until the year turned. After 52, the WeekNr - CURWEEK doesn't work anymore as wished.
Do you have some kind of elegant solution for this?
CREATE VIEW [dbo].[vw_NOSSCE_HealthKPIs_DEV]
AS
(
SELECT
[Region]
,[Country]
,[GlobalMaterialCode]
,[Material]
,[MaterialCode]
,[PlantCode]
,[Plant]
,[Brand]
,
(WeekNr - (SELECT TOP 1 CURWEEK FROM [TO_BDB].[dbo].[TO_BDB_NOSSCE_FACT_NORMALIZED]) ) as
WeekNr
,[MEDICAL_NEED]
,[TOP_SELLER]
,[PHARMABU_CODE]
,[PharmaBU]
,[SourceLocationCode]
,[SourceLocation]
,[InventoryTotal]
,[MaxStockQtyTotal]
,[TotalSafetyStockTotal]
,[DangerTreshold]
,[FORECAST_FLG]
,[DangerZoneDesc]
FROM [TO_BDB].[dbo].[TO_BDB_NOSSCE_PAST12_HIST]
WHERE (WeekNr - (SELECT TOP 1 CURWEEK
FROM [TO_BDB].[dbo].[TO_BDB_NOSSCE_FACT_NORMALIZED]) )
BETWEEN -12 and 6
GROUP BY [Region]
,[Country]
,[GlobalMaterialCode]
,[Material]
,[MaterialCode]
,[PlantCode]
,[Plant]
,[Brand]
,WeekNr, [MEDICAL_NEED]
,[TOP_SELLER]
,[PHARMABU_CODE]
,[PharmaBU]
,[SourceLocationCode]
,[SourceLocation]
,[InventoryTotal]
,[MaxStockQtyTotal]
,[TotalSafetyStockTotal]
,[DangerTreshold]
,[FORECAST_FLG]
,[DangerZoneDesc]
UNION ALL
SELECT * FROM [dbo].[vw_NOSSCE_HealthKPIs] WHERE WeekNr > 0
)
UPDATE, I created the Calendar table, why does it give me a Week 53?
CREATE TABLE [dbo].[TO_BDB_NOSSCE_CALENDAR](
[ID] [int] IDENTITY(1,1) NOT NULL,
[DATE] [date] NOT NULL,
[YEAR] AS (datepart(year,[DATE])) PERSISTED,
[SEMESTER] AS (case when datepart(month,[DATE])<(7) then '1' else '2' end) PERSISTED NOT NULL,
[TRIMESTER] AS (case when datepart(month,[DATE])<(4) then '1' else case when datepart(month,[DATE])<(7) then '2' else case when datepart(month,[DATE])<(10) then '3' else '4' end end end) PERSISTED NOT NULL,
[MONTH] AS (case when len(CONVERT([varchar](2),datepart(month,[DATE]),0))=(1) then '0'+CONVERT([varchar](2),datepart(month,[DATE]),0) else CONVERT([varchar](2),datepart(month,[DATE]),0) end) PERSISTED,
[WEEK] AS (case when len(CONVERT([varchar](2),datepart(week,[DATE]),0))=(1) then '0'+CONVERT([varchar](2),datepart(week,[DATE]),0) else CONVERT([varchar](2),datepart(week,[DATE]),0) end),
[DAY] AS (case when len(CONVERT([varchar](2),datepart(day,[DATE]),0))=(1) then '0'+CONVERT([varchar](2),datepart(day,[DATE]),0) else CONVERT([varchar](2),datepart(day,[DATE]),0) end) PERSISTED,
[WEEKNUMBER] AS (datepart(week,[DATE])),
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO