SCD类型2的T-SQL语法

时间:2012-02-13 23:52:55

标签: sql-server-2005 tsql data-warehouse scd2

我目前正在尝试完成将scd类型2数据加载到维度的查询的最后部分。 根据下面提供的数据,我想生成一个可以插入到维度中的输出 使旧记录到期并跟踪历史记录等。数据是我拥有属性已更改的最新记录。更改的值可以在Lookup列中找到,也可以在dateOfchange中找到,即更改发生的日期。这个dateOfchange显然应该成为最新记录的有效日期。

样本数据如下:

CREATE TABLE #tstDimPortfolio
(
[ID][INT] IDENTITY (1,1) NOT NULL,
[UPI] [varchar](20) NOT NULL,
[MF_CODE] [varchar](10) NULL,
[BH_Code] [varchar](10) NULL,
[CR_Code] [varchar](10) NULL,
[ValidFrom][varchar](10) NOT NULL,
[ValidTo][varchar](10) NULL,
[IsCurrent] [CHAR] (1) NULL,
[DateofChange] [varchar](10) NULL,
[LookupMF_CODE] [varchar](10) NULL,
[LookupBH_Code] [varchar](10) NULL,
[LookupCR_Code] [varchar](10) NULL,
) 

INSERT INTO #tstDimPortfolio
SELECT 'B06531','B06531','','B06531','20111230',NULL,'Y','20120101','','B06531', ''
UNION ALL
SELECT 'BLI003','','BLI003','BBL_WORLD','20111230',NULL,'Y','20120102','BLI004','',    ''UNION ALL 
SELECT 'BLI003','','BLI003','BBL_WORLD','20111230',NULL,'Y','20120103','BLI005','',    ''UNION ALL 
SELECT 'BLI027','BLI027','L147','BBL_GBN','20111230',NULL,'Y','20120104','','L146', ''

使用此数据,新输出应为

UPI MF_CODE   BH_Code  CR_Code   ValidFrom  ValidTo      IsCurrent
_______________________________________________________________________
B06531  B06531  B06531               20111230   20120101        N
B06531  B06531  B06531  B06531       20120101   NULL            Y
BLI003  BLI003  BBL_WORLD            20111230   20120102        N
BLI003  BLI004  BLI003  BBL_WORLD    20120102   20120103        N
BLI003  BLI005  BLI003  BBL_WORLD    20120103   NULL            Y
BLI027  BLI027  L147    BBL_GBN      20111230   20120104        N
BLI027  BLI027  L146    BBL_GBN      20120104   NULL            Y

任何想法

2 个答案:

答案 0 :(得分:0)

我很担心这张桌子上的自然键是什么。是“UPI?”如果是这样,你有两个具有相同UPI的记录(BLI003),但它们都是活动的,绝不应该是这种情况。

无论如何,假设它是UPI。如果我们想要更新B06531,请将您的更新放入表格中:

CREATE TABLE #tstDimPortfolioUpdates
(
[ID][INT] IDENTITY (1,1) NOT NULL,
[UPI] [varchar](20) NOT NULL,
[MF_CODE] [varchar](10) NULL,
[BH_Code] [varchar](10) NULL,
[CR_Code] [varchar](10) NULL,
[ValidFrom][varchar](20) NOT NULL,
[ValidTo][varchar](15) NULL,
[IsCurrent] [CHAR] (1) NULL,
[DateofChange] [varchar](10) NULL,
[LookupMF_CODE] [varchar](10) NULL,
[LookupBH_Code] [varchar](10) NULL,
[LookupCR_Code] [varchar](10) NULL,
) 


INSERT INTO #tstDimPortfolioUpdates
SELECT 'B06531','B06531','B06531','B06531','20120102',NULL,'Y','20120102','','B06531', '' 

通过更改ValidTo,IsCurrent和DateOfChange值来设置任何不再有效的记录

UPDATE dp
SET ValidTo = '20120101', IsCurrent = 'N', DateOfChange = '20120101'
FROM #tstDimPortfolio dp
INNER JOIN #tstDimPortfolioUpdates up ON dp.UPI = up.UPI
AND dp.IsCurrent = 'Y'

插入新记录

INSERT INTO #tstDimPortfolio (UPI, MF_Code, BH_Code, CR_Code, ValidFrom, ValidTo
    ,IsCurrent, DateOfChange, LookupMF_Code, LookupBH_Code, LookupCR_Code)
SELECT UPI, MF_Code, BH_Code, CR_Code, ValidFrom, ValidTo
    ,IsCurrent, DateOfChange, LookupMF_Code, LookupBH_Code, LookupCR_Code
FROM #tstDimPortfolioUpdates

仔细检查更新是否正确

SELECT * FROM #tstDimPortfolio
ORDER BY UPI

答案 1 :(得分:0)

我设法使用递归cte获得所需的结果。 UPI是自然的关键。但是,事件实际上来自一个单独的文件,该文件被旋转并连接到维度。 (BLI003)有两行,因为它有两个事件。 sql如下:

 CREATE TABLE #tstDimPortfolio
 (
[ID][INT] IDENTITY (1,1) NOT NULL,
[UPI] [varchar](20) NOT NULL,
[MF_CODE] [varchar](10) NULL,
[BH_Code] [varchar](10) NULL,
[CR_Code] [varchar](10) NULL,
[ValidFrom][varchar](10) NOT NULL,
[ValidTo][varchar](10) NULL,
[IsCurrent] [CHAR] (1) NULL,
[DateofChange] [varchar](10) NULL,
[LookupMF_CODE] [varchar](10) NULL,
[LookupBH_Code] [varchar](10) NULL,
[LookupCR_Code] [varchar](10) NULL,


) 

INSERT INTO #tstDimPortfolio
SELECT 'B06531','B06531','','B06531','20111230',NULL,'Y','20120101','','B06531', ''UNION ALL
SELECT 'BLI003','','BLI003','BBL_WORLD','20111230',NULL,'Y','20120102','BLI004','', ''UNION ALL 
SELECT 'BLI003','','BLI003','BBL_WORLD','20111230',NULL,'Y','20120103','','BLI005', ''UNION ALL 
SELECT 'BLI027','BLI027','L147','BBL_GBN','20111230',NULL,'Y','20120104','','L146', ''





SELECT  ROW_NUMBER() OVER (PARTITION BY UPI ORDER BY UPI, DateofChange) Sequence,
    UPI, 
    CASE WHEN LookupMF_CODE <> '' THEN LookupMF_CODE ELSE MF_CODE END MF_CODE, 
    CASE WHEN LookupMF_CODE <> '' THEN LookupBH_Code ELSE BH_Code END BH_Code, 
    CASE WHEN LookupCR_Code <> '' THEN LookupCR_Code ELSE CR_Code  END CR_Code, 
    ValidFrom, ValidTo, IsCurrent, DateofChange,
    LookupMF_CODE,
    LookupBH_CODE,
    LookupCR_CODE
INTO #Dimension_Table
FROM #tstDimPortfolio


;WITH AddedDim AS
(SELECT * FROM 

(
SELECT 
ROW_NUMBER() OVER (PARTITION BY UPI ORDER BY UPI, DateofChange) Sequence,
UPI,
CASE WHEN LookupMF_CODE <> '' THEN LookupMF_CODE ELSE MF_CODE END MF_CODE, 
CASE WHEN LookupBH_CODE <> '' THEN LookupBH_Code ELSE BH_Code END BH_Code, 
CASE WHEN LookupCR_Code <> '' THEN LookupCR_Code ELSE CR_Code  END CR_Code, 
DateofChange AS ValidFrom, 
ValidTo, 
IsCurrent 
FROM #tstDimPortfolio
)A
WHERE SEQUENCE = 1

UNION ALL
SELECT  
DT.Sequence
, DT.UPI
,CASE WHEN DT.LookupMF_CODE <> '' THEN DT.LookupMF_CODE ELSE DM.MF_CODE END MF_CODE
,CASE WHEN DT.LookupBH_CODE <> '' THEN DT.LookupBH_Code ELSE DM.BH_Code END BH_Code 
,CASE WHEN DT.LookupCR_Code <> '' THEN DT.LookupCR_Code ELSE DM.CR_Code  END CR_Code
,DT.DateofChange AS ValidFrom 
,DT.ValidTo 
,DT.IsCurrent 
FROM #Dimension_Table dt inner join
AddedDim dm
on DT.UPI = DM.UPI
AND dt.Sequence = DM.Sequence + 1 
)

SELECT *    INTO #temp    FROM AddedDim