当前位置:网站首页>【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
;

边栏推荐
- 双链表的定义 ~
- 深度学习 | MATLAB实现TCN时间卷积神经网络spatialDropoutLayer参数描述
- 【commons-lang3专题】003- RandomStringUtils 专题
- (perfect solution) why is the effect of using train mode on the train/val/test dataset good, but it is all very poor in Eval mode
- Summary of process and thread knowledge points 1
- Charles -- 从0-1教你如何使用抓包工具
- This article enables you to understand the underlying principle of MySQL- Internal structure, index, lock, cluster
- Brushless DC motor controller (how much is the hall charge for changing the motor)
- Cookie和Session
- SystemVerilog-连接和复制运算符
猜你喜欢

ACM SIGIR 2022 | interpretation of selected papers of meituan technical team

In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
![[web development] basic knowledge of flask framework](/img/79/5ece84552c82e98f5e15fac1ee3335.png)
[web development] basic knowledge of flask framework

Necessary interview skills for Android (including interview questions and learning materials)

Charles -- teach you how to use the packet capturing tool from 0-1

What opportunities does the London gold real-time market bring?

Method of converting inline elements to block elements

教你一文解决 js 数字精度丢失问题

散列表 ~

The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
随机推荐
ACM SIGIR 2022 | interpretation of selected papers of meituan technical team
面试突击69:TCP 可靠吗?为什么?
一文让你搞懂MYSQL底层原理。-内部结构、索引、锁、集群
Day2:三种语言暴刷牛客130题
ACM SIGIR 2022 | 美团技术团队精选论文解读
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(6)开题答辩PPT
电子招标初学者指南
Charles -- 从0-1教你如何使用抓包工具
[notes for question brushing] delete continuous nodes with a total value of zero from the linked list
Summary of process and thread knowledge points 1
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
[Commons lang3 topic] 005- objectutils topic
写作作业一
How to create a custom 404 error page in WordPress
如何执行建设项目的时间影响分析?
机器学习 | MATLAB实现RBF径向基神经网络newrbe参数设定
Beginner's Guide to electronic bidding
Daniel guild Games: summary and future outlook of this year
Y80. Chapter 4 Prometheus big factory monitoring system and practice -- Kube state metrics component introduction and monitoring extension (XI)
Hilbert transform and instantaneous frequency