使用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.mzph.cn/web/80833.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux下Docker使用阿里云镜像加速器

在中国大陆环境中配置 Docker 使用阿里云镜像加速器,并确保通过 Clash 代理访问 Docker Hub 我这里用的Debian12。 步骤 1:获取阿里云镜像加速器地址 登录阿里云容器镜像服务控制台:(qinyang.wang) 网址:阿里云登录 - 欢迎登录阿…

Electron 后台常驻服务实现(托盘 + 开机自启)

基于 electron-vite-vue 项目结构 本篇将详细介绍如何为 Electron 应用实现后台常驻运行,包括: ✅ 创建系统托盘图标(Tray)✅ 支持点击托盘菜单控制窗口显示/退出✅ 实现开机自启功能(Auto Launch) &#…

opencv的直方图

理解并运用 OpenCV 中的图像直方图 📊🖼️ 图像直方图是计算机视觉和图像处理中一种基本且强大的工具,它提供了图像像素强度分布的图形化表示。OpenCV 作为一个全面的计算机视觉库,内置了计算和可视化直方图的强大功能。本文将深…

Linux 内核探秘:从零构建 GPIO 设备驱动程序实战指南

在嵌入式系统开发领域,GPIO(通用输入 / 输出)作为硬件与软件交互的桥梁,是实现设备控制与数据采集的基础。编写高效、稳定的 GPIO 设备驱动程序,对于发挥硬件性能至关重要。本文将深入剖析 Linux 内核中 GPIO 驱动开发…

嵌入式单片机中STM32F1演示寄存器控制方法

该文以STM32F103C8T6为示例,演示如何使用操作寄存器的方法点亮(关闭LED灯),并讲解了如何调试,以及使用宏定义。 第一:操作寄存器点亮LED灯。 (1)首先我们的目的是操作板子上的LED2灯,对其实现点亮和关闭操作。打开STM32F103C8T6的原理图,找到LED2的位置。 可以看到…

牛客网 NC16407 题解:托米航空公司的座位安排问题

牛客网 NC16407 题解:托米航空公司的座位安排问题 题目分析 解题思路 本题可以采用深度优先搜索(DFS)来解决: 从左上角开始,按行优先顺序遍历每个座位对于每个座位,有两种选择: 选择该座位(如果满足条件…

智慧展馆数字孪生平台

2022年进博会上,国家会展中心凭借“数字孪生机器人调度平台”惊艳全球,实现人机协同、虚实联动的智慧运营;2023年天府农博园通过“BIMIoT”技术,贯穿展馆全生命周期管理,成为农业会展的数字化标杆。这些案例背后&#…

胡说八道1---豆包问答总结

用户提问 1 指令:25 - - [21/May/2025:01:35:45 0000] “POST /prod-api/system/base/getList HTTP/1.1” 405 559 “http://192.168.1.109:16380/login” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 …

C# AOP编程

AOP(面向切片编程的概念我这里就不介绍了,这里先介绍一下C#中的AOP编程框架。 1.AOP的分类 .net下支持AOP的框架很多,搜了一下有:PostSharp、AspectInjector、Fody 、Castle Windsor、Spring.NET、Ninject、Unity等,实现的方式主要…

linux编译安装srs

下载编译运行 git clone https://github.com/ossrs/srs.git cd srs/trunk ./configure --h265on make需要安装 yum install -y patch yum install -y unzip yum install -y tcl编译完成后即可启动SRS # 启动 ./objs/srs -c conf/srs.conf # 查看日志 tail -n 30 -f ./objs/s…

EtherNet/IP机柜内解决方案在医疗控制中心智能化的应用潜能和方向分析

引言 在数智化转型浪潮席卷各行各业的今天,医疗领域同样面临着提升运营效率、改善患者体验和加强系统可靠性的多重挑战。Rockwell Automation于2025年5月20日推出的EtherNet/IP机柜内解决方案,为医疗中心的自动化升级提供了一种创新路径。本报告将深入分析这一解决方案的核心…

大模型下载到本地

一、huggingface 方式一 from huggingface_hub import snapshot_downloadlocal_dir "./origin" model_name "Qwen/Qwen2.5-1.5B"# snapshot_download(repo_idmodel_name, cache_dirlocal_dir) model_dir snapshot_download(model_name,cache_dirlocal…

【C++】vector容器实现

目录 一、vector的成员变量 二、vector手动实现 (1)构造 (2)析构 (3)尾插 (4)扩容 (5)[ ]运算符重载 5.1 迭代器的实现: (6&…

PostgreSQL日常维护

目录 一、PostgreSQL 概述 二、基本使用 (一)登录数据库 (二)数据库操作 1. 列出数据库 2. 创建数据库 3. 删除数据库 4. 切换数据库 5. 查看数据库大小 (三)数据表操作 1. 列出表 2. 创建表 …

广东省省考备考(第十六天5.21)—言语:语句排序题(听课后强化)

错题 解析 对比选项,确定首句。①句介绍目前人类可以利用一些技术手段进入元宇宙,凭借网络重新定义自己,体验一种全新的生活,②句介绍对于多数人来说,首先要弄清楚什么是元宇宙,③句介绍元宇宙是指超越现实…

高并发架构设计之限流

一、引言 再强大的系统,也怕流量短事件内集中爆发,就像银行怕挤兑一样,所以,高并发另一个必不可少的模块就是限流。限流是一种通过控制请求的速率或数量来保护系统免受过载的技术。流控的精髓是限制单位时间内的请求量&#xff0…

视频监控联网系统GB28181协议中设备控制流程详解

文章目录 9.3 设备控制9.3.1 基本要求9.3.2 命令流程9.3.2.1 无应答命令流程 9.3.3 协议接口9.3.3.1 请求命令9.3.3.2 应答命令 智联视频超融合平台介绍 9.3 设备控制 9.3.1 基本要求 控制满足以下基本要求: a) 源设备向目标设备发送控制命令,控制命令…

深入剖析原型模式:原理、实现与应用实践

在软件开发的世界里,设计模式如同建筑师手中的蓝图,为复杂系统的构建提供了行之有效的解决方案。其中,原型模式(Prototype Pattern)作为创建型设计模式的重要一员,以其独特的对象创建方式,在提高代码复用性、增强系统灵活性等方面发挥着关键作用。本文将深入剖析原型模式…

图绘Linux:基础指令脉络阁

目录 Linux命令行介绍 目录操作 ls 目录所含文件信息 ls 常用选项 pwd 在那个目录下 cd 进入目录 mkdir 创建目录 文件操作 touch 创建普通文件 echo向文件写入 cat 输出文件内容 cp 拷贝文件/目录 mv剪切重命名 rm 删除文件/目录 查找 * 匹配符 man 查找指令 …

数据分析 —— 数据预处理

一、什么是数据预处理 数据预处理(Data Preprocessing)是数据分析和机器学习中至关重要的步骤,旨在将原始数据转换为更高质量、更适合分析或建模的形式。由于真实世界的数据通常存在不完整、不一致、噪声或冗余等问题,预处理可以…