Practice Complex SQL Queries | SQL Query to fetch N consecutive records

  Рет қаралды 79,483

techTFQ

techTFQ

Күн бұрын

Пікірлер: 191
@brianligat2038
@brianligat2038 Жыл бұрын
Have been doing SQL for years, but your clips are still very informative. Thanks.
@arihantdhadiwal9115
@arihantdhadiwal9115 3 жыл бұрын
Amongst so many tutorials out there on youtube , hands down your's are the best. Will eagerly wait for your upcoming uploads . thanks
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Arihant 🙏🏼 Am so glad to hear you liked my contents 🙂
@joe2271
@joe2271 2 жыл бұрын
Seriously. You are awesome and explain it so well
@techTFQ
@techTFQ 2 жыл бұрын
Thank you 🙏🏼
@minalgupta7456
@minalgupta7456 2 жыл бұрын
I agree
@racheldong60
@racheldong60 Жыл бұрын
this is a great presentation to explain how to pull N consecutive records! Thank you
@arindamnath1233
@arindamnath1233 2 жыл бұрын
The best explanation for consecutive record fetching I ever get. Thank you
@techTFQ
@techTFQ 2 жыл бұрын
Glad you think so 🙏🏼
@guchhusworld587
@guchhusworld587 Жыл бұрын
Thanks TFQ for your videos. Just wanted tweak the last part of the query to make it more generic. With t1 as ( select id,city,temperature,day, row_number() over (order by id) as diff, id - row_number() over (order by id) as conse from weather where temperature
@mananshah7095
@mananshah7095 2 жыл бұрын
You are one of the best SQL instructor on KZbin. I have been using SQL for more than 2 years but still I have learnt so much from you. Please keep uploading the videos. Thank you!
@techTFQ
@techTFQ 2 жыл бұрын
Glad to hear that Manah :)
@reigngamer94
@reigngamer94 2 жыл бұрын
Complex query explained in the best way
@techTFQ
@techTFQ 2 жыл бұрын
Thank you :)
@bhuwanthapliyal7977
@bhuwanthapliyal7977 3 жыл бұрын
No doubt your videos are more elaborate and better than institutes charging so much. One request, please create a series for SQL to cover end to end course
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Bhuwan.. glad you like it 🙏🏼 Yes I will make detailed SQL tutorial videos in the near future..
@InvisibleDivide
@InvisibleDivide 2 жыл бұрын
Solution is so simple yet so big brain at the same time.
@karthikd2104
@karthikd2104 3 жыл бұрын
Clear explanation ! Easy way to understand!
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Karthik 🙏🏼 Glad you liked it ..
@ashabhumza3394
@ashabhumza3394 2 жыл бұрын
Mashallah! You are really an SQL genius... 👍
@techTFQ
@techTFQ 2 жыл бұрын
Thank you Ashab ☺️
@abhilashs3160
@abhilashs3160 2 жыл бұрын
Great way of explaining complex concepts so that everyone can understand it clearly, you are a life saver.
@techTFQ
@techTFQ 2 жыл бұрын
Thank you Abhilash .. glad you liked it
@vikranttyagi5232
@vikranttyagi5232 2 жыл бұрын
All of your SQL videos are really helpful to understand the advanced concepts. Thanks for making them.
@techTFQ
@techTFQ 2 жыл бұрын
Thank you Vikrant :)
@pavangsk8404
@pavangsk8404 Жыл бұрын
you have touth a very important problem in awesome way... brillliant sir
@vikaskumar-qr5tj
@vikaskumar-qr5tj 3 жыл бұрын
Beautiful explanation just crystal clear.....
@techTFQ
@techTFQ 3 жыл бұрын
Thank you vikas
@aashishmalhotra
@aashishmalhotra 2 жыл бұрын
Perfect zoom for phone user , perfect mic , and perfect execution to approach a problem! Make more videos on companies SQL questions and your channel will definitely grow.
@techTFQ
@techTFQ 2 жыл бұрын
Thank you :)
@hungphutr
@hungphutr 3 жыл бұрын
thank you for the tutorial, super helpful!. for the last problem, I think we can increase the complexity by having multiple order records within a day. by that way, each orderID is no longer a unique indicator of a day for us to calculate the difference. I think there will be just one extra step that is to get to the current table structure like we have in this video by creating a new table grouped by order_date and generating a new id column using row_number.
@IMHitesh
@IMHitesh 3 жыл бұрын
Thanks for solving this.... I was searching for the solution since so many days great effort... cheers
@techTFQ
@techTFQ 3 жыл бұрын
Your welcome Hitesh 🙏🏼 Glad this helped you..
@microvlog06
@microvlog06 Жыл бұрын
your video helped me in solving leetcode hard problem. Thankyou!😀
@SagarKumar-db2xy
@SagarKumar-db2xy 3 жыл бұрын
Trust me, you are the best. Please post more videos on interview questions for beginners.
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Sagar 🙏🏼 Very happy to know you like my content 🙂
@TheIndianSocial
@TheIndianSocial 8 ай бұрын
best explanation for the streak concept.
@sajidsshaikh707
@sajidsshaikh707 2 жыл бұрын
Yes, indeed your tutorials are really helpful man !!
@techTFQ
@techTFQ 2 жыл бұрын
Glad to hear that sajid!
@KrishnaYadav-ge5re
@KrishnaYadav-ge5re 2 жыл бұрын
I am very happy and excited to see more videos from you.. YOUR VIDEOS ARE AWESOME.. Great explainations
@tsukuruuu
@tsukuruuu Жыл бұрын
midway on the video , i immediately clicked on subscribe. this is a gem!
@techTFQ
@techTFQ 11 ай бұрын
Thank you
@girijashamak9142
@girijashamak9142 3 жыл бұрын
One more gem of a content
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Girija for such kind words 🙏🏼🙏🏼
@askarhameedka2953
@askarhameedka2953 3 жыл бұрын
I cleared one assessment with this code. thanks
@techTFQ
@techTFQ 3 жыл бұрын
That’s great to hear 😃 Congratulations 👏
@salmanbaig15
@salmanbaig15 2 жыл бұрын
Terrific explanation.. Thank you!
@siribn2657
@siribn2657 2 жыл бұрын
Super helpful tutorial, brilliantly explained. waiting for more of these kinds..
@techTFQ
@techTFQ 2 жыл бұрын
Thank you Siri 🙏🏼
@benoitpatigny
@benoitpatigny 3 жыл бұрын
Thank you for your awesome explanation, it's very useful, have a consecutive nice day
@techTFQ
@techTFQ 3 жыл бұрын
😃 Thank you Benoit 🙏🏼 Glad this helped ..
@nileshkadivar6281
@nileshkadivar6281 2 жыл бұрын
Very well explained about problem statement. Watched many videos regarding SQL on your channel and it's easy to understand. 👏👌
@techTFQ
@techTFQ 2 жыл бұрын
Thank you Nilesh
@GreatNishan
@GreatNishan 3 жыл бұрын
Nice video. I wish you post a continuation of this video by using “Preceding 1 and following 1” window clause and solving these questions.
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Nishan 🙏🏼 Glad you liked this video.. As for your suggestion, let me consider this and may be plan for a future video..
@tinurathi5210
@tinurathi5210 2 жыл бұрын
Thank you.. this is very helpful!
@sivagssri
@sivagssri 3 жыл бұрын
Wat to go... Such a crisp n clear explanation even for experience guy like me, got to see your channel and recommend to my group.
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Siva 🙏🏼 Happy to know you found some value from this video 🙂
@sonalivv
@sonalivv 3 жыл бұрын
This was brilliant! A perfect blend of math and SQL. Keep these videos coming!
@techTFQ
@techTFQ 3 жыл бұрын
Thank you so much 🙏🏼 So glad you find this useful 🙂
@swatisinha3230
@swatisinha3230 3 жыл бұрын
Very helpful. I used to struggle with this, but this explanation is worth watching :)
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Swati 🙏🏼 Glad this helped ..
@reddaiahreddymallu
@reddaiahreddymallu Жыл бұрын
Thank you, Thoufiq.
@fathimafarahna2633
@fathimafarahna2633 3 жыл бұрын
Amazing clarity and explanation👌 Keep up spreading knowledge. God bless 🙏
@techTFQ
@techTFQ 3 жыл бұрын
Thank you ❤️
@bibekrawat2284
@bibekrawat2284 2 жыл бұрын
Such a nice concept.
@techTFQ
@techTFQ 2 жыл бұрын
Thank you 🙏🏼
@flwi
@flwi 2 жыл бұрын
That is a very clever approach imho! Well done!
@purnamohan2991
@purnamohan2991 11 ай бұрын
very helpful sir, thanks a lot.
@KSudS_
@KSudS_ 3 жыл бұрын
Great job. Pls we need to tackle more sql queries. If you can handle 10 questions per video, will be awesome. thanks.
@techTFQ
@techTFQ 3 жыл бұрын
Thank you 🙏🏼 Sure will plan it out..
@piyushpathak7311
@piyushpathak7311 3 жыл бұрын
Sir we want more videos like this.. PLZZ upload..
@techTFQ
@techTFQ 3 жыл бұрын
Thank you for liking my content Piyush 🙏🏼 Will try my best to upload more such contents 🙏🏼☺️
@shoboyjr
@shoboyjr 2 жыл бұрын
This is really helpful! Thank you!
@karthikeyan_thangavel
@karthikeyan_thangavel 3 жыл бұрын
Very well helped at very critical situation... 🤗🥳🎊
@techTFQ
@techTFQ 3 жыл бұрын
Nice to know that .. glad this helped 😃
@zaraahmed1597
@zaraahmed1597 2 жыл бұрын
Loved the approach.Thanks for sharing :)
@techTFQ
@techTFQ 2 жыл бұрын
Your welcome 🙏🏼 Glad this helped
@TheRaju991
@TheRaju991 2 жыл бұрын
This was super helpful 👍
@MehmetEratli
@MehmetEratli Жыл бұрын
Thank you so much for the amazing content! Are you planning to do more videos like this or where you solve stratascratch problems? Those would also be super helpful to see how you approach problems
@srt5806
@srt5806 3 жыл бұрын
Crisply explained!
@techTFQ
@techTFQ 3 жыл бұрын
Thank you 🙏🏼
@hadireg
@hadireg 2 жыл бұрын
Thanks mate! Very useful indeed
@techTFQ
@techTFQ 2 жыл бұрын
Your welcome buddy
@shivsharma9153
@shivsharma9153 3 жыл бұрын
REALLY ENJOYING CHANNEL!!
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Shiv 🙏🏼 Glad you like the channel 🙂
@aneksingh4496
@aneksingh4496 3 жыл бұрын
Really good and nicely explained
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Anek 🙏🏼
@flaviusioan69
@flaviusioan69 3 жыл бұрын
Great tutorial ... Thnx
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Frisan 🙏
@prachijain5960
@prachijain5960 3 жыл бұрын
Informative video. Please share some videos on outliers
@techTFQ
@techTFQ 3 жыл бұрын
Thanks Prachi 🙏🏼 Let me consider your suggestion for a future video ..
@juhairahamed5342
@juhairahamed5342 2 жыл бұрын
Good explanation
@techTFQ
@techTFQ 2 жыл бұрын
Thank you :)
@mohammedshahil4898
@mohammedshahil4898 3 жыл бұрын
Really very helpful🙌🏻👌🏻 Thank you for this yet another amazing video✌️
@techTFQ
@techTFQ 3 жыл бұрын
Thank you hero 🙏🏼 I am glad to know you benefit from these videos 🙂😍
@hanweithoo5008
@hanweithoo5008 3 жыл бұрын
Tabibitosan method. Great stuff !
@techTFQ
@techTFQ 3 жыл бұрын
Thank you HanWei 🙏🏼 Glad you liked it ..
@ShivamTiwari-on2kl
@ShivamTiwari-on2kl Жыл бұрын
I had a different approach using lead/lag window functions. So I will basically check if the lead/lag values are same as the current row values (both +ve or -ve) and if they are, i will assign them same ids using case statement
@sachinpatel-oq3ib
@sachinpatel-oq3ib 3 жыл бұрын
learned something new 👍
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Sachin 🙏🏼 So glad I was able to teach you something new 🙂
@shafiquek9960
@shafiquek9960 3 жыл бұрын
Well explained.
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Shafique 🙏🏼 So glad you are liking my video 🙂
@santhoshkumarj4648
@santhoshkumarj4648 3 жыл бұрын
Genius 🔥🔥 pls post more videos
@techTFQ
@techTFQ 3 жыл бұрын
Thank you Santhosh and will do 🙏🏼
@sheebaparveen3630
@sheebaparveen3630 3 жыл бұрын
Great . Such good videos 🔥🔥💯💯💯
@techTFQ
@techTFQ 3 жыл бұрын
Thank you cheeb ❤️
@SudhirKumar-rl4wt
@SudhirKumar-rl4wt 2 жыл бұрын
Thanks for the tutorial ..in the last solution ..I think it's the order_date column that establishes the fact that two rows are consecutive because order_id is varchar and comparing one order_id with another order_id doesn't make a lot of sense..so row_number() should be over order_date and not over order_id.because our final ouput should say which orders(order_id) are consecutive..we shouldn't make that assumption.
@SudhirKumar-rl4wt
@SudhirKumar-rl4wt 2 жыл бұрын
Here Order_date should substitute ID(primary key) from first problem/solution
@shahinurrahman7745
@shahinurrahman7745 2 жыл бұрын
Awesome explanation! Just one query- instead of partition we can use group by - having clause also, right? Any particular reason to use partitioning? Thanks again for all of your wonderful videos!
@techTFQ
@techTFQ 2 жыл бұрын
Thank you 🙏🏼 Group by and partition by in window function are 2 completely different concepts.. this query is best solved using partition by .. Explaining the difference between these 2, cannot be done in comment but may need a separate video for it ..
@nawalambavkar7543
@nawalambavkar7543 3 жыл бұрын
Hi Thank you amazing videos Learnt a lot from you Can you please suggest some websites for practising SQL queries for data science
@techTFQ
@techTFQ 3 жыл бұрын
Thank you 🙏🏼 Not really sure if there is any website for SQL practice focused on data science but I would recommend using leetcode to practice SQL queries.. I’ve made a separate video about leetcode and how to find SQL questions on it. You can check that out on my channel
@divya001007
@divya001007 2 ай бұрын
Thank you so much for your best tutorials @thoufiq_sir , it was really an amazing explanation. Tqsm for ur efforts
@anudeepreddy5559
@anudeepreddy5559 10 ай бұрын
Superb Thoufiq ❤❤
@sandeepkumar-ol5lg
@sandeepkumar-ol5lg 2 жыл бұрын
Amazing explanation, by the way you’re using a tool or any online editor to run queries .
@techTFQ
@techTFQ 2 жыл бұрын
Thank you :) I use PostgreSQL database and PgAdmin tool to write queries
@bhavaniethirajan9627
@bhavaniethirajan9627 3 жыл бұрын
Hi.. first of all your way of explanation is awesome and easy understanding thanku for that.. one thing only couldn't understand like in ur example for pk ID u have given values as 1,2,3....n and row number will be 1,2,3....n so u can filter temperature less than zero n did subtraction between pk id and row number but what if pk id values will be starting like 2456,2457 n so on? How can we group this kinda values
@techTFQ
@techTFQ 3 жыл бұрын
Thank you for the kind feedback Bhavani 🙏🏼 Even if ID is any other numeric value (as long as it is unique) , the logic would work fine.. In case the ID is non numeric then you can explicitly create an ID column (using row number) as I mentioned in one of the examples..
@ameygoesgaming8793
@ameygoesgaming8793 7 ай бұрын
gold content
@munna1431000
@munna1431000 2 жыл бұрын
@techTFQ Is there a way to get dump of data or database which you walkthrough in your videos ?
@techTFQ
@techTFQ 2 жыл бұрын
Yes you can find it in my blog and I have provided the link to my blog in the video description
@sudhindrab1606
@sudhindrab1606 Жыл бұрын
alternate solution for the last problem: select * from (select * , count(*) over(partition by substring(order_date,0,8)) as no_of_records from orders)s where no_of_records =1
@AraSh-yn2tt
@AraSh-yn2tt 3 жыл бұрын
That was nice bro, helped a lot, please keep going. Btw, could you post a video about how to calculate l, for example, growth rate in one specific column through time? In general, how to perform mathematical operations on grouped columns
@techTFQ
@techTFQ 3 жыл бұрын
Thank you for the suggestion Ara.. Ill consider this for a future video..
@belikk1986
@belikk1986 2 жыл бұрын
Great tutorial! What if i have multiple records per one day and i still need to count quantity of consequtive days?
@vishalsvits
@vishalsvits 2 жыл бұрын
Hi , at 17:16 . over clause must have order by.
@MrManda007
@MrManda007 2 жыл бұрын
In the first case when you have an id and then do subtraction it can be done only in this dataset there is no math rule who will guarantee that subtraction id from the generated column will give you the numbers you get in your example. For example in my case id column is the same as generated column and the different field is always zero. 1- 1 =0 2-2=0 etc. You just find the way who will work on this dataset.
@sumitkumar-zv4xb
@sumitkumar-zv4xb Жыл бұрын
HEY TFQ, A little tweak in this problem how i can find the maximum consecutive days for which the temperature was less than 0.
@quantum634
@quantum634 3 жыл бұрын
Hei it's me once again, just saw you answer to my comment in your previous Video and I was hei that's so cool from him..1 second later I see, you already uploaded the video haha :) Thank you for making an extra tutorial explaing this ! Very well done ! My question is answered completely. Keep up the good work. :)
@techTFQ
@techTFQ 3 жыл бұрын
Hey … thank you so much 🙏🏼 I am so glad this video was able to answer your previous question 🙂 And thank you again for the feedback and for the appreciation 🙏🏼☺️
@mohammadshahbaz3287
@mohammadshahbaz3287 9 ай бұрын
@techTFQ one question - for the case #2 scenario - without primary key, if u assign row_number with any order by clause, then its possible u would end up getting random ordering & might get wrong result, right?
@pveeranjireddy8959
@pveeranjireddy8959 3 жыл бұрын
Great videos bro ... I subscribed channel...
@techTFQ
@techTFQ 3 жыл бұрын
Thank you 🙏🏼 So glad you liked the content 🙂
@harshitsinha4633
@harshitsinha4633 5 ай бұрын
21:28 in 3rd part, row number is itself an int how you converting if, (date- int )how it is working in date, we use datediff funct for difference
@databasewala6384
@databasewala6384 2 жыл бұрын
Can you please make a video to delete duplicate records without using rowid.
@nandhinisekar9689
@nandhinisekar9689 3 жыл бұрын
Awesome
@techTFQ
@techTFQ 3 жыл бұрын
Thank you 🙏🏼 Glad you liked it 🙂
@jasdeepsinghmonga4383
@jasdeepsinghmonga4383 2 жыл бұрын
How to find the difference in MySQL for last problem? Not able to do it using Cast
@priyanshu.tiwari
@priyanshu.tiwari 2 жыл бұрын
For Second Part, In MSSQL, this can be used: WITH T1 AS ( select *, ROW_NUMBER() OVER (ORDER BY order_id) AS RowNumber, DATEADD(day,-ROW_NUMBER() OVER (ORDER BY order_id),order_date) AS DIFF FROM orders ), T2 AS ( SELECT *,count(*) over(partition by DIFF) as no_of_records FROM T1 ) SELECT * FROM T2 WHERE no_of_records=3; Thank you :)
@akhilkishore7361
@akhilkishore7361 2 жыл бұрын
what if there are multiple cities and you need to get the temp group by each city
@praneethmuragani
@praneethmuragani Жыл бұрын
Hi bro! here is my solution select id, city, temperature, day from ( select *, lag(temperature) over(order by day asc) as lg, lead(temperature) over(order by day asc) as ld from weather ) tbl where temperature < 0 and (lg < 0 or ld < 0) and (lg is not null or ld is not null)
@vishalrana9594
@vishalrana9594 2 жыл бұрын
A very informative video....just have a small doubt in the second part in which we don't have a primary key and we are assigning row numbers twice...In the first part, we had primary key that was different to row number but in the second case, both sets will be the same... So id-Row_num will give output as 0 for all...Please correct me if I am wrong.
@gparvez6695
@gparvez6695 2 жыл бұрын
Hello, I am working in mysql work bench. I cam up with an issue that when I use cast function to convert the rownumber to int i.e (date - cast(row_number() over()) as int ) as diff. Could you please help me solve with this issue.
@Thesussyboi412
@Thesussyboi412 Жыл бұрын
Given the users purchase history write a query to print users who have done purchase on more than 1 day and products purchased on a given day are never repeated on any other day. Here is the ready script: create table purchase_history (userid int ,productid int ,purchasedate date ); SET DATEFORMAT dmy; insert into purchase_history values (1,1,'23-01-2012') ,(1,2,'23-01-2012') ,(1,3,'25-01-2012') ,(2,1,'23-01-2012') ,(2,2,'23-01-2012') ,(2,2,'25-01-2012') ,(2,4,'25-01-2012') ,(3,4,'23-01-2012') ,(3,1,'23-01-2012') ,(4,1,'23-01-2012') ,(4,2,'25-01-2012'); Can you provide the answer for above query?
@jackdesparrow4783
@jackdesparrow4783 Жыл бұрын
I have a doubt in 1st query. When there is 2 consecutive I'ds having t
@kasturipal5522
@kasturipal5522 2 жыл бұрын
Hi , I have been seriously following your blog , but the solution is not working for me when solving leetcode 180 prob .With t1 as (select * , row_number() over(order by id) as rn, id - (row_number() over(order by id)) as difference from Logs ) , t2 as (select *, count(*) over(partition by difference) as no_of_records from t1) select num as ConsecutiveNums from t2 where no_of_records >= 3; Can you please check
@sql8758
@sql8758 2 жыл бұрын
Hi Thank you very much for the video. it's super helpful. I have one question, instead of create a new temporary table t2, can we just use t1 and GROUP BY? For example in the last example, after creating t1, I will just write: 'SELECT * FROM t1 GROUP BY no_of_records HAVING COUNT(*)=3' Does this work?
@guchhusworld587
@guchhusworld587 Жыл бұрын
select * with a group by wont work
@arnabdas4056
@arnabdas4056 Жыл бұрын
How can the logic work I didn't get it consider a date 2022-01-04 and I'd value 4 and temp say -1and I have to determine 4 consecutive days.it will break the entire logic.
@shr_ee._________
@shr_ee._________ Жыл бұрын
sir , how to find out customers who did orders in consecutive months ? when customer_id , order_id and order_date is given ?? please reply me .. i really need help ..
@MrRajat769
@MrRajat769 Жыл бұрын
How to find last 12 months sales from last visit date
@unknown-ph4yb
@unknown-ph4yb 2 жыл бұрын
I have a query can u explain this please
@techTFQ
@techTFQ 2 жыл бұрын
Sorry but I cannot assist anyone personally due to my time constraints
@akhilgupta513
@akhilgupta513 3 жыл бұрын
For the solutions where you are relying on PK (ID/OrderId) to be in asc order to do the maths. Let's say PK (ID /Order ID) is not already sorted? (Except VW , where date is not given)
@阿哲玩家
@阿哲玩家 3 жыл бұрын
you can create another row_number in asc number as temp column and use it to do the remaining maths.
@hajarigupta121
@hajarigupta121 Ай бұрын
hello sir , i m recently started my MYSQL learning and following your learning but some where i get confuse regarding the query for cast the text in integer as it showing error and i m trying to do the same with CTE clause and join but i m not getting proper solution , so please guide me sir. question is : -- Finding n consecutive records with consecutive date value. i have try by this : create table dates( order_id varchar(20) primary key, order_date date); insert into dates values ('ORD1001', '2021-01-01'), ('ORD1002', '2021-02-01'), ('ORD1003', '2021-03-02'), ('ORD1004', '2021-02-03'), ('ORD1005', '2021-03-01'), ('ORD1006', '2021-06-01'), ('ORD1007', '2021-12-25'), ('ORD1008', '2021-12-26'); select * from dates; with Table1 as( select *,row_number()over(order by order_date)as id from dates ), demo as (select T1.order_id,T1.order_date from Table1 T1 join Table1 T2 on T1.id = T2.id+1 and T1.order_date =T2. order_date +interval 1 day ) select * from demo ; your solution : with t1 as (select *, row_number() over(order by order_date) as rn, order_date - cast(row_number() over(order by order_date)::numeric as int) as diff from dates), t2 as (select *, count(1) over (partition by diff) as cnt from t1) select order_id, order_date from t2 where cnt >= 3;
@asifrehman8837
@asifrehman8837 3 жыл бұрын
What if after 6th primary key number, again there is another 3 consecutive records?
@techTFQ
@techTFQ 3 жыл бұрын
this query would capture any no of consecutive records.. Let me know if you have a example where this would not work..
@snehalsanap1750
@snehalsanap1750 2 жыл бұрын
hi, while doing this 3rd part where only date is given both in mysql and ssms the cast operation over id and subtraction of it with date is giving error... operand type
@snehalsanap1750
@snehalsanap1750 2 жыл бұрын
select *, row_number() over(order by order_id) as rn, order_date - cast(row_number() over(order by order_id) as int) as difference from orders; error Operand type clash: date is incompatible with int - both in msql workbench and ssms.. how to solve? @ techTFQ
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 58 МЛН
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2 МЛН
Learn how to write SQL Queries(Practice Complex SQL Queries)
49:56
Solving SQL Query | Rows to Column in SQL
1:13:11
techTFQ
Рет қаралды 177 М.
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 58 МЛН