leetcode-Database-180. Numbers appearing consecutively --- and some pits in this question, and answers using hive-over () function.

      Original title: https://leetcode-cn.com/problems/consecutive-numbers/
      Problem-solving directory https://blog.csdn.net/weixin_42845682/article/details/105196004

1. Title Description

      Write a SQL query to find all numbers that appear at least three times in a row.

+----+-----+
| Id | Num |
+----+-----+
| 1  |  1  |
| 2  |  1  |
| 3  |  1  |
| 4  |  2  |
| 5  |  1  |
| 6  |  2  |
| 7  |  2  |
+----+-----+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/consecutive-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/consecutive-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

      A few points to note:
      1. It appears three times in a row, not three times.
      2. If more than 3 times, no matter how many times, the number can only appear once. For example: 3 appears 6 times in a row, and there can only be 1 3 in the final result.

Second, the first answer-mysql

      my answer:

select 
	distinct(n1) ConsecutiveNums  /* 因为条件2,所以需要distinct */
from(
select  
	l1.id i1,
	l1.num n1,
	l2.id i2,
	l2.num n2,
	l3.id i3,
	l3.num n3  
from logs l1
join logs l2 on l1.id+1=l2.id
join logs l3 on l1.id+2=l3.id
) tmp
where n1 = n2
and n2 = n3 

      It should be noted here that where must be written after: n1 = n2
and n2 = n3. Instead of n1 = n2 = n3, why? It is recommended to refer to this article I wrote: In Mysql, what is the difference between 1 = 1 and 1 = 1 = 1 and -1 = -1 and -1 = -1 = -1 and 5 = 5 and 5 = 5 = 5 . The address is: https://blog.csdn.net/weixin_42845682/article/details/105262892

3. The second answer – hive

      Its practical over () can also be done. But mysql does not have over (). For the sake of convenience, I did it with hive.
      First of all, for the convenience of the diagram, enable the local mode of hive:

hive (test)> set hive.exec.mode.local.auto=true;
hive (test)> set hive.exec.mode.local.auto;
hive.exec.mode.local.auto=true

      Insert the data and take a look at the query.

hive (test)> select * from logs;
OK
logs.id	logs.num
1	1
2	1
3	1
4	2
5	1
6	2
7	2
Time taken: 0.047 seconds, Fetched: 7 row(s)

      Use over () to query:

select 
    distinct(n1)
from(
	select 
		id,
		num n1, 
		lag(num,1) over(  ) n2,
		lag(num,2) over(  ) n3 
	from logs
) t1
where n1 =n2 
and n2=n3

Fourth, the third answer-hive expansion

      Now it is required to appear 3 times in a row. If it is required to appear 5 times in a row, 10 times in a row? Do we have to write so many joins or so many lags?
      No, you have to change the wording.
      Next, the process is a bit complicated, try to read it. . .
      First, this is the original data:

hive (test)> select * from logs;
OK
logs.id	logs.num
1	1
2	1
3	1
4	2
5	1
6	2
7	2
Time taken: 0.027 seconds, Fetched: 7 row(s)

Tired, write again after a while.

      
      
      
      
      
      

Published 48 original articles · Like 36 · Visits 130,000+

Guess you like

Origin blog.csdn.net/weixin_42845682/article/details/105264355