背景
在制造业数仓建设过程中,人员分析主题下生产人员基础信息以及历史信息相当重要,
因为一般会包含人员的【入转调离】,或者一些特殊的变动,比如公司工号编码格式变动等;
在此需求下,我们需要定时记录人员信息的变动,但如果每次都全量同步会极大增加存储成本,
故一般会使用拉链表的形式保存,达成记录历史数据同时降低存储成本;
本文将介绍【通过定时任务将每一天变动后人员基础信息最新版本同步到流水表,再通过流水表生成拉链表】的SQL实现过程;
原表数据结构
原表示例
本文示例将使用人员基础信息中常用的字段,其他字段也可灵活选取:
示例原表DDL
create table employee
(
id bigint not null primary key comment 'id',
num varchar(100) null comment '工号',
name varchar(100) null comment '姓名',
job varchar(100) null comment '岗位',
tel varchar(100) null comment '联系电话',
email varchar(100) null comment '邮箱地址',
status varchar(100) null comment '在职状态'
);
流水表
在原表基础上,流水表将在主键中添加 fn_date 字段作为原表数据变动的时间,或者记录同步到流水表的时间。
并在所有字段后面添加 描述字段,记录最新版本记录和前一版记录的变动内容;
流水表DDL
create table clip_dwh.employee_flow
(
fn_date varchar(12) not null comment '变动时间',
id bigint not null comment 'id',
num varchar(100) null comment '工号',
name varchar(100) null comment '姓名',
job varchar(100) null comment '岗位',
tel varchar(100)