使用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.pswp.cn/web/80833.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/80833.shtml
英文地址,請注明出處:http://en.pswp.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)是數據分析和機器學習中至關重要的步驟,旨在將原始數據轉換為更高質量、更適合分析或建模的形式。由于真實世界的數據通常存在不完整、不一致、噪聲或冗余等問題,預處理可以…