У меня проблема с этим календарем, расчеты недель в году работают правильно, исходя из наших потребностей, но теперь мне нужно добавить переменную итерации ( @ runningWeekSeed ), которая увеличивается на 1 при изменении недели ( @ WorkWeekSeed ).
Я не уверен, правильно ли я объяснил.
/*
This Query is built for a 4-4-5 calendar, Mon - Sun week.
Each Fiscal year start on 01/01/YYYY ends on 12/31/YYYY.
*/
DECLARE
@FiscalCalendarStart DATETIME, -- Calendar starts
@EndOfCalendar DATETIME, -- Calendar ends
@CurrentDate DATETIME, -- Calendar date being calculated against
@RunningDaySeed INT, -- Days calculations
@RunningWeekSeed INT, -- Weeks calculations
@FiscalYearSeed INT, -- Years calculations
@RunningPeriodSeed INT, -- Fiscal Month
@WorkWeekSeed INT, -- Fiscal Week
@WorkQuarterSeed INT, -- Fiscal Quarter
@WorkPeriod INT, -- Used to assign where the extra "leap week" goes. Based on the 4-4-5 calendar.
@WeekEnding DATETIME
DECLARE @FiscalTimePeriods AS TABLE (
Sysdate VARCHAR (10), -- Date with format MMDDYYYY
Date_Time_Target DATETIME, -- The date, but with Time duh
MSC_Week INT, -- Running week according MSC Calendar
MSC_Year INT, -- Calendar with corresponding days into the Fiscal year
weekEnding DATETIME,
runningWeekSeed INT
)--Of Table
/* These are user variables, and should be set according every need */
SELECT
@FiscalCalendarStart = '20150101', -- The date of year start. Used as the base date for calculations (This case will be on 12/29/2015 to match 7 days week for week 1 of 2015)
@EndOfCalendar = '20901231' , -- The date on which the calendar query will end. Can be adjusted for each need
@RunningDaySeed = 1, -- This is used to measure the number of days since the calendar began, often used for depreciation (usually 1)
@RunningPeriodSeed = 1, -- The number of fiscal months since the original calendar began (usually 1)
@RunningWeekSeed = 1, -- The number of fiscal weeks since the original calendar began (usually 1)
@FiscalYearSeed = 2015 -- The starting fiscal year (first 3 days of week 1 belongs to 2014 but it's on 2015 fiscal calendar)
/* These are iteration variables, do not mess with these */
SELECT
@WorkPeriod = 1 ,
@WorkWeekSeed = 1 ,
@WorkQuarterSeed = 1
/* The loop is iterated once for each day */
SET @CurrentDate = @FiscalCalendarStart
SELECT @WeekEnding = dateadd ( D , 8 - DATEPART ( DW , @CurrentDate ), @CurrentDate )
WHILE @CurrentDate <= @EndOfCalendar
BEGIN --MAIN BEGIN
INSERT INTO @FiscalTimePeriods
SELECT
CONVERT ( VARCHAR (10), @CurrentDate , 101 ) AS SysDate,
@CurrentDate AS Date_Time_Target,
@WorkWeekSeed AS MSC_Week,
YEAR (@CurrentDate) AS MSC_Year,
@WeekEnding,
@RunningWeekSeed
/* Iterate the date and increment the RunningDay */
SET @CurrentDate = DATEADD ( D , 1 , @CurrentDate )
SELECT @RunningDaySeed = @RunningDaySeed + 1,
@WeekEnding = dateadd ( D , 8 - DATEPART ( DW , @CurrentDate ), @CurrentDate )
/* If @CurrentDate is 01/01/YYYY, reset fiscal counters */
IF (DATEPART (MONTH, @CurrentDate) = 1 AND DATEPART(DAY, @CurrentDate) = 1)
BEGIN
SELECT
@WorkPeriod = 1 ,
@WorkWeekSeed = 1 ,
@WorkQuarterSeed = 1,
@FiscalYearSeed = @FiscalYearSeed + 1
END
ELSE
/*************Check if Monday is between 12/23 to 12/31 *****************/
IF DATEPART ( DW , @CurrentDate ) = 2 -- Check if @CurrentDate is monday
IF DATEPART (MONTH, @CurrentDate) = 12 AND (DATEPART(DAY, @CurrentDate) in (23, 24, 25, 26, 27, 28, 29, 30, 31)) -- If @CurrentDate is Dec 23 to 31
BEGIN
SELECT
@WeekEnding = dateadd ( D , 8 - DATEPART ( DW , @CurrentDate ), @CurrentDate ),
@WorkWeekSeed = 52,
@WorkQuarterSeed = 4,
@WorkPeriod = 12
END
ELSE
IF DATEPART ( DW , @CurrentDate ) = 2
BEGIN
SELECT
@WeekEnding = dateadd ( D , 8 - DATEPART ( DW , @CurrentDate ), @CurrentDate ),
@WorkWeekSeed = CASE
WHEN @WorkWeekSeed = 53 THEN 52
ELSE @WorkWeekSeed + 1
END
END
/* Compare if current date acomplish ISO_WEEK standard ISO 8601*/
IF DATEPART ( ISO_WEEK , @CurrentDate ) = 1
SELECT
@WorkPeriod = 1,
@WorkWeekSeed = 1,
@WorkQuarterSeed = 1
------------------------HERE IS WHERE START TO DO THE CALCULATION OF THE runningWeekSeed-------------------------
IF @WorkWeekSeed = 52
SELECT @WeekEnding = dateadd ( D , 8 - DATEPART ( DW , @CurrentDate ), @CurrentDate ),
@RunningWeekSeed = @RunningWeekSeed
ELSE
IF DATEPART(DW, @CurrentDate) = 2
SELECT @RunningWeekSeed = @RunningWeekSeed + 1
ELSE
IF (DATEPART (MONTH, @CurrentDate) = 1 AND DATEPART(DAY, @CurrentDate) = 1)
SELECT @RunningWeekSeed = @RunningWeekSeed + 1
END -- MAIN BEGIN
SELECT
*
FROM @FiscalTimePeriods
ORDER BY Date_Time_Target ASC
Нужные результаты должны быть такими: