当前位置:网站首页>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
边栏推荐
- Add user-defined formula (time sharing t+0) to mobile app access as an example
- Everyone Xinfu builds: a one-stop intelligent business credit service platform
- 做机器视觉哪个软件好?
- Does bone conduction earphone have external sound? Advantages of bone conduction earphones
- Leetcode -- number of palindromes
- [fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
- Typescript array out of order output
- Yyds dry inventory company stipulates that all interfaces use post requests. Why?
- PCL 最小中值平方法拟合平面
- 月报总结|Moonbeam6月份大事一览
猜你喜欢

Yyds dry inventory method of deleting expired documents in batch

What is Amazon keyword index? The consequences of not indexing are serious

触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加

IDEA中设置背景图片(超详细)

做机器视觉哪个软件好?

Solve * * warning * *: your ApplicationContext is unlikely to start due to a @componentscan of the defau

Add user-defined formula (time sharing t+0) to mobile app access as an example

Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation

Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!

Construction and business practice of Zhongke brain knowledge map platform
随机推荐
PyC file decompile
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
Effectively use keywords to increase Amazon sales
云原生的 CICD 框架:Tekton
TypeScript数组乱序输出
Summary of multithreading and thread synchronization knowledge
Some problems about MySQL installation
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
Original God 2.6 server download and installation tutorial
仙人掌之歌——投石问路(2)
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条...
Comprehensively interpret the background and concept of service mesh
Boot connection to impala database
Source code look me
How to use stustr function in Oracle view
Yyds dry goods inventory student attendance system based on QT design
理想之光不灭
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus