How to solve the problem of satisfying N consecutive days in SQL

SQL column

Summary of basic knowledge of SQL database

Summary of advanced knowledge of SQL database

In our daily statistics process, the problem of satisfying the statistics for N consecutive days is very common. For example, count the days where the sales for three consecutive days are greater than 100,000, and the days where the passenger flow is greater than 100 for a week, and so on.

Today we use an example to show you how to solve similar problems.

(This is a question I posted on Knowledge Planet. Interested friends can long press the QR code at the end of the article to join in.)

In a gymnasium, daily traffic information is recorded in these three columns: serial number (id), date (date), and traffic (people). Please write a query statement to find out the peak period, which requires three consecutive days or more, and the daily flow of people is not less than 100. For example, the table stadium:

How to solve the problem of satisfying N consecutive days in SQL

For the example data above, the output is:

How to solve the problem of satisfying N consecutive days in SQL

The problem and the desired result are already known, how to solve it? Two ideas for solving problems are provided below

Idea 1: Find the date difference

It can be known that the three records before and after a certain date are continuous by solving for the date difference before and after a certain date as 1 and -1 respectively. The popular point is that the difference between today and the previous day is 1, and the difference between today and tomorrow is -1, so the dates of yesterday, today and tomorrow are consecutive for three consecutive days.

The specific solution is as follows:

SELECT DISTINCT d.*
FROM stadium d,stadium a
WHERE ABS(DATEDIFF(DAY,d.date,a.date))<2  --求解时间差绝对值为1或0的日期
AND a.date IN
(
SELECT a.date
FROM stadium a,stadium b,stadium c
WHERE
DATEDIFF(DAY,a.date ,b.date)=1  --今天减去昨天
AND DATEDIFF(DAY,a.date,c.date)=-1  --今天前去明天
AND a.people >99    
AND b.people > 99
AND c.people >99
)

(Hint: You can slide the code left and right)

Idea 2: Permutation and combination

According to the continuity of id, there are three cases: divided into three tables s1, s2, s3 combination judgment,
(1) s1.id-s2.id=1, s2.id-s3.id=1, equivalent Three consecutive
(2) s2.id-s1.id=1, s1.id-s3.id=1 in the sequence of s3 s2 s1 , which is equivalent to three consecutive
(3) s3.id in the sequence of s3 s1 s2 -s2.id=1,s2.id-s1.id=1, which is equivalent to three consecutive in the order of s1 s2 s3

The specific solution is as follows:


select DISTINCT s1.*
from stadium s1, stadium s2, stadium s3
where s1.people >= 100 and s2.people>= 100 and s3.people >= 100
and
(
    (s1.id - s2.id = 1 and s2.id - s3.id =1)
   or
   (s2.id - s1.id = 1 and s1.id - s3.id =1) 
   or
   (s3.id - s2.id = 1 and s2.id - s1.id = 1) 
) order by s1.id;

However, this method is limited to suitable scenarios. If it exceeds 3 days, the number of consecutive permutations and combinations will increase, and the effect is not very good.

The above two ideas can be obtained. Friends may wish to try it yourself.

Guess you like

Origin blog.51cto.com/15057820/2655153