Working solution: WITH cte AS (SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id) AS rnk FROM Logs), cte2 AS (SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur FROM cte) SELECT DISTINCT num AS ConsecutiveNums FROM cte2 GROUP BY diff, num HAVING COUNT(*) >= 3;
when id's are jumbled up and id's don't follow sequence use this query with cte as ( select id, num ,lead(num,1) over(order by id) as next, lead(num,2) over(order by id ) as next_2_next, lead(id,1) over(order by id) as next_team_id, lead(id,2) over(order by id) as next_2_next_team_id from Logs ) select distinct num as ConsecutiveNums from cte where (num=next and num=next_2_next) and (id+1=next_team_id and id+2=next_2_next_team_id)
Hi Bro, The given solution is not working currently. Not sure if test cases are updated. Kindly help us if there is any other easy approach to solve this. Thank you
with cte as ( select id,num,lead(num,1) over(order by id) as next,lead(num,2) over(order by id ) as next_2_next, lead(id,1) over(order by id) as next_team_id,lead(id,2) over(order by id) as next_2_next_team_id from Logs ) select distinct num as ConsecutiveNums from cte where (num=next and num=next_2_next) and (id+1=next_team_id and id+2=next_2_next_team_id) can use this one
WITH cte AS (SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id) AS rnk FROM Logs), cte2 AS (SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur FROM cte) SELECT DISTINCT num AS ConsecutiveNums FROM cte2 GROUP BY diff, num HAVING COUNT(*) >= 3;
WITH cte AS (SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id) AS rnk FROM Logs), cte2 AS (SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur FROM cte) SELECT DISTINCT num AS ConsecutiveNums FROM cte2 GROUP BY diff, num HAVING COUNT(*) >= 3;
use DISTINCT in SELECT statement: WITH cte AS( SELECT *, LEAD(num,1) OVER() AS NEXT_1, LEAD(num,2) OVER() AS NEXT_2 FROM Logs) SELECT DISTINCT num AS ConsecutiveNums FROM cte WHERE num = NEXT_1 AND num = NEXT_2;
WITH cte AS (SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id) AS rnk FROM Logs), cte2 AS (SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur FROM cte) SELECT DISTINCT num AS ConsecutiveNums FROM cte2 GROUP BY diff, num HAVING COUNT(*) >= 3;
WITH cte AS (SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id) AS rnk FROM Logs), cte2 AS (SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur FROM cte) SELECT DISTINCT num AS ConsecutiveNums FROM cte2 GROUP BY diff, num HAVING COUNT(*) >= 3;
Working solution:
WITH cte AS
(SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id)
AS rnk
FROM Logs),
cte2 AS
(SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur
FROM cte)
SELECT DISTINCT num AS ConsecutiveNums
FROM cte2
GROUP BY diff, num
HAVING COUNT(*) >= 3;
when id's are jumbled up and id's don't follow sequence use this query with cte as
(
select id, num ,lead(num,1) over(order by id) as next, lead(num,2) over(order by id ) as next_2_next,
lead(id,1) over(order by id) as next_team_id, lead(id,2) over(order by id) as next_2_next_team_id
from Logs
)
select distinct num as ConsecutiveNums
from cte
where (num=next and num=next_2_next)
and (id+1=next_team_id and id+2=next_2_next_team_id)
I WAS WONDERING WHY THE CODE IN THIS VIDEO IS NOT WORKING. BUTY, AFTER WRITING YOUR CODE , ALL TESTCASES PASSED. THANKS
These videos are very helpful in learning sql!!!!
loved the solution , thanks
Hi Bro, The given solution is not working currently. Not sure if test cases are updated. Kindly help us if there is any other easy approach to solve this. Thank you
brilliant solution thanks
explanation top 100%
Glad that you found the video useful 😊
Great. Thanks
for sql server? we can't mention inside the argument in lead function
nice
perfectly explained, Thnx
This won't work when Id's are jumbled up
with cte as
(
select id,num,lead(num,1) over(order by id) as next,lead(num,2) over(order by id ) as next_2_next,
lead(id,1) over(order by id) as next_team_id,lead(id,2) over(order by id) as next_2_next_team_id
from Logs
)
select distinct num as ConsecutiveNums
from cte
where (num=next and num=next_2_next)
and (id+1=next_team_id and id+2=next_2_next_team_id) can use this one
WITH cte AS
(SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id)
AS rnk
FROM Logs),
cte2 AS
(SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur
FROM cte)
SELECT DISTINCT num AS ConsecutiveNums
FROM cte2
GROUP BY diff, num
HAVING COUNT(*) >= 3;
Awesome !!
Glad that you found the video useful 😊
Great Explanation
Glad that you found it useful, Rukshar.
@@EverydayDataScience Can you make a video on Tree node -problem 608
why can't we write where num = next_1 = next_2? Thank you!
Fails for negative nums idk why.
test case with 4 1's are not accepting.
WITH cte AS
(SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id)
AS rnk
FROM Logs),
cte2 AS
(SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur
FROM cte)
SELECT DISTINCT num AS ConsecutiveNums
FROM cte2
GROUP BY diff, num
HAVING COUNT(*) >= 3;
Sir please update the solution since question is telling that atleast three so there can be many more occurrences for same no.
use DISTINCT in SELECT statement:
WITH cte AS(
SELECT *, LEAD(num,1) OVER() AS NEXT_1,
LEAD(num,2) OVER() AS NEXT_2
FROM Logs)
SELECT DISTINCT num AS ConsecutiveNums
FROM cte
WHERE num = NEXT_1 AND
num = NEXT_2;
WITH cte AS
(SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id)
AS rnk
FROM Logs),
cte2 AS
(SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur
FROM cte)
SELECT DISTINCT num AS ConsecutiveNums
FROM cte2
GROUP BY diff, num
HAVING COUNT(*) >= 3;
This solution is not working for some test cases. Please check it once .
WITH cte AS
(SELECT *, ROW_NUMBER() OVER(PARTITION BY num ORDER BY id)
AS rnk
FROM Logs),
cte2 AS
(SELECT *, (id*1.0-rnk*1.0) AS diff. // multiplication with 1.0 is done so that SIGNED/UNSIGNED difference error doesn't occur
FROM cte)
SELECT DISTINCT num AS ConsecutiveNums
FROM cte2
GROUP BY diff, num
HAVING COUNT(*) >= 3;
Thanks sir for such a great explanation. What's your paypal?
brilliant solution thanks