当前位置:网站首页>MySQL queries users logged in for at least N consecutive days
MySQL queries users logged in for at least N consecutive days
2022-04-23 04:37:00 【Jieyou grocery store Q】
data
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for orde
-- ----------------------------
DROP TABLE IF EXISTS `orde`;
CREATE TABLE `orde` (
`id` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`date` datetime NOT NULL,
`orders` varchar(10) COLLATE utf8mb4_general_ci DEFAULT NULL,
PRIMARY KEY (`id`,`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
-- ----------------------------
-- Records of orde
-- ----------------------------
BEGIN;
INSERT INTO `orde` VALUES ('1', '2022-01-01 00:00:00', '101');
INSERT INTO `orde` VALUES ('1', '2022-01-02 00:00:00', '109');
INSERT INTO `orde` VALUES ('1', '2022-01-03 00:00:00', '150');
INSERT INTO `orde` VALUES ('1', '2022-01-04 00:00:00', '991');
INSERT INTO `orde` VALUES ('1', '2022-01-05 00:00:00', '145');
INSERT INTO `orde` VALUES ('1', '2022-01-06 00:00:00', '155');
INSERT INTO `orde` VALUES ('1', '2022-01-07 00:00:00', '199');
INSERT INTO `orde` VALUES ('1', '2022-01-08 00:00:00', '188');
INSERT INTO `orde` VALUES ('2', '2022-01-02 00:00:00', '109');
INSERT INTO `orde` VALUES ('3', '2022-01-03 00:00:00', '150');
INSERT INTO `orde` VALUES ('4', '2022-01-01 00:00:00', '100');
INSERT INTO `orde` VALUES ('4', '2022-01-04 00:00:00', '199');
INSERT INTO `orde` VALUES ('5', '2022-01-05 00:00:00', '145');
INSERT INTO `orde` VALUES ('6', '2022-01-06 00:00:00', '1455');
INSERT INTO `orde` VALUES ('7', '2022-01-07 00:00:00', '199');
INSERT INTO `orde` VALUES ('8', '2022-01-08 00:00:00', '188');
INSERT INTO `orde` VALUES ('9', '2022-01-01 00:00:00', '110');
INSERT INTO `orde` VALUES ('9', '2022-01-02 00:00:00', '109');
INSERT INTO `orde` VALUES ('9', '2022-01-03 00:00:00', '150');
INSERT INTO `orde` VALUES ('9', '2022-01-04 00:00:00', '919');
INSERT INTO `orde` VALUES ('9', '2022-01-06 00:00:00', '145');
INSERT INTO `orde` VALUES ('9', '2022-01-09 00:00:00', '455');
INSERT INTO `orde` VALUES ('9', '2022-01-10 00:00:00', '199');
INSERT INTO `orde` VALUES ('9', '2022-01-13 00:00:00', '188');
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
ranking
SELECT
*,
ROW_NUMBER() over (PARTITION by id ORDER BY date) rn
FROM orde
Ideas : Consecutive date - Continuous sort value = Fix a date
In this way, we can count how many consecutive dates there are
SELECT
*,
DATE_SUB(tmp.date, INTERVAL tmp.rn day) as dateSub
FROM
(SELECT
*,
ROW_NUMBER() over (PARTITION by id ORDER BY date) rn
FROM orde) tmp
And then according to dateSub, id Count the number in groups
Calculate the earliest and latest time of continuous time period
SELECT
tmp2.id,
min(tmp2.date) startDate,
max(tmp2.date) endDate,
count(*) count
FROM
(SELECT
*,
DATE_SUB(tmp.date, INTERVAL tmp.rn day) as dateSub
FROM
(SELECT
*,
ROW_NUMBER() over (PARTITION by id ORDER BY date) rn
FROM orde) tmp) tmp2
GROUP BY tmp2.dateSub, tmp2.id
Add judgment , Specifies to get continuous data 3 More than days of data
SELECT
tmp2.id,
min(tmp2.date) startDate,
max(tmp2.date) endDate,
count(*) count
FROM
(SELECT
*,
DATE_SUB(tmp.date, INTERVAL tmp.rn day) as dateSub
FROM
(SELECT
*,
ROW_NUMBER() over (PARTITION by id ORDER BY date) rn
FROM orde) tmp) tmp2
GROUP BY tmp2.dateSub, tmp2.id
HAVING count(*) > 3
版权声明
本文为[Jieyou grocery store Q]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230406554857.html
边栏推荐
- Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
- AWS EKS添加集群用户或IAM角色
- 【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
- [echart] démarrer avec echart
- 2019 is coming to an end, the longest day.
- Alibaba cloud IOT transfer to PostgreSQL database scheme
- [AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
- [BIM introduction practice] Revit building wall: detailed picture and text explanation of structure, envelope and lamination
- MYSQL去重方法汇总
- C language character constant
猜你喜欢
test
AWS EKS 部署要点以及控制台与eksctl创建的差异
補:注解(Annotation)
递归调用--排列的穷举
Express middleware ② (classification of Middleware)
win10, mysql-8.0.26-winx64. Zip installation
Supplément: annotation
Coinbase: basic knowledge, facts and statistics about cross chain bridge
Common string processing functions in C language
用D435i录制自己的数据集运行ORBslam2并构建稠密点云
随机推荐
Error occurs when thymeleaf th: value is null
Difference between LabVIEW small end sequence and large end sequence
VHDL implementation of 32-bit binary to BCD code
华为机试--高精度整数加法
Chlamydia infection -- causes, symptoms, treatment and Prevention
Cortex-M3寄存器组、汇编语言与C语言的接口介绍
QML advanced (V) - realize all kinds of cool special effects through particle simulation system
三十六计是什么
电钻、电锤、电镐的区别
229. Find mode II
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
zynq平台交叉编译器的安装
Go reflection rule
单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真
补充番外14:cmake实践项目笔记(未完待续4/22)
520.检测大写字母
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
Chapter 4 - understanding standard equipment documents, filters and pipelines
Brushless motor drive scheme based on Infineon MCU GTM module