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

边栏推荐
- What are the methods to track the real-time market of London Silver?
- 【Leetcode-滑动窗口问题】
- Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
- iNFTnews | 元宇宙购物体验将成为吸引消费者的一大利器
- Talk about the cross end technical scheme
- 【commons-lang3专题】003- RandomStringUtils 专题
- 递归与分治
- SystemVerilog-连接和复制运算符
- Principle and usage setting of large page memory
- Charles -- teach you how to use the packet capturing tool from 0-1
猜你喜欢

大页内存原理及使用设置

Visual full link log tracking

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

浅谈一下跨端技术方案

PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益

What are the methods to track the real-time market of London Silver?

分类预测 | MATLAB实现TCN时间卷积神经网络的时序分类预测

Summary of process and thread knowledge points 2

The method of tracking the real-time market of London Silver

Consumer unit 消费单元
随机推荐
mysql分表之后怎么平滑上线?
18张图,直观理解神经网络、流形和拓扑
system verilog常用语法
Prometheus 的 API 稳定性保障
散列表 ~
递归与分治
Hilbert transform and instantaneous frequency
【树莓派】widows电脑如何与树莓派连接
【AD学习】本次海上航行器大赛画pcb图的历程
自制 | 纯手工自制一个16位RISC架构CPU
Solid smart contract tutorial (5) -nft auction contract
Principle and usage setting of large page memory
如何给女友讲明白JS的bind模拟实现
[untitled]
图扑软件亮相 2022 福州数博会,携手共创数字新时代
🧐 table1 | 一秒搞定你的三线表
Talk about the cross end technical scheme
【刷题笔记】二进制链表转整数
In the second round, 1000 okaleido tiger were sold out in one hour after logging in to binance NFT again
regular expression