当前位置: 首页 > news >正文

使用Starrocks制作拉链表

5月1日向ods_order_info插入3条数据:

CREATE TABLE ods_order_info(`dt` string,`id` string COMMENT '订单编号',`total_amount` decimal(10,2) COMMENT '订单金额'
)
PRIMARY KEY(`dt`, `id`)
PARTITION BY (`dt`)
DISTRIBUTED BY HASH(`id`)
PROPERTIES (
"replication_num" = "1"
);insert into ods_order_info
select '2025-05-01'          as dt,9527                 as id,2000                 as total_amount
;insert into ods_order_info
select '2025-05-01'          as dt,9528                 as id,3000                 as total_amount
;insert into ods_order_info
select '2025-05-01'          as dt,9529                 as id,4000                 as total_amount
;

查询结果:

MySQL [tmp]> select * from ods_order_info;
+------------+------+--------------+
| dt         | id   | total_amount |
+------------+------+--------------+
| 2025-05-01 | 9529 |      4000.00 |
| 2025-05-01 | 9528 |      3000.00 |
| 2025-05-01 | 9527 |      2000.00 |
+------------+------+--------------+
3 rows in set (0.01 sec)

制作拉链表dwd_order_info_his:

drop table if exists dwd_order_info_his;
create table dwd_order_info_his( `end_date`  string COMMENT '有效结束日期',`id` string COMMENT '订单编号',`total_amount` decimal(10,2) COMMENT '订单金额', `start_date`  string COMMENT '有效开始日期',`record_status` string COMMENT '是否有效'
)
PRIMARY KEY(`end_date`, `id`)
PARTITION BY (`end_date`)
DISTRIBUTED BY HASH(`id`)
PROPERTIES (
"replication_num" = "1"
);

初始化拉链表:

insert overwrite dwd_order_info_his
select'9999-99-99',id,total_amount,'2025-05-01','active'
from ods_order_info di
where di.dt='2025-05-01';

查询结果:

MySQL [tmp]> select * from dwd_order_info_his;
+------------+------+--------------+------------+---------------+
| end_date   | id   | total_amount | start_date | record_status |
+------------+------+--------------+------------+---------------+
| 9999-99-99 | 9527 |      2000.00 | 2025-05-01 | active        |
| 9999-99-99 | 9529 |      4000.00 | 2025-05-01 | active        |
| 9999-99-99 | 9528 |      3000.00 | 2025-05-01 | active        |
+------------+------+--------------+------------+---------------+
3 rows in set (0.01 sec)

创建临时表用于导数据:

drop table if exists dwd_order_info_his_tmp;
create table dwd_order_info_his_tmp( `end_date`  string COMMENT '有效结束日期',`id` string COMMENT '订单编号',`total_amount` decimal(10,2) COMMENT '订单金额', `start_date`  string COMMENT '有效开始日期'
)
PRIMARY KEY(`end_date`, `id`)
PARTITION BY (`end_date`)
DISTRIBUTED BY HASH(`id`)
PROPERTIES (
"replication_num" = "1"
);

5月2日ODS表发生改变:

insert into ods_order_info
select '2025-05-02'          as dt,9527                 as id,2222                 as total_amount
;insert into ods_order_info
select '2025-05-02'          as dt,9540                 as id,7000                 as total_amount
;

导入数据:

insert overwrite dwd_order_info_his_tmp
select * from 
(
select '9999-99-99' end_date,id,total_amount,'2025-05-02' start_date
from ods_order_info where dt='2025-05-02'
union all 
select if(oi.id is null, oh.end_date, date_add(oi.dt, -1)) end_date,oh.id,oh.total_amount,oh.start_date
from dwd_order_info_his oh left join (select*from ods_order_infowhere dt='2025-05-02'
) oion oh.id=oi.id and oh.end_date='9999-99-99'  
)his 
order by his.id, start_date;insert overwrite dwd_order_info_his 
select 
end_date,
id,
total_amount,
start_date,
case when end_date = '9999-99-99' then 'active' else 'expire' end as record_status
from dwd_order_info_his_tmp;

查询结果:

MySQL [tmp]> select * from dwd_order_info_his where start_date = '2025-05-01' and record_status = 'active';
+------------+------+--------------+------------+---------------+
| end_date   | id   | total_amount | start_date | record_status |
+------------+------+--------------+------------+---------------+
| 9999-99-99 | 9529 |      4000.00 | 2025-05-01 | active        |
| 9999-99-99 | 9528 |      3000.00 | 2025-05-01 | active        |
+------------+------+--------------+------------+---------------+
2 rows in set (0.01 sec)MySQL [tmp]> select * from dwd_order_info_his where start_date = '2025-05-02' and record_status = 'active';
+------------+------+--------------+------------+---------------+
| end_date   | id   | total_amount | start_date | record_status |
+------------+------+--------------+------------+---------------+
| 9999-99-99 | 9527 |      2222.00 | 2025-05-02 | active        |
| 9999-99-99 | 9540 |      7000.00 | 2025-05-02 | active        |
+------------+------+--------------+------------+---------------+
2 rows in set (0.01 sec)

5月3日ODS层数据再次改变:

insert into ods_order_info
select '2025-05-03'          as dt,9528                 as id,3333                 as total_amount
;insert into ods_order_info
select '2025-05-03'          as dt,9541                 as id,8000                 as total_amount
;

导入数据:

insert overwrite dwd_order_info_his_tmp
select * from 
(
select '9999-99-99' end_date,id,total_amount,'2025-05-03' start_date
from ods_order_info where dt='2025-05-03'
union all 
select if(oi.id is null, oh.end_date, date_add(oi.dt, -1)) end_date,oh.id,oh.total_amount,oh.start_date
from dwd_order_info_his oh left join (select*from ods_order_infowhere dt='2025-05-03'
) oion oh.id=oi.id and oh.end_date='9999-99-99'  
)his 
order by his.id, start_date;

查询数据:

MySQL [tmp]> select * from dwd_order_info_his;
+---------------------+------+--------------+------------+---------------+
| end_date            | id   | total_amount | start_date | record_status |
+---------------------+------+--------------+------------+---------------+
| 9999-99-99          | 9529 |      4000.00 | 2025-05-01 | active        |
| 9999-99-99          | 9541 |      8000.00 | 2025-05-03 | active        |
| 2025-05-01 00:00:00 | 9527 |      2000.00 | 2025-05-01 | expire        |
| 9999-99-99          | 9528 |      3333.00 | 2025-05-03 | active        |
| 9999-99-99          | 9540 |      7000.00 | 2025-05-02 | active        |
| 9999-99-99          | 9527 |      2222.00 | 2025-05-02 | active        |
| 2025-05-02 00:00:00 | 9528 |      3000.00 | 2025-05-01 | expire        |
+---------------------+------+--------------+------------+---------------+
7 rows in set (0.01 sec)

MySQL [tmp]> select * from dwd_order_info_his where start_date = '2025-05-01' and record_status = 'active';
+------------+------+--------------+------------+---------------+
| end_date   | id   | total_amount | start_date | record_status |
+------------+------+--------------+------------+---------------+
| 9999-99-99 | 9529 |      4000.00 | 2025-05-01 | active        |
+------------+------+--------------+------------+---------------+
1 row in set (0.03 sec)MySQL [tmp]> select * from dwd_order_info_his where start_date = '2025-05-02' and record_status = 'active';
+------------+------+--------------+------------+---------------+
| end_date   | id   | total_amount | start_date | record_status |
+------------+------+--------------+------------+---------------+
| 9999-99-99 | 9540 |      7000.00 | 2025-05-02 | active        |
| 9999-99-99 | 9527 |      2222.00 | 2025-05-02 | active        |
+------------+------+--------------+------------+---------------+
2 rows in set (0.01 sec)MySQL [tmp]> select * from dwd_order_info_his where start_date = '2025-05-03' and record_status = 'active';
+------------+------+--------------+------------+---------------+
| end_date   | id   | total_amount | start_date | record_status |
+------------+------+--------------+------------+---------------+
| 9999-99-99 | 9541 |      8000.00 | 2025-05-03 | active        |
| 9999-99-99 | 9528 |      3333.00 | 2025-05-03 | active        |
+------------+------+--------------+------------+---------------+
2 rows in set (0.01 sec)

http://www.xdnf.cn/news/564877.html

相关文章:

  • Typescript总结篇——配置TS、基础知识(类型、接口、类型别名、泛型、extendsinfer关键字)
  • OOP和软件设计中的五大核心设计原则——SOLID原则
  • 8.2 线性变换的矩阵
  • Isaac Sim及Isaac Lab的安装与部署
  • 结构体对齐规则与优化
  • openlayer:07点击实现切换图层之addLayer
  • 学习笔记:黑马程序员JavaWeb开发教程(2025.4.8)
  • 数据集下载并保存本地进行加载
  • Python----循环神经网络(Word2Vec)
  • HTTP/HTTPS 协议浅解
  • Python高效网络爬虫开发指南
  • 年度工作计划总结述职报告PPT模版一组分享
  • docker上传镜像
  • 【springcloud核心技术站概述】
  • PHP伪随机数
  • 【TTS回顾】StyleTTS 深度剖析:TTS+风格迁移
  • day019-特殊符号、正则表达式与三剑客
  • 佰力博科技与您探讨压电材料的原理与压电效应的应用
  • ATT Global赞助非小号全球行,引领RWA创新浪潮
  • 发二区利器:CNN+LSTM时序预测
  • 什么是“架构孤岛”?如何识别与整合?为什么现代企业在追求敏捷开发的同时,反而更容易陷入架构孤岛陷阱?
  • Docker安装Fluentd采集中间件
  • 微步在线 模拟登录 某验4代滑块验证
  • 探索 Duix.Heygem:开源数字人的创新之旅
  • 数据结构篇--优先级队列排序--实验报告
  • jenkins凭据管理
  • STM32项目分享:智能家居(机智云)升级版
  • 【RabbitMQ】记录 InvalidDefinitionException: Java 8 date/time type
  • 高级认知型Agent
  • 轻量化MEC终端 特点