あなたの質問を理解しようとしました。 次のクエリを試してみてください。しかし、ここで再帰的な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 ...これをあなたのサンプルコードで説明してください。提供されていることははっきりしていません。 2015年1月12日以降にさらにデータが必要ですか? – Zorkolot