0

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

样本数据如下:

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

有任何想法吗

4

2 回答 2

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
于 2012-02-14T19:42:16.643 回答
0

我设法使用递归 cte 获得了所需的结果。UPI 是自然密钥。然而,事件实际上来自一个单独的文件,该文件被旋转并连接到维度上。(BLI003) 有 2 行,因为它有两个事件。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

于 2012-02-14T23:22:09.350 回答