当前位置:网站首页>【mysql】多指标历史累计去重问题
【mysql】多指标历史累计去重问题
2022-07-29 00:01:00 【檀越剑指大厂】
多指标累计去重问题
1.数据准备
create table db.sku_store_info as
select '1' as product_key, '2018-01-01' as time_id, '001' as store_key
UNION ALL
select '1' as product_key, '2018-01-01' as time_id, '002' as store_key
UNION ALL
select '1' as product_key, '2018-01-01' as time_id, '001' as store_key
UNION ALL
select '1' as product_key, '2018-01-02' as time_id, '004' as store_key
UNION ALL
select '1' as product_key, '2018-01-02' as time_id, '002' as store_key
UNION ALL
select '1' as product_key, '2018-01-02' as time_id, '003' as store_key
UNION ALL
select '1' as product_key, '2018-01-02' as time_id, '002' as store_key
UNION ALL
select '1' as product_key, '2018-01-02' as time_id, '004' as store_key
UNION ALL
select '1' as product_key, '2018-01-03' as time_id, '005' as store_key
UNION ALL
select '1' as product_key, '2018-01-03' as time_id, '003' as store_key
UNION ALL
select '1' as product_key, '2018-01-03' as time_id, '001' as store_key
UNION ALL
select '1' as product_key, '2018-01-03' as time_id, '005' as store_key
UNION ALL
select '1' as product_key, '2018-02-03' as time_id, '005' as store_key
UNION ALL
select '1' as product_key, '2018-02-04' as time_id, '005' as store_key
UNION ALL
select '1' as product_key, '2018-02-05' as time_id, '009' as store_key
UNION ALL
select '2' as product_key, '2018-01-03' as time_id, '005' as store_key
UNION ALL
select '2' as product_key, '2018-02-03' as time_id, '001' as store_key
UNION ALL
select '2' as product_key, '2019-01-03' as time_id, '006' as store_key
UNION ALL
select '2' as product_key, '2020-01-03' as time_id, '005' as store_key
;
2.编写sql
I.当天累计
with tmp_table as (
select product_key as product_key
, time_id as date_id
, store_key as store_key
from ads_sense_rep.sku_store_info
group by product_key, time_id, store_key
)
select product_key as product_key
, date_id as date_id
, store_key as store_key
, count(distinct store_key) over(partition by product_key, date_id) as user_cnt_act
from tmp_table
;

II.历史累计
with tmp_table as (
select product_key as product_key
, time_id as date_id
, store_key as store_key
from ads_sense_rep.sku_store_info
group by product_key, time_id, store_key
)
, tmp_total_table as (
select product_key as product_key
, date_id as date_id
, store_key as store_key
, count(distinct store_key) over(partition by product_key,date_id) as user_cnt_act
, count(distinct store_key) over(partition by product_key order by date_id asc rows between unbounded preceding AND current row) as tmp_day_user_cnt_act_total
from tmp_table
)
select product_key as product_key
, date_id as date_id
, max(user_cnt_act) as user_cnt_act
, max(tmp_day_user_cnt_act_total) as user_cnt_act_total
from tmp_total_table
group by product_key, date_id
;

边栏推荐
- Brushless DC motor controller (how much is the hall charge for changing the motor)
- 如何执行建设项目的时间影响分析?
- Charles -- 从0-1教你如何使用抓包工具
- 时序预测 | MATLAB实现TCN时间卷积神经网络的时间序列预测
- Necessary interview skills for Android (including interview questions and learning materials)
- SystemVerilog-连接和复制运算符
- Inftnews | yuanuniverse shopping experience will become a powerful tool to attract consumers
- 🧐 table1 | 一秒搞定你的三线表
- [unity] configure unity edit C as vscode
- Hilbert 变换与瞬时频率
猜你喜欢

How to carry out engineering implementation of DDD Domain Driven Design

Visual full link log tracking

MySQL stored procedure realizes the creation of a table (copy the structure of the original table and create a new table)

Univariate function integration 1__ Indefinite integral

大页内存原理及使用设置

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(5)任务书

可视化全链路日志追踪

🧐 Table1 | finish your third line watch in one second

Cookie和Session

Daniel guild Games: summary and future outlook of this year
随机推荐
How to create a custom 404 error page in WordPress
Connect with Alipay payment
[Commons lang3 topic] 005- objectutils topic
Y80. Chapter 4 Prometheus big factory monitoring system and practice -- Kube state metrics component introduction and monitoring extension (XI)
进程和线程知识点总结 2
[raspberry pie] how does the windows computer connect with raspberry pie
Date conversion EEE MMM DD hh:mm:ss zzz YYYY
Wechat campus bathroom reservation applet graduation design finished product (8) graduation design thesis template
【Leetcode-滑动窗口问题】
Deep learning | matlab implementation of TCN time convolution neural network spatialdropoutlayer parameter description
Hilbert transform and instantaneous frequency
Daniel guild Games: summary and future outlook of this year
Thread lock and its ascending and descending levels
【AD学习】本次海上航行器大赛画pcb图的历程
[target detection] Introduction to yolor theory + practical test visdrone data set
返回*this的成员函数
ThinkPHP高仿蓝奏云网盘系统程序
【Jenkins笔记】入门,自由空间;持续集成企业微信;allure报告,持续集成电子邮件通知;构建定时任务
Necessary interview skills for Android (including interview questions and learning materials)
mysql存储过程 实现创建一张表(复制原表的结构新建的表)