Моя цель состоит в том, чтобы создать календарь 5-4-4, в котором мой начальный период учета = 2019-04-01, а конечный период = 2020-03-31. Моя финансовая неделя должна начинаться с недели 1 до недели 52 Ниже приведен мой запрос SQL, в котором отображается мой бухгалтерский календарь
DECLARE @START_DATE DATE
DECLARE @END_DATE DATE
DECLARE @COUNTER INT
SET @START_DATE = '2019-04-01'
SET @END_DATE = '2020-03-31'
SET @COUNTER = 1
DECLARE @TABLE TABLE(
CALENDAR_DATE DATE,
WEEK_NUM INTEGER,
START_WEEK DATE,
END_WEEK DATE
)
WHILE @START_DATE <= @END_DATE
BEGIN
SET DATEFIRST 4
INSERT INTO @TABLE (CALENDAR_DATE,START_WEEK,END_WEEK)
SELECT
@START_DATE,
DATEADD(dd, 0 - (@@DATEFIRST + 5 + DATEPART(dw, @START_DATE)) % 7, @START_DATE),
DATEADD(dd, 6 - (@@DATEFIRST + 5 + DATEPART(dw, @START_DATE)) % 7, @START_DATE) Sunday
SET @START_DATE = DATEADD(day, 1, @START_DATE)
SET @COUNTER = @COUNTER + 1
END
SELECT DISTINCT
START_WEEK,
END_WEEK,
CASE
WHEN START_WEEK = '2019-04-01' AND END_WEEK = '2019-04-07' THEN 'WEEK 1'
WHEN START_WEEK = '2019-04-08' AND END_WEEK = '2019-04-14' THEN 'WEEK 2'
WHEN START_WEEK = '2019-04-15' AND END_WEEK = '2019-04-21' THEN 'WEEK 3'
WHEN START_WEEK = '2019-04-22' AND END_WEEK = '2019-04-28' THEN 'WEEK 4'
WHEN START_WEEK = '2019-04-29' AND END_WEEK = '2019-05-05' THEN 'WEEK 5'
WHEN START_WEEK = '2019-05-06' AND END_WEEK = '2019-05-12' THEN 'WEEK 6'
WHEN START_WEEK = '2019-05-13' AND END_WEEK = '2019-05-19' THEN 'WEEK 7'
WHEN START_WEEK = '2019-05-20' AND END_WEEK = '2019-05-26' THEN 'WEEK 8'
WHEN START_WEEK = '2019-05-27' AND END_WEEK = '2019-06-02' THEN 'WEEK 9'
WHEN START_WEEK = '2019-06-03' AND END_WEEK = '2019-06-09' THEN 'WEEK 10'
WHEN START_WEEK = '2019-06-10' AND END_WEEK = '2019-06-16' THEN 'WEEK 11'
WHEN START_WEEK = '2019-06-17' AND END_WEEK = '2019-06-23' THEN 'WEEK 12'
WHEN START_WEEK = '2019-06-24' AND END_WEEK = '2019-06-30' THEN 'WEEK 13'
WHEN START_WEEK = '2019-07-01' AND END_WEEK = '2019-07-07' THEN 'WEEK 14'
WHEN START_WEEK = '2019-07-08' AND END_WEEK = '2019-07-14' THEN 'WEEK 15'
WHEN START_WEEK = '2019-07-15' AND END_WEEK = '2019-07-21' THEN 'WEEK 16'
WHEN START_WEEK = '2019-07-22' AND END_WEEK = '2019-07-28' THEN 'WEEK 17'
WHEN START_WEEK = '2019-07-29' AND END_WEEK = '2019-08-04' THEN 'WEEK 18'
WHEN START_WEEK = '2019-08-05' AND END_WEEK = '2019-08-11' THEN 'WEEK 19'
WHEN START_WEEK = '2019-08-12' AND END_WEEK = '2019-08-18' THEN 'WEEK 20'
WHEN START_WEEK = '2019-08-19' AND END_WEEK = '2019-08-25' THEN 'WEEK 21'
WHEN START_WEEK = '2019-08-26' AND END_WEEK = '2019-09-01' THEN 'WEEK 22'
WHEN START_WEEK = '2019-09-02' AND END_WEEK = '2019-09-08' THEN 'WEEK 23'
WHEN START_WEEK = '2019-09-09' AND END_WEEK = '2019-09-15' THEN 'WEEK 24'
WHEN START_WEEK = '2019-09-16' AND END_WEEK = '2019-09-22' THEN 'WEEK 25'
WHEN START_WEEK = '2019-09-23' AND END_WEEK = '2019-09-29' THEN 'WEEK 26'
WHEN START_WEEK = '2019-09-30' AND END_WEEK = '2019-10-06' THEN 'WEEK 27'
WHEN START_WEEK = '2019-10-07' AND END_WEEK = '2019-10-13' THEN 'WEEK 28'
WHEN START_WEEK = '2019-10-14' AND END_WEEK = '2019-10-20' THEN 'WEEK 29'
WHEN START_WEEK = '2019-10-21' AND END_WEEK = '2019-10-27' THEN 'WEEK 30'
WHEN START_WEEK = '2019-10-28' AND END_WEEK = '2019-11-03' THEN 'WEEK 31'
WHEN START_WEEK = '2019-11-04' AND END_WEEK = '2019-11-10' THEN 'WEEK 32'
WHEN START_WEEK = '2019-11-11' AND END_WEEK = '2019-11-17' THEN 'WEEK 33'
WHEN START_WEEK = '2019-11-18' AND END_WEEK = '2019-11-24' THEN 'WEEK 34'
WHEN START_WEEK = '2019-11-25' AND END_WEEK = '2019-12-01' THEN 'WEEK 35'
WHEN START_WEEK = '2019-12-02' AND END_WEEK = '2019-12-08' THEN 'WEEK 36'
WHEN START_WEEK = '2019-12-09' AND END_WEEK = '2019-12-15' THEN 'WEEK 37'
WHEN START_WEEK = '2019-12-16' AND END_WEEK = '2019-12-22' THEN 'WEEK 38'
WHEN START_WEEK = '2019-12-23' AND END_WEEK = '2019-12-29' THEN 'WEEK 39'
WHEN START_WEEK = '2019-12-30' AND END_WEEK = '2020-01-05' THEN 'WEEK 40'
WHEN START_WEEK = '2020-01-06' AND END_WEEK = '2020-01-12' THEN 'WEEK 41'
WHEN START_WEEK = '2020-01-13' AND END_WEEK = '2020-01-19' THEN 'WEEK 42'
WHEN START_WEEK = '2020-01-20' AND END_WEEK = '2020-01-26' THEN 'WEEK 43'
WHEN START_WEEK = '2020-01-27' AND END_WEEK = '2020-02-02' THEN 'WEEK 44'
WHEN START_WEEK = '2020-02-03' AND END_WEEK = '2020-02-09' THEN 'WEEK 45'
WHEN START_WEEK = '2020-02-10' AND END_WEEK = '2020-02-16' THEN 'WEEK 46'
WHEN START_WEEK = '2020-02-17' AND END_WEEK = '2020-02-23' THEN 'WEEK 47'
WHEN START_WEEK = '2020-02-24' AND END_WEEK = '2020-03-01' THEN 'WEEK 48'
WHEN START_WEEK = '2020-03-02' AND END_WEEK = '2020-03-08' THEN 'WEEK 49'
WHEN START_WEEK = '2020-03-09' AND END_WEEK = '2020-03-15' THEN 'WEEK 50'
WHEN START_WEEK = '2020-03-16' AND END_WEEK = '2020-03-22' THEN 'WEEK 51'
WHEN START_WEEK = '2020-03-23' AND END_WEEK = '2020-03-29' THEN 'WEEK 52'
END AS FY_WEEK
FROM @TABLE
ORDER BY START_WEEK,END_WEEK;
Моя проблема - столбец Fiscal Week (FY_WEEK), я не хочу жестко кодировать эти значения START_WEEK и END_WEEK.
Я ищу способ, которым я мог бы динамически заполнять свой столбец финансовой недели вместо значений жесткого кодирования.