当前位置:网站首页>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
边栏推荐
- Dimension table and fact table in data warehouse
- 由ASP.NET Core根据路径下载文件异常引发的探究
- Summary of multithreading and thread synchronization knowledge
- 分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
- [Xiaobai chat cloud] suggestions on container transformation of small and medium-sized enterprises
- mysql 计算经纬度范围内的数据
- The light of ideal never dies
- Leetcode --- longest public prefix
- Recalling the college entrance examination and becoming a programmer, do you regret it?
- JS learning notes - data types
猜你喜欢

总结|机器视觉中三大坐标系及其相互关系

Route service grid traffic through two-level gateway design

Some problems about MySQL installation

Set the background picture in the idea (ultra detailed)

Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction

The light of ideal never dies

华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys

解决** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the defau

Summary of monthly report | list of major events of moonbeam in June

Dimension table and fact table in data warehouse
随机推荐
Conditions and solutions of deadlock
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology
Some problems about MySQL installation
[5g NR] RRC connection release
The difference and usage of calloc, malloc and realloc functions
Typescript array out of order output
idea 公共方法抽取快捷键
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
外企高管、连续创业者、瑜伽和滑雪高手,持续迭代重构的程序人生
JS learning notes - process control
Idea jar package conflict troubleshooting
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
MySQL min() finds the minimum value under certain conditions, and there are multiple results
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
Invalid bound statement (not found)解决方法总结
SSM integration exception handler and project exception handling scheme
Leetcode -- number of palindromes
The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines
Mathematical analysis_ Notes_ Chapter 5: univariate differential calculus