当前位置:网站首页>SQL interview question: find the maximum number of consecutive login days
SQL interview question: find the maximum number of consecutive login days
2022-06-28 19:02:00 【South Lake Fishing Song】
Find the maximum number of continuous login days for users mysql Realization 
with t_1 as( -- Use timestampdiff(unit, begin, end) <= 30 Conduct where Conditional filtering , And use distinct Function to get a new table t_1
select DISTINCT dimMemberID as user_id, date(dimDateID) as login_date
from fct_sales
where timestampdiff(day, date(dimDateID),(select max(dimDateID) from fct_sales)) <= 30
and dimMemberID <> 0
order by login_date
),t_2 as( -- Get a press user_id A new table grouped with a sort number t_2
select user_id
,login_date
,row_number()over(partition by user_id order by login_date asc ) as ranks
from t_1
),t_3 as ( -- Use date_sub Function to get a new column gap And new tables t_3
select user_id
,login_date
,ranks
,date_sub(login_date, interval ranks day) as gap
from t_2
), t_4 as ( -- Yes t_3 Table by user_id、gap Grouping , And for the login_date Conduct count Count , Get the table t_4
select user_id
,gap
,count(login_date) as num
from t_3
group by user_id
,gap
) ,t_5 as ( -- according to user_id Grouping , Use max Take out the biggest num
select user_id
,max(num) as series_login -- according to user_id grouping , Find the maximum number of consecutive login days
from t_4
group by user_id
)
select * ,count(1)over() as cnt from t_5;
- with t_1 as
- timestampdiff
边栏推荐
猜你喜欢

Render function parsing

声网发布灵隼物联网云平台 可一小时构建示例场景

1 goal, 3 fields, 6 factors and 9 links of digital transformation

怎样去除DataFrame字段列名

Hands on Teaching of servlet use (1)

Anonymous function this pointing and variable promotion

Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known

Win 10创建一个gin框架的项目

shell读取Json文件的值

Idea merge other branches into dev branch
随机推荐
Analyzing the practical development of robot teaching
async-validator. JS data verifier
new String(“hello“)之后,到底创建了几个对象?
Sound network releases lingfalcon Internet of things cloud platform, which can build sample scenarios in one hour
memory thrashing
Shanghai Pudong Development Bank Software Test interview real question
浅谈软件研发的复杂性与效能提升之道
Steam education to break the barriers between disciplines
Native implementation Net5.0+ custom log
Cvpr2022 | Zhejiang University and ant group put forward a hierarchical residual multi granularity classification network based on label relation tree to model hierarchical knowledge among multi granu
Business layer modification - reverse modification based on the existing framework
180.1. Log in continuously for n days (database)
声网发布灵隼物联网云平台 可一小时构建示例场景
devpi
Memory leak
618活动季——百数低代码平台特享折扣来临
C语言文件操作
About Critical Values
数字化转型的1个目标,3大领域,6大因素和9个环节
Pipeline based hybrid rendering