有一张游客表,记录了7月份某个地方的游客数量
id date visits
1 2017-07-01 100
……
id的值等于日期列中day的值,如何输出接下来连续三天访问量均超过100人的日期?
试试这个。
SELECT id,
date_visit,
visits
FROM (SELECT a.*,
COUNT(*)
OVER (
PARTITION BY rn ) cnt
FROM (SELECT t.*,
id - ROW_NUMBER()
OVER (
ORDER BY id) rn
FROM table1 t
WHERE visits > 100) a) b
WHERE cnt >= 3
ORDER BY id;
我认为这对你的情况有用。
SELECT * FROM `Tourists`
WHERE `visits` > 100
ORDER BY date [ ASC | DESC ]
LIMIT 3
根据您的要求选择 ASC 或 DESC。
如果我错误地理解了您的需求,请告诉我
如果我们只有一个大于 3 的序列,则得票最高的答案有效。如果我们有两个大于 3 的序列怎么办?那么测试结果就会失败。
我在这种情况下提出的解决方案。
with cte as (
select
*,id-ROW_NUMBER() over (order by id) as cn
from Visitors where NO_of_visitor >100
)
select * from cte
where cn >= (select MAX(cn) from cte)
与 cte AS ( 选择a.id AS id1、b.id AS id2、c.id AS id3 来自游客作为 INNER JOIN 游客 AS b ON b.id= a.id+1 AND b.visits>100 内连接游客 AS c ON c.id=b.id+1 AND c.visits>100 哪里 a.访问量 >100 ) SELECT * FROM 游客 WHERE 游客.id IN (SELECT id1 FROM cte UNION SELECT id2 FROM cte UNION SELECT id3 FROM cte );