Create a tablecreate table order(id varchar(10),date datetime,orders varchar(10)); insert into orde values('1' , '2019/1/1',10 ) insert into orde values('1' , '2019/1/2',109 ) insert into orde values('1' , '2019/1/3',150 ) insert into orde values('1' , '2019/1/4',99) insert into orde values('1' , '2019/1/5',145) insert into orde values('1' , '2019/1/6',1455) insert into orde values('1' , '2019/1/7',199) insert into orde values('1' , '2019/1/8',188 ) insert into orde values('4' , '2019/1/1',10 ) insert into orde values('2' , '2019/1/2',109 ) insert into orde values('3' , '2019/1/3',150 ) insert into orde values('4' , '2019/1/4',99) insert into orde values('5' , '2019/1/5',145) insert into orde values('6' , '2019/1/6',1455) insert into orde values('7' , '2019/1/7',199) insert into orde values('8' , '2019/1/8',188 ) insert into orde values('9' , '2019/1/1',10 ) insert into orde values('9' , '2019/1/2',109 ) insert into orde values('9' , '2019/1/3',150 ) insert into orde values('9' , '2019/1/4',99) insert into orde values('9' , '2019/1/6',145) insert into orde values('9' , '2019/1/9',1455) insert into orde values('9' , '2019/1/10',199) insert into orde values('9' , '2019/1/13',188 ) IdeasGroup the table by id and rank by time, subtract the rank rnk from the time. If the time is continuous, the subtraction result is equal: select *,date-rankrnk from (select *,row_number() over(partition by id order by date) rankfrom orde) a; Then group by id, rnk, and count select id,rnk,count(*) ok from (select *,date-rankrnk from (select *,row_number() over(partition by id order by date) rankfrom orde) a) b group by id,rnk; The following table is obtained: Finally, filter the count items ok>=7. You can also filter directly: select id,rnk from (select *,date-rankrnk from (select *,row_number() over(partition by id order by date) rankfrom orde) a) b group by id,rnk having count(*)>=7; The above is the SQL query that the editor introduced to you for users who have placed orders for at least seven consecutive days. I hope it will be helpful to you. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: 5 basic skills of topic page design (Alibaba UED Shanmu)
>>: What are the new CSS :where and :is pseudo-class functions?
This article introduces common problems of Xshell...
This article shares the specific code of JavaScri...
Preface Take Element Plus as an example to config...
Table name and fields –1. Student List Student (s...
Common utf8mb4 sorting rules in MySQL are: utf8mb...
Table of contents 1. Vertical (longitudinal) slic...
Table of contents 1. Introduction to NFS 2. NFS C...
Table of contents 1. Basic Concepts 1.1 Two kinds...
Adding necessary comments is a good habit that a ...
First, let me talk about the general idea: 1. Log...
Table of contents 1. Steps 1. Define buttom permi...
This article describes the steps to install the p...
<br />Related articles: 9 practical tips for...
Table of contents Preface 1. Arrange the installa...
Use the vscode editor to create a vue template, s...