当前位置:网站首页>[MySQL] historical cumulative de duplication of multiple indicators
[MySQL] historical cumulative de duplication of multiple indicators
2022-07-29 01:19:00 【Tanyue Jianzhi Dachang】
Multi index cumulative weight removal problem
1. Data preparation
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. To write sql
I. Accumulated on the same day
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. Historical accumulation
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
;

边栏推荐
- log4j动态加载配置文件
- [Commons lang3 topic] 002 randomutils topic
- Letax record \documentclass{}, authoryear attribute is used
- 时间复杂度、空间复杂度的学习总结
- [ManageEngine] what is the LAN monitoring software and what is its function
- mysql分表之后怎么平滑上线?
- [unity] configure unity edit C as vscode
- Flink Postgres CDC
- [ManageEngine] help Harbin Engineering University realize integrated monitoring and management of network traffic
- How to check the redis Version (check the redis process)
猜你喜欢

Intel带你初识视觉识别--OpenVINO

This article enables you to understand the underlying principle of MySQL- Internal structure, index, lock, cluster

A new generation of ultra safe cellular battery, Sihao aipao, is on the market, starting from 139900

【idea】查询字段使用位置

新一代超安全蜂窝电池,思皓爱跑上市,13.99万起售
![[target detection] Introduction to yolor theory + practical test visdrone data set](/img/cd/3cb13d6d79cd207c6d637af7756ffc.png)
[target detection] Introduction to yolor theory + practical test visdrone data set

北京护照西班牙语翻译推荐

ACM SIGIR 2022 | 美团技术团队精选论文解读

【mysql】多指标历史累计去重问题

对接支付宝支付
随机推荐
Naver 三方登录
How to check the redis Version (check the redis process)
solidity实现智能合约教程(5)-NFT拍卖合约
进程和线程知识点总结1
[notes for question brushing] binary linked list to integer
正则校验与时间格式化
[Commons lang3 topic] 005- objectutils topic
[notes for question brushing] specified interval reversal in the linked list
递归与分治
进程和线程知识点总结 2
【mysql】字符串转int
[target detection] Introduction to yolor theory + practical test visdrone data set
SDRAM Controller Design (two design methods of digital controller)
(update 20211130) about the download and installation of Jupiter notebook and its own configuration and theme
Cookie和Session
可视化全链路日志追踪
如何处理项目中的时间、范围和成本限制?
Seven SQL performance optimizations that spark 3.0 must know
新一代超安全蜂窝电池,思皓爱跑上市,13.99万起售
教你一文解决 js 数字精度丢失问题