繁体   English   中英

使用 postgresql 设计一个缓慢变化的维度类型 2 脚本

[英]Designing a slowly changing dimension type 2 script with postgresql

假设我有以下目标表:

CREATE TABLE DimCustomer (
CustomerKey serial PRIMARY KEY,
    CustomerNum int NOT NULL,
    CustomerName varchar(25) NOT NULL,
    Planet varchar(25) NOT NULL,
    RowIsCurrent char(1) NOT NULL DEFAULT 'Y',
    RowStartDate date NOT NULL DEFAULT CURRENT_TIMESTAMP,
    RowEndDate date NOT NULL DEFAULT '12/31/9999'
);

INSERT INTO DimCustomer
(CustomerNum, CustomerName, Planet,  RowStartDate) 
VALUES (101,'Anakin Skywalker', 'Tatooine',   CURRENT_TIMESTAMP - INTERVAL '101 days'),
       (102,'Yoda', 'Coruscant',  CURRENT_TIMESTAMP - INTERVAL '100 days'),
       (103,'Obi-Wan Kenobi', 'Coruscant',  CURRENT_TIMESTAMP - INTERVAL '100 days')

我有以下临时表:

CREATE TABLE Staging_DimCustomer
(
    CustomerNum int NOT NULL,
    CustomerName varchar(25) NOT NULL,
    Planet varchar(25) NOT NULL,
    ChangeDate date NOT NULL DEFAULT CURRENT_TIMESTAMP,
    RankNo int NOT NULL DEFAULT 1
)
INSERT INTO Staging_DimCustomer(CustomerNum, CustomerName, Planet, ChangeDate)
VALUES
(103,'Ben Kenobi', 'Coruscant',   CURRENT_TIMESTAMP - INTERVAL '99 days')

在临时表中,看起来像'Obi-Wan Kenobi'customernum 103 )将他的名字更改为'Ben Kenobi' 我想创建一个脚本来实现 scd 类型 2 并产生以下结果(缓慢变化的维度类型 2):

在此处输入图片说明

以下是我的尝试:

INSERT INTO DimCustomer (
  CustomerNum, CustomerName, Planet, RowIsCurrent, RowStartDate, RowEndDate
  ) 
 select CustomerNum, CustomerName, Planet, 'Y', ChangeDate, '12/31/9999'
 from Staging_DimCustomer 

 ON CONFLICT (CustomerNum) and RowIsCurrent = 'Y'
  DO UPDATE SET
    CustomerName = EXCLUDED.CustomerName,
    Planet = EXCLUDED.Planet,
    RowIsCurrent = 'N',
    RowEndDate = EXCLUDED.ChangeDate

我不知道如何查找更改的值,更新现有行以rowiscurrent = 'Y'它,然后插入带有rowiscurrent = 'Y'标志的新行。 我正在尝试根据这篇 sql server 文章http://www.made2mentor.com/2013/08/how-to-load-slowly-changed-dimensions-using-t-sql-merge/为我的解决方案建模。

假设更改都在最新行上,那么您可以更新当前行,然后插入:

with u as (
      update dimCustomer c
          set RowIsCurrent = 'N',
              RowEndDate = sc.ChangeDate
      from Staging_DimCustomer sc
      where sc.CustomerNum = c.CustomerNum and
            c.RowIsCurrent = 'Y'
     )
insert into dimCustomer (CustomerNum, CustomerName, Planet, RowIsCurrent, RowStartDate, RowEndDate
                         ) 
     select CustomerNum, CustomerName, Planet, 'Y', ChangeDate, '9999-12-31'::date
     from Staging_DimCustomer sc;

这假设更改发生在最新的记录上。 实施历史性更改相当棘手,我猜这没有必要。

请注意,您可能需要额外检查插入的行实际上与当前行不同。

编辑:

如果要避免更改已存在的行,可以执行以下操作:

with sc as (
      select *
      from Staging_DimCustomer
      where not exists (select 1
                        from DimCustomer c
                        where c.CustomerNum = sc.CustomerNum and
                              c.CustomerName = sc.CustomerName and
                              . . .  -- whatever other columns you want to check
                      )
     ),
     u as (
      update dimCustomer c
          set RowIsCurrent = 'N',
              RowEndDate = sc.ChangeDate
      from sc
      where sc.CustomerNum = c.CustomerNum and
            c.RowIsCurrent = 'Y'
     )
insert into dimCustomer (CustomerNum, CustomerName, Planet, RowIsCurrent, RowStartDate, RowEndDate
                         ) 
     select CustomerNum, CustomerName, Planet, 'Y', ChangeDate, '9999-12-31'::date
     from sc;

我认为这应该可以正常工作,而不是更新或插入已经存在的记录:

with us as (
  update dimCustomer c
      set RowIsCurrent = 'N',
          RowEndDate = sc.ChangeDate
  from Staging_DimCustomer sc
  where sc.CustomerNum = c.CustomerNum and
        c.RowIsCurrent = 'Y' and 
        sc.customername <> c.customername
 ),
 u as (
 select stg.customernum,stg.customername,stg.planet ,stg.changedate from Staging_DimCustomer  stg
 Inner join  DimCustomer dim on dim.customernum=stg.customernum and dim.rowiscurrent='Y'
 and (dim.customername <> stg.customername
      or dim.planet <> stg.planet
      )
 UNION
    select stg.customernum,stg.customername,stg.planet ,stg.changedate from Staging_DimCustomer  stg
 where  stg.customernum not IN(select dim.customernum  from DimCustomer dim where dim.rowiscurrent='Y')
 )
insert into dimCustomer (CustomerNum, CustomerName, Planet, RowIsCurrent, RowStartDate, RowEndDate
                     ) 
select CustomerNum, CustomerName, Planet, 'Y', ChangeDate, '9999-12-31'::date
 from  u ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM