当前位置:网站首页>True MySQL interview question (19) -- Tiktok -- select the list of users who have logged in for two consecutive days every month

True MySQL interview question (19) -- Tiktok -- select the list of users who have logged in for two consecutive days every month

2022-06-22 07:12:00 Socialphobia_ FOGO

Check that there is continuous login every month 2 User list of days

There is a “ User login record form ”, Contains two fields : user id、 date .
 Insert picture description here
give the result as follows :
 Insert picture description here

# Inquire about 2021 Every month of the year , continuity 2 There is a list of logged in users every day .
SELECT DISTINCT 
  MONTH( date ) AS  month ,
   user id 
FROM
  (SELECT 
    *,
    lead ( date ) over (
      PARTITION BY  user id,
      MONTH( date ) 
  ORDER BY  date 
  ) AS  Next login date of this month  
  FROM
     User login record form  
  WHERE YEAR( date ) = 2021) a 
WHERE DATEDIFF(
     Next login date of this month ,
     date 
  ) = 1 
ORDER BY  month 

原网站

版权声明
本文为[Socialphobia_ FOGO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220704214185.html