当前位置:网站首页>SQL solves the problem of continuous login deformation holiday filtering
SQL solves the problem of continuous login deformation holiday filtering
2022-07-02 16:27:00 【Mu code text】
Content catalog
One 、 explain
Let's first talk about the effects that need to be achieved , It is to judge holidays and working days , If it's a weekday , Return to the current day , If it is a holiday or rest , Return to the next working date
06-01 Working day 06-01
06-02 Weekend 06-04 * because The next working day is 06-04
06-03 Weekend 06-04
06-07、06-08、06-09 The holiday season , return 06-10, That is, the next working day
There are two solutions explained before the continuous login days , It can solve the problem of continuous login , Then it is often used in actual production , But for the deformation of some such problems , Whether it can be easily solved ?
Two 、 Requirement specification
There's a demand recently , It is not demand , It is a small function implementation . The need to do event warning before , It is necessary to feed back the alarm events , Judge whether it is the feedback of the day .
At first, the idea was simple , Only considering the weekend , Set the alarm event if it is a weekend , Feedback on Monday is considered as feedback on the day , It was achieved in this way :
If it's Saturday , Feedback date increased 2, If it's Sunday , Feedback date increased 1
case dayofweek(alarm_date)
when 7 then date_add(fb_date,2)
when 0 then date_add(fb_date,1)
else fb_date end as fb_date
But in practice , Found a checkpoint with holidays , Especially in the first half of the year, there are many holidays , Therefore, not filtering holidays has a great impact on the results , in addition , For some weekends , It may also be working days off .
3、 ... and 、 Filter holidays
Why is it said that this problem is a deformation of the continuous login problem ? Let's think about it , Are holidays continuous ? Whether it's weekends or holidays , Are consecutive dates . Then the question becomes , We need to distinguish which are holidays ? Which are working days ?
I think of two ways to realize this
Train of thought
To write UDF, stay jar Maintain a file in the package , Documents can record the date of holidays , Write a custom one UDF Implement this function
This method can be implemented , But the process will be a little cumbersome , But the advantage is that it can be reused , Write once , Use everywhere
Train of thought two
Maintain a holiday dimension table , Through some sql Logic implementation , The advantage is that the implementation process is not cumbersome , The disadvantage is that reusability is not strong . For shortcomings , In fact, you can output the results to a dimension table for use , At the same time, some other dimensions can be added , Because there are many time and holiday dimensions in the production environment .
Four 、 Function realization
Adopt idea 2 to realize , Maintain a dimension table , Whether the date is a holiday is recorded in the dimension table , There are also two ways to implement
The way 1: Equal difference solution
Recall the solution of continuous login problem , Calculate equal difference , Find consecutive days , Sort in positive order , Returns the next day of consecutive days , Is the result of the need
The flow chart is as follows :
| date | Whether it's a holiday or not | Return value |
|---|---|---|
| 06-01 | 1 | 06-03 |
| 06-02 | 1 | 06-03 |
| 06-03 | 0 | 06-03 |
| 06-04 | 1 | 06-05 |
| 06-05 | 0 | 06-05 |
1 select
2 log_date,
3 date_add(log_date, rn) next_work_day
4 from (
5 select
6 log_date,
7 row_number()
8 over(partition by start_day order by log_date desc) rn
9 from (
10 select
11 log_date,
12 date_sub(log_date, rn) start_day
13 from(
14 select
15 log_date,
16 row_number()
17 over(order by log_date) rn
18 from (
19 select
20 log_date
21 from
22 tmp_bdp.tmp_log_date
23 ) t1
24 ) t2
25 ) t3
26 ) t4
The way 2: Subtraction before and after
It is also a way to simulate continuous login , Find the next day of consecutive days , Is the value to return
1 select
2 log_date,
3 max(log_date)
4 over(partition by flag order by log_date) next_day
5 from (
6 select
7 log_date,
8 sum(if(diff_date > 1, 1, 0))
9 over(order by log_date) flag
10 from (
11 select
12 log_date,
13 datediff(log_date, lag_date) diff_date
14 from (
15 select
16 `date` as log_date,
17 lag(`date`, 1, '1970-01-01')
18 over(order by `date`) lag_date
19 from
20 bili_dim.dim_date_info_d
21 where year(`date`) = 2022 and holiday_type <> 0
22 ) t1
23 ) t2
24 ) t3
边栏推荐
- Group by的用法
- 关于mysql安装的一些问题
- Add user-defined formula (time sharing t+0) to mobile app access as an example
- Yyds dry inventory company stipulates that all interfaces use post requests. Why?
- Summary of multithreading and thread synchronization knowledge
- Understand the key technology of AGV -- the difference between laser slam and visual slam
- Mathematical analysis_ Notes_ Chapter 6: Riemann integral of univariate function
- 一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
- Boot 连接 Impala数据库
- Bib | graph representation based on heterogeneous information network learning to predict drug disease association
猜你喜欢
随机推荐
Introduction to database system Chapter 1 short answer questions - how was the final exam?
七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
dried food! Understand the structural vulnerability of graph convolution networks
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
ROW_NUMBER()、RANK()、DENSE_RANK区别
Huawei ECS installs mysqlb for mysqld service failed because the control process exited with error code. See “sys
How to use stustr function in Oracle view
Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time
Mysql database mysqldump why there is no statement to create a database
JS learning notes - data types
PCL 最小中值平方法拟合平面
sim2real环境配置教程
JS learning notes - operators
JS learning notes - first acquaintance
Route service grid traffic through two-level gateway design
Seal Library - installation and introduction
仙人掌之歌——投石问路(3)
Which software is good for machine vision?
路由模式:hash和history模式








