当前位置:网站首页>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
边栏推荐
- SSM integration exception handler and project exception handling scheme
- 关于mysql安装的一些问题
- 华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys
- Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)
- By asp Net core downloads files according to the path exception
- 结构体的内存对齐
- 虚假的暑假
- 源码look me
- Song of cactus - throwing stones to ask the way (2)
- 电脑管理员权限在哪里可以打开
猜你喜欢

原神2.6服务端下载以及搭建安装教程

Does bone conduction earphone have external sound? Advantages of bone conduction earphones

Remove the underline in router link

mysql数据库mysqldump为啥没有创建数据库的语句

通过两级网关设计来路由服务网格流量

Everyone Xinfu builds: a one-stop intelligent business credit service platform

Original God 2.6 server download and installation tutorial

Construction and business practice of Zhongke brain knowledge map platform

路由模式:hash和history模式
![[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology](/img/5c/5f835c286548907f3f09ecb66b2068.jpg)
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology
随机推荐
Add user-defined formula (time sharing t+0) to mobile app access as an example
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
Recommended practice sharing of Zhilian recruitment based on Nebula graph
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
Comprehensively interpret the background and concept of service mesh
电脑设备打印机驱动安装失败如何解决
Processing gzip: stdin: not in gzip format: child returned status 1tar: error is not recoverable: exitin
Yyds dry inventory method of deleting expired documents in batch
Boot 中bean配置覆盖
win10系统升级一段时间后,内存占用过高
dried food! Understand the structural vulnerability of graph convolution networks
sql解决连续登录问题变形-节假日过滤
Today in history: Alipay launched barcode payment; The father of time-sharing system was born; The first TV advertisement in the world
How to use stustr function in Oracle view
[fluent] dart data type string type (string definition | string splicing | string API call)
绝对真理和相对真理思考
Idea jar package conflict troubleshooting
Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
Leetcode -- number of palindromes
Storage, reading and writing of blood relationship data of Nepal Graph & Data Warehouse