Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report