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

Posted

技术标签:

【中文标题】如何使用 SQL Server 通过 ETL 存储过程提取数据【英文标题】:How to pull data with ETL stored procedure using SQL Server 【发布时间】:2018-12-06 07:48:06 【问题描述】:

在我的旧数据库中,有一个表 Album 存储有关 ID、AlbumName、Release_Date(例如 01/01/2017)等信息。

我想将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  

此代码不起作用,有什么解决方法的帮助吗?非常感谢!

【问题讨论】:

你的意思是拉 release_date ?我在您的程序中看不到 release_date。 我只是在其中粘贴了一张照片。我的意思是我想将 Album(Release_Date) DATETIME 分解为年、季度、月、周等。但是,我不知道如何从 Release_Date 列中提取所有数据并插入 DimDateAlbum。例如,第一个 2017-10-17 应该是:DateKey:20171017, Date Year: 2017, Quarter: 4, QuarterName: Forth, etc... 为什么要使用整数 [DateKey] 而您已经将 release_date 作为日期?你应该什么都不做,你的事实表已经可以在 Release_Date = [Date] 上加入时间维度 你的意思是我不用创建任何ETL存储过程?抱歉,我是 ETL 的新手,所以我很困惑,因为时间维度表中没有数据,所以我必须先从相册中提取数据.. 你不应该破坏任何东西。您创建日历,然后将您的表加入其中。而且在结果集中的每一行你都会看到年、季、日,这就是创造DateTime暗淡的感觉 【参考方案1】:

如果我理解正确,您想填充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

【讨论】:

以上是关于如何使用 SQL Server 通过 ETL 存储过程提取数据的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server 调查超时错误

试图通过SSIS教程。为SQL Server 2012创建一个简单的ETL包。

WAN 上 MySQL 到 SQL Server 的 ETL 机制

SQL Server 索引重新创建存储过程慢

从SQL Server到MySQL的ETL实现

从 SQL Server 2016 到 SQL Server Management Studio 2005 的 ETL