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

使用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.lryc.cn/news/2383441.html

相关文章:

  • Oracle 11g 单实例使用+asm修改主机名导致ORA-29701 故障分析
  • Spring Boot接口通用返回值设计与实现最佳实践
  • DeepSeek 赋能军事:重塑现代战争形态的科技密码
  • day09-新热文章-实时计算
  • Elasticsearch面试题带答案
  • OpenCV CUDA模块图像过滤------用于创建一个最大值盒式滤波器(Max Box Filter)函数createBoxMaxFilter()
  • Redis数据库-消息队列
  • 【Docker】Docker -p 将容器内部的端口映射到宿主机的端口
  • 破解充电安全难题:智能终端的多重防护体系构建
  • apptrace 三大策略,助力电商 App 在 618 突围
  • SpringAI的使用
  • Core Web Vitals 全链路优化:从浏览器引擎到网络协议深度调优
  • SuperVINS:应对挑战性成像条件的实时视觉-惯性SLAM框架【全流程配置与测试!!!】【2025最新版!!!!】
  • Node-Red通过开疆智能Profinet转ModbusTCP采集西门子PLC数据配置案例
  • vscode连接WSL卡住
  • Redis面试题全面解析:从基础到底层实现
  • 【性能测试】jvm监控
  • Uniapp开发鸿蒙应用时如何运行和调试项目
  • QT+RSVisa控制LXI仪器
  • PHP8.0版本导出excel失败
  • GO语言学习(五)
  • js不同浏览器标签页、窗口或 iframe 之间可以相互通信
  • springboot3+vue3融合项目实战-大事件文章管理系统-文章分类也表查询(条件分页)
  • Canvas进阶篇:鼠标交互动画
  • Mac下载bilibili视频
  • Unity editor文件数UI(支持勾选框)
  • 【Node.js】Web开发框架
  • 使用Vite创建一个动态网页的前端项目
  • 系统架构设计师案例分析题——web篇
  • MySQL--day5--多表查询