如何使用SQL Server通过ETL存储过程提取数据

时间:2018-12-06 07:48:06

标签: sql-server stored-procedures etl data-warehouse snowflake-datawarehouse

在我的旧数据库中,有一个表Album,其中存储了有关ID,专辑名称,发行日期(例如,2017年1月1日)等信息。

enter image description here

我想进一步将Release_Date分解成一个时间维度表,所以我创建了一个DimDateAlbum表。

这是我创建的时间维度表。

CREATE TABLE [DimDateAlbum]
(
    [DateKey] INT PRIMARY KEY, 
    [Date] DATETIME NOT NULL,
    [Year] INT NOT NULL,
    [Quarter] TINYINT NOT NULL, 
    [QuarterName] VARCHAR(6) NOT NULL, -- January to March: First, April to 
        June: Second etc
    [Month] TINYINT NOT NULL,
    [MonthName] VARCHAR(9) NOT NULL, -- January, February etc
    [Day] TINYINT NOT NULL, -- Field holds day number of Month
    [DayofWeek] TINYINT NOT NULL, 
    [WeekName] VARCHAR(9) NOT NULL, -- Field displays 1: Monday, 2: Tuesday etc 
)  

如下所述:我可以将Release_Date插入[DateKey]到时间维度表中,但是,如何进一步将日期细分为年,季度,天等?

INSERT INTO DimDateAlbum
    SELECT 
        a.Release_Date AS [DateKey],
        CONVERT (char(8), a.Release_Date, 112) AS [DateKey],
        a.Release_Date AS [Date],
        DATEPART(YEAR, a.Release_Date) AS [Year], -- calendar year
        DATEPART(QQ, a.Release_Date) AS [Quarter], -- calendar quarter
        CASE (qq, a.Release_Date) 
           WHEN 1 THEN 'First' 
           WHEN 2 THEN 'Second' 
           WHEN 3 THEN 'Third' 
           WHEN 4 THEN 'Fourth' 
        END AS [QuarterName], 
        DATEPART(MONTH, a.Release_Date) AS [Month], -- month number of the year
        DATENAME(MM, a.Release_Date) AS [MonthName], -- month name
        DATEPART(DAY, a.Release_Date) AS [Day],  -- day number of the month
        DATEPART(DW, a.Release_Date) AS [DayofWeek], -- day number of week 
        CASE datepart(DW, a.Release_Date)  
           WHEN 1 THEN 'Monday' 
           WHEN 2 THEN 'Tuesday' 
           WHEN 3 THEN 'Wednesday' 
           WHEN 4 THEN 'Thursday' 
           WHEN 5 THEN 'Friday' 
           WHEN 6 THEN 'Saturday' 
           WHEN 7 THEN 'Sunday'
        END AS [WeekName]
    FROM 
        dbo.Album AS a  

此代码不起作用,如何解决它有帮助吗?非常感谢!

1 个答案:

答案 0 :(得分:2)

如果我对您的理解正确,那么您想填充DimDateAlbum表。我对您的表做了一些编辑(添加了身份约束,以避免手动编写此字段),现在看起来像这样:

CREATE TABLE [DimDateAlbum]
(
    [DateKey] INT IDENTITY CONSTRAINT PK_DimDateAlbum_ID PRIMARY KEY, 
    [Date] DATETIME NOT NULL,
    [Year] INT NOT NULL,
    [Quarter] TINYINT NOT NULL, 
    [QuarterName] VARCHAR(50) NOT NULL, -- January to March: First, April to 
    [Month] TINYINT NOT NULL,
    [MonthName] VARCHAR(9) NOT NULL, -- January, February etc
    [Day] TINYINT NOT NULL, -- Field holds day number of Month
    [DayofWeek] TINYINT NOT NULL, 
    [WeekName] VARCHAR(50) NOT NULL, -- Field displays 1: Monday, 2: Tuesday etc 
) 

现在您可以插入数据了。我添加了一个测试变量以插入一行,但是它可以用于从表中插入:

INSERT INTO dbo.DimDateAlbum
(   
    DateKey, 
    Date,
    Year,
    Quarter,
    QuarterName,
    Month,
    MonthName,
    Day,
    DayofWeek,
    WeekName
)
SELECT     
         CAST(a.Release_Date AS DATETIME)
       , YEAR(CAST(a.Release_Date AS DATETIME)) --        
       ,  DATEPART(QUARTER, CAST(a.Release_Date AS DATETIME)) -- Quarter
       , CASE -- Quarter Name
            WHEN DATEPART(QUARTER, CAST(a.Release_Date AS DATETIME)) = 1 THEN 'January to March' -- Quarter Name
            WHEN DATEPART(QUARTER, CAST(a.Release_Date AS DATETIME)) = 2 THEN 'April to June' -- Quarter Name
            WHEN DATEPART(QUARTER, CAST(a.Release_Date AS DATETIME)) = 3 THEN 'July to September' -- Quarter Name
            WHEN DATEPART(QUARTER, CAST(a.Release_Date AS DATETIME)) = 4 THEN 'October to December' -- Quarter Name
        END
        , MONTH(CAST(a.Release_Date AS DATETIME)) -- Month number
        , DATENAME(MONTH, DATEADD( MONTH, MONTH(CAST(a.Release_Date AS DATETIME)), 0) - 1) -- Month name        
        , DAY(CAST(a.Release_Date AS DATETIME)) -- 6
        , DATEPART(dw, CAST(a.Release_Date AS DATETIME)) -- 5
        , DATENAME(dw, CAST(a.Release_Date AS DATETIME)) -- Thursday
FROM Album a

工作示例:

DECLARE @FooDate VARCHAR(30) = '2018-12-06 12:10:51.727'
INSERT INTO dbo.DimDateAlbum
(   
    DateKey, 
    Date,
    Year,
    Quarter,
    QuarterName,
    Month,
    MonthName,
    Day,
    DayofWeek,
    WeekName
)
SELECT     
         CAST(@FooDate AS DATETIME)
       , YEAR(CAST(@FooDate AS DATETIME)) --        
       ,  DATEPART(QUARTER, CAST(@FooDate AS DATETIME)) -- Quarter
       , CASE -- Quarter Name
            WHEN DATEPART(QUARTER, CAST(@FooDate AS DATETIME)) = 1 THEN 'January to March' -- Quarter Name
            WHEN DATEPART(QUARTER, CAST(@FooDate AS DATETIME)) = 2 THEN 'April to June' -- Quarter Name
            WHEN DATEPART(QUARTER, CAST(@FooDate AS DATETIME)) = 3 THEN 'July to September' -- Quarter Name
            WHEN DATEPART(QUARTER, CAST(@FooDate AS DATETIME)) = 4 THEN 'October to December' -- Quarter Name
        END
        , MONTH(CAST(@FooDate AS DATETIME)) -- Month number
        , DATENAME(MONTH, DATEADD( MONTH, MONTH(CAST(@FooDate AS DATETIME)), 0) - 1) -- Month name
        , DAY(CAST(@FooDate AS DATETIME)) -- 6
        , DATEPART(dw, CAST(@FooDate AS DATETIME)) -- 5
        , DATENAME(dw, CAST(@FooDate AS DATETIME)) -- Thursday