귀하의 질문을 이해하려고 시도했습니다. 다음 쿼리를 사용하면 도움이 될 것입니다. 그러나 여기에 재귀 CTE를 사용했기 때문에 SQLServer 2005에서 작동하지 않을 수도 있습니다.
DECLARE @ToDate date
SET @ToDate='20171201' -- we'll calculate new rows until this date
;WITH monthCTE AS(
-- get the last row from your table
SELECT [Week Num],[Record Date],[Fiscal Year],[Fiscal Month],[Fiscal Week],[Customer #],[Group Description]
FROM
(
SELECT TOP 1 *
FROM [Your data]
ORDER BY [Week Num] DESC
) q
UNION ALL
-- calculate the next row
SELECT
[Week Num]+DATEDIFF(WEEK,[Record Date],[New Record Date]),
[New Record Date],
YEAR([New Record Date]),
MONTH([New Record Date]),
CASE
WHEN MONTH([New Record Date])=1 THEN 1 -- return 1 if it is the first month in the year
ELSE [Fiscal Week]+DATEDIFF(WEEK,[Record Date],[New Record Date])
END,
[Customer #],
[Group Description]
FROM
(
SELECT
[Week Num],[Record Date],[Fiscal Year],[Fiscal Month],[Fiscal Week],[Customer #],[Group Description],
DATEADD(MONTH,1,[Record Date]) [New Record Date] -- add one month to the previous date
FROM monthCTE
WHERE [Record Date]<@ToDate
) q
)
SELECT [Week Num],[Record Date],[Fiscal Year],[Fiscal Month],[Fiscal Week],[Customer #],[Group Description]
FROM [Your data]
UNION -- this operation also exclude duplicate for the last row
SELECT [Week Num],[Record Date],[Fiscal Year],[Fiscal Month],[Fiscal Week],[Customer #],[Group Description]
FROM monthCTE
ORDER BY [Week Num]
나는 당신을 올바르게 이해하고 올바르게 작동하면 모든 표현식의 정확성을 검사해야합니다.
SQL Server 2005 – ReportWarrior
응? 나는 당신이 여기서하려고하는 것을 이해하기조차 할 수 없다. –
"나는 달 점프 데이터가있는이 문제가 있습니다." Ok ... 예제 코드에서이를 설명합니다. 제공되는 것은 명확하지 않습니다. 2011 년 1 월 12 일 이후에 더 많은 데이터가 있어야합니까? – Zorkolot