Get the App
SLTechnology News&Howtos  ›  Development  › 

How does SQL query users who have placed orders for at least seven consecutive days

Shulou Source: shulou.com Published: 2022-06-02 04:26:17 10月04日 Update

This article introduces the knowledge of "how to query users who have placed orders for at least seven days in a row". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Create table create table orde (id varchar (10), date datetime,orders varchar (10)) Insert into orde values ('1 pound, '2019 racer 1 pound) insert into orde values (' 1 pound, '2019 peg 1 pound) insert into orde values (' 1 pound, '2019 pound 1 pound 3 pound dagger 150) insert into orde values (' 1 pound, '2019 pound 1 pound 4 pound 99) insert into orde values (' 1 pound, '2019 pound 1 pound 5 pound) insert into orde values (' 1 pound, '201919 pound 1 pound 6') 1455) insert into orde values ('1pm,' 20119) insert into orde values ('4', '20119) insert into orde values (' 4', '20119) insert into orde values (' 2pm, '20119) insert into orde values (' 3) insert into orde values ('4', '20119) 99) insert into orde values ('5mm,' 20119) insert into orde values ('6pm,' 20119) insert into orde values ('7pm,' 20119) insert into orde values ('8pm,' 20119) insert into orde values ('9', '201919) insert into orde values (' 9', '201919, 1nd) insert into orde values (' 9', '201919) ) insert into orde values ('9pm,' 20119) insert into orde values ('9, '2019) insert into orde values (' 9, '20119) insert into orde values (' 9, '2019) insert into orde values

The tables are grouped according to id and ranked by time, time minus ranking rnk, and if the time is continuous, the result of subtraction is equal:

Select *, date- rank rnk from (select *, row_number () over (partition by id order by date) rank from orde) a

Then grouped and counted according to id,rnk

Select id,rnk,count (*) ok from (select *, date- ranking rnk from (select *, row_number () over (partition by id order by date) ranking from orde) a) b group by id,rnk

Get the following table:

Finally, filter the count item ok > = 7.

You can also filter directly:

Select id,rnk from (select *, date- ranking rnk from (select *, row_number () over (partition by id order by date) ranking from orde) a) b group by id,rnk having count (*) > = 7; "how to query users who have placed orders for at least seven consecutive days" is here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Time users queries content more knowledge grouping practicality achievement next dilemma reality ideas situations articles cases results editing websites industries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Linux MySQL Docker