QuXiangjie / Study-Review

自己欠缺的还太多了,希望通过总结每天的学习内容,整理每天的思绪来丰富自己的知识库。我想成为一名优秀的金融数据分析师,并行发展技术与商业业务。博客内容为:数理统计、财务业务、Python(数据分析及可视化)、Excel(数据分析)、SQL、英文
0 stars 0 forks source link

180. Consecutive Numbers #6

Open QuXiangjie opened 7 months ago

QuXiangjie commented 7 months ago

Question SQL solution

SELECT distinct t1.num as ConsecutiveNums
FROM logs t1
JOIN logs t2 ON t1.id + 1 = t2.id
JOIN logs t3 ON t1.id + 2 = t3.id
WHERE t1.num = t2.num and t2.num = t3.num

The join table looks like id1 num1, id2 num2, id3 num3, which makes the id 2 and id 3 and id 1 in the same row, and can be compared.

Below is a better solution from peers, this solution is amazing!

select distinct Num as ConsecutiveNums
from Logs
where (Id + 1, Num) in (select * from Logs) and (Id + 2, Num) in (select * from Logs)