日付のDimテーブルを作成して設定するコードは次のとおりです。お役に立てれば。
CREATE TABLE [dbo].[DimDate]
(
[DateKey] INT primary key,
[Date] DATETIME,
[DayofMonth] TINYINT, -- Field will hold day number of Month
[NameofMonth] VARCHAR(9),--January, February etc
[TransferWindow] VARCHAR(20), -- Summer & Winter
)
--Specify Start Date and End date here
--Value of Start Date Must be Less than Your End Date
DECLARE @StartDate DATETIME = '01/01/2015' --Starting value of Date Range
DECLARE @EndDate DATETIME = '12/31/2025' --End Value of Date Range
DECLARE @CurrentDate AS DATETIME = @StartDate
WHILE @CurrentDate < @EndDate
BEGIN
INSERT INTO [dbo].[DimDate]
SELECT
CONVERT (char(8),@CurrentDate,112) as DateKey,
@CurrentDate AS Date,
DATEPART(DD, @CurrentDate) AS [DayOfMonth],
DATENAME(MM, @CurrentDate) AS [MonthName],
CASE WHEN (MONTH(@CurrentDate) BETWEEN 6 AND 8) OR (MONTH(@CurrentDate) =9 AND DATEPART(DD, @CurrentDate)=1) THEN 'Summer'
WHEN MONTH(@CurrentDate) =1 THEN 'Winter'
ELSE ''
END AS [TransferWindow]
SET @CurrentDate = DATEADD(DD, 1, @CurrentDate)
END
SELECT * FROM [DimDate]
--DROP TABLE [DimDate]
ありがとうございました! – SMC