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

Поделиться
HTML-код
  • Опубликовано: 25 июл 2024
  • Practice Complex SQL Queries | SQL Query to fetch N consecutive records
    In this video, I explain how to write a complex SQL query which is to fetch N consecutive records from a table based on some condition. We see how to write a generic SQL Query to fetch any number of consecutive records from a table.
    We see 3 different variations of writing this query:
    1) How to write this query when the table has a primary key column
    2) How to write this query when the table DOES NOT have a primary key column
    3) How to write this query based on Date field value.
    All the queries and scripts mentioned in this video are posted on my blog. You can download them for free from below link:
    techtfq.com/blog/learn-how-to...
    Timestamp:
    00:00 Intro
    01:08 SQL Query to fetch N consecutive records from a table which has a primary key
    14:52 SQL Query to fetch N consecutive records from a table which does not have a primary key
    18:46 SQL Query to fetch N consecutive records from a table based on date field
    🔴 WATCH MORE VIDEOS HERE 👇
    ✅ SQL Tutorial - Basic concepts:
    • SQL Tutorial - Basic c...
    ✅ SQL Tutorial - Intermediate concepts:
    • SQL Tutorial - Interme...
    ✅ SQL Tutorial - Advance concepts:
    • SQL Tutorial - Advance...
    ✅ Practice Solving Basic SQL Queries:
    • Practice Solving BASIC...
    ✅ Practice Solving Intermediate SQL Queries:
    • Practice Solving INTER...
    ✅ Practice Solving Complex SQL Queries:
    • Practice Solving COMPL...
    ✅ Data Analytics Career guidance:
    • Data Analytics career ...
    ✅ SQL Course, SQL Training Platform Recommendations:
    • SQL Course / Training
    ✅ Python Tutorial:
    • Python Tutorial
    THANK YOU,
    Thoufiq

Комментарии • 189

  • @brianligat2038
    @brianligat2038 Год назад +2

    Have been doing SQL for years, but your clips are still very informative. Thanks.

  • @arihantdhadiwal9115
    @arihantdhadiwal9115 2 года назад +27

    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  2 года назад +2

      Thank you Arihant 🙏🏼
      Am so glad to hear you liked my contents 🙂

    • @joe2271
      @joe2271 2 года назад +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

  • @sonalivv
    @sonalivv 2 года назад +4

    This was brilliant! A perfect blend of math and SQL. Keep these videos coming!

    • @techTFQ
      @techTFQ  2 года назад

      Thank you so much 🙏🏼
      So glad you find this useful 🙂

  • @KrishnaYadav-ge5re
    @KrishnaYadav-ge5re 2 года назад

    I am very happy and excited to see more videos from you.. YOUR VIDEOS ARE AWESOME.. Great explainations

  • @hungphutr
    @hungphutr 2 года назад +5

    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.

  • @abhilashs3160
    @abhilashs3160 2 года назад +4

    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

  • @flwi
    @flwi 2 года назад

    That is a very clever approach imho! Well done!

  • @karthikd2104
    @karthikd2104 2 года назад +3

    Clear explanation ! Easy way to understand!

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Karthik 🙏🏼
      Glad you liked it ..

  • @MohdSalmanBaig
    @MohdSalmanBaig 2 года назад

    Terrific explanation.. Thank you!

  • @vikranttyagi5232
    @vikranttyagi5232 2 года назад +2

    All of your SQL videos are really helpful to understand the advanced concepts. Thanks for making them.

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Vikrant :)

  • @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

  • @pavangsk8404
    @pavangsk8404 11 месяцев назад

    you have touth a very important problem in awesome way... brillliant sir

  • @arindamnath1233
    @arindamnath1233 2 года назад +1

    The best explanation for consecutive record fetching I ever get. Thank you

    • @techTFQ
      @techTFQ  2 года назад

      Glad you think so 🙏🏼

  • @shoboyjr
    @shoboyjr Год назад

    This is really helpful! Thank you!

  • @bhuwanthapliyal7977
    @bhuwanthapliyal7977 2 года назад +8

    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  2 года назад +1

      Thank you Bhuwan.. glad you like it 🙏🏼
      Yes I will make detailed SQL tutorial videos in the near future..

  • @sajidsshaikh707
    @sajidsshaikh707 2 года назад +2

    Yes, indeed your tutorials are really helpful man !!

    • @techTFQ
      @techTFQ  2 года назад

      Glad to hear that sajid!

  • @swatisinha3230
    @swatisinha3230 2 года назад +2

    Very helpful. I used to struggle with this, but this explanation is worth watching :)

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Swati 🙏🏼
      Glad this helped ..

  • @tinurathi5210
    @tinurathi5210 2 года назад

    Thank you.. this is very helpful!

  • @TheIndianSocial
    @TheIndianSocial 4 месяца назад

    best explanation for the streak concept.

  • @reddaiahreddymallu
    @reddaiahreddymallu Год назад

    Thank you, Thoufiq.

  • @sivagssri
    @sivagssri 2 года назад +2

    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  2 года назад

      Thank you Siva 🙏🏼
      Happy to know you found some value from this video 🙂

  • @siribn2657
    @siribn2657 2 года назад +1

    Super helpful tutorial, brilliantly explained. waiting for more of these kinds..

    • @techTFQ
      @techTFQ  2 года назад +1

      Thank you Siri 🙏🏼

  • @purnamohan2991
    @purnamohan2991 7 месяцев назад

    very helpful sir, thanks a lot.

  • @fathimafarahna2633
    @fathimafarahna2633 2 года назад +2

    Amazing clarity and explanation👌 Keep up spreading knowledge. God bless 🙏

    • @techTFQ
      @techTFQ  2 года назад

      Thank you ❤️

  • @vikaskumar-qr5tj
    @vikaskumar-qr5tj 2 года назад +2

    Beautiful explanation just crystal clear.....

    • @techTFQ
      @techTFQ  2 года назад

      Thank you vikas

  • @IMHitesh
    @IMHitesh 2 года назад +2

    Thanks for solving this.... I was searching for the solution since so many days
    great effort... cheers

    • @techTFQ
      @techTFQ  2 года назад

      Your welcome Hitesh 🙏🏼
      Glad this helped you..

  • @user-nh8bg5po6v
    @user-nh8bg5po6v 11 месяцев назад

    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

  • @InvisibleDivide
    @InvisibleDivide Год назад

    Solution is so simple yet so big brain at the same time.

  • @mananshah7095
    @mananshah7095 2 года назад +3

    You are one of the best SQL instructor on RUclips. 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 :)

  • @mohammedshahil4898
    @mohammedshahil4898 2 года назад +1

    Really very helpful🙌🏻👌🏻 Thank you for this yet another amazing video✌️

    • @techTFQ
      @techTFQ  2 года назад

      Thank you hero 🙏🏼
      I am glad to know you benefit from these videos 🙂😍

  • @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.

  • @nileshkadivar6281
    @nileshkadivar6281 2 года назад +1

    Very well explained about problem statement. Watched many videos regarding SQL on your channel and it's easy to understand. 👏👌

    • @techTFQ
      @techTFQ  2 года назад +1

      Thank you Nilesh

  • @ashabhumza3394
    @ashabhumza3394 2 года назад +2

    Mashallah! You are really an SQL genius... 👍

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Ashab ☺️

  • @microvlog06
    @microvlog06 Год назад

    your video helped me in solving leetcode hard problem. Thankyou!😀

  • @hadireg
    @hadireg 2 года назад +1

    Thanks mate! Very useful indeed

    • @techTFQ
      @techTFQ  2 года назад

      Your welcome buddy

  • @tsukuruuu
    @tsukuruuu 8 месяцев назад

    midway on the video , i immediately clicked on subscribe. this is a gem!

    • @techTFQ
      @techTFQ  7 месяцев назад

      Thank you

  • @TheRaju991
    @TheRaju991 2 года назад

    This was super helpful 👍

  • @benoitpatigny
    @benoitpatigny 2 года назад +2

    Thank you for your awesome explanation, it's very useful, have a consecutive nice day

    • @techTFQ
      @techTFQ  2 года назад +1

      😃
      Thank you Benoit 🙏🏼
      Glad this helped ..

  • @zaraahmed1597
    @zaraahmed1597 2 года назад +1

    Loved the approach.Thanks for sharing :)

    • @techTFQ
      @techTFQ  2 года назад

      Your welcome 🙏🏼
      Glad this helped

  • @reigngamer94
    @reigngamer94 2 года назад +1

    Complex query explained in the best way

    • @techTFQ
      @techTFQ  2 года назад

      Thank you :)

  • @SagarKumar-db2xy
    @SagarKumar-db2xy 2 года назад +2

    Trust me, you are the best. Please post more videos on interview questions for beginners.

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Sagar 🙏🏼
      Very happy to know you like my content 🙂

  • @girijashamak9142
    @girijashamak9142 2 года назад +2

    One more gem of a content

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Girija for such kind words 🙏🏼🙏🏼

  • @bibekrawat2284
    @bibekrawat2284 2 года назад +1

    Such a nice concept.

    • @techTFQ
      @techTFQ  2 года назад +1

      Thank you 🙏🏼

  • @GreatNishan
    @GreatNishan 2 года назад +2

    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  2 года назад +1

      Thank you Nishan 🙏🏼
      Glad you liked this video..
      As for your suggestion, let me consider this and may be plan for a future video..

  • @karthikeyan_thangavel
    @karthikeyan_thangavel 2 года назад +1

    Very well helped at very critical situation... 🤗🥳🎊

    • @techTFQ
      @techTFQ  2 года назад +1

      Nice to know that .. glad this helped 😃

  • @KSudS_
    @KSudS_ 2 года назад +2

    Great job. Pls we need to tackle more sql queries. If you can handle 10 questions per video, will be awesome. thanks.

    • @techTFQ
      @techTFQ  2 года назад

      Thank you 🙏🏼
      Sure will plan it out..

  • @anudeepreddy5559
    @anudeepreddy5559 6 месяцев назад

    Superb Thoufiq ❤❤

  • @sheebaparveen3630
    @sheebaparveen3630 2 года назад +1

    Great . Such good videos 🔥🔥💯💯💯

    • @techTFQ
      @techTFQ  2 года назад

      Thank you cheeb ❤️

  • @AraSh-yn2tt
    @AraSh-yn2tt 2 года назад +2

    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  2 года назад +1

      Thank you for the suggestion Ara.. Ill consider this for a future video..

  • @shivsharma9153
    @shivsharma9153 2 года назад +1

    REALLY ENJOYING CHANNEL!!

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Shiv 🙏🏼
      Glad you like the channel 🙂

  • @askarhameedka2953
    @askarhameedka2953 2 года назад +1

    I cleared one assessment with this code. thanks

    • @techTFQ
      @techTFQ  2 года назад

      That’s great to hear 😃
      Congratulations 👏

  • @flaviusioan69
    @flaviusioan69 2 года назад +1

    Great tutorial ... Thnx

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Frisan 🙏

  • @ameygoesgaming8793
    @ameygoesgaming8793 3 месяца назад

    gold content

  • @hanweithoo5008
    @hanweithoo5008 2 года назад +1

    Tabibitosan method. Great stuff !

    • @techTFQ
      @techTFQ  2 года назад

      Thank you HanWei 🙏🏼
      Glad you liked it ..

  • @juhairahamed5342
    @juhairahamed5342 2 года назад +1

    Good explanation

    • @techTFQ
      @techTFQ  2 года назад

      Thank you :)

  • @shafiquek9960
    @shafiquek9960 2 года назад +1

    Well explained.

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Shafique 🙏🏼
      So glad you are liking my video 🙂

  • @sachinpatel-oq3ib
    @sachinpatel-oq3ib 2 года назад +1

    learned something new 👍

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Sachin 🙏🏼
      So glad I was able to teach you something new 🙂

  • @santhoshkumarj4648
    @santhoshkumarj4648 2 года назад +1

    Genius 🔥🔥 pls post more videos

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Santhosh and will do 🙏🏼

  • @aneksingh4496
    @aneksingh4496 2 года назад +1

    Really good and nicely explained

    • @techTFQ
      @techTFQ  2 года назад

      Thank you Anek 🙏🏼

  • @shahinurrahman7745
    @shahinurrahman7745 2 года назад +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 года назад +3

      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 ..

  • @srt5806
    @srt5806 2 года назад +1

    Crisply explained!

    • @techTFQ
      @techTFQ  2 года назад

      Thank you 🙏🏼

  • @piyushpathak7311
    @piyushpathak7311 2 года назад +3

    Sir we want more videos like this.. PLZZ upload..

    • @techTFQ
      @techTFQ  2 года назад

      Thank you for liking my content Piyush 🙏🏼
      Will try my best to upload more such contents 🙏🏼☺️

  • @prachijain5960
    @prachijain5960 2 года назад +1

    Informative video. Please share some videos on outliers

    • @techTFQ
      @techTFQ  2 года назад

      Thanks Prachi 🙏🏼
      Let me consider your suggestion for a future video ..

  • @pveeranjireddy8959
    @pveeranjireddy8959 2 года назад +1

    Great videos bro ...
    I subscribed channel...

    • @techTFQ
      @techTFQ  2 года назад +1

      Thank you 🙏🏼
      So glad you liked the content 🙂

  • @nandhinisekar9689
    @nandhinisekar9689 2 года назад

    Awesome

    • @techTFQ
      @techTFQ  2 года назад

      Thank you 🙏🏼
      Glad you liked it 🙂

  • @belikk1986
    @belikk1986 Год назад

    Great tutorial! What if i have multiple records per one day and i still need to count quantity of consequtive days?

  • @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

  • @nawalambavkar7543
    @nawalambavkar7543 2 года назад +2

    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  2 года назад +1

      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

  • @sandeepkumar-ol5lg
    @sandeepkumar-ol5lg 2 года назад +1

    Amazing explanation, by the way you’re using a tool or any online editor to run queries .

    • @techTFQ
      @techTFQ  2 года назад +1

      Thank you :)
      I use PostgreSQL database and PgAdmin tool to write queries

  • @vishalsvits
    @vishalsvits Год назад

    Hi , at 17:16 . over clause must have order by.

  • @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.

  • @bhavaniethirajan9627
    @bhavaniethirajan9627 2 года назад +2

    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  2 года назад

      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..

  • @munna1431000
    @munna1431000 2 года назад +1

    @techTFQ Is there a way to get dump of data or database which you walkthrough in your videos ?

    • @techTFQ
      @techTFQ  2 года назад +1

      Yes you can find it in my blog and I have provided the link to my blog in the video description

  • @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

  • @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.

  • @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.

  • @akhilkishore7361
    @akhilkishore7361 Год назад

    what if there are multiple cities and you need to get the temp group by each city

  • @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?

  • @quantum634
    @quantum634 2 года назад +1

    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  2 года назад

      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 5 месяцев назад

    @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?

  • @harshitsinha4633
    @harshitsinha4633 Месяц назад

    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.

  • @jasdeepsinghmonga4383
    @jasdeepsinghmonga4383 2 года назад

    How to find the difference in MySQL for last problem? Not able to do it using Cast

  • @jackdesparrow4783
    @jackdesparrow4783 Год назад

    I have a doubt in 1st query. When there is 2 consecutive I'ds having t

  • @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.

  • @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

  • @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.

  • @unknown-ph4yb
    @unknown-ph4yb 2 года назад +1

    I have a query can u explain this please

    • @techTFQ
      @techTFQ  2 года назад

      Sorry but I cannot assist anyone personally due to my time constraints

  • @MrRajat769
    @MrRajat769 8 месяцев назад

    How to find last 12 months sales from last visit date

  • @praneethmuragani
    @praneethmuragani 9 месяцев назад

    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)

  • @devcents
    @devcents 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 :)

  • @shr_ee._________
    @shr_ee._________ 10 месяцев назад

    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 ..

  • @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

  • @asifrehman8837
    @asifrehman8837 2 года назад +1

    What if after 6th primary key number, again there is another 3 consecutive records?

    • @techTFQ
      @techTFQ  2 года назад

      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

  • @shubhampandhare8866
    @shubhampandhare8866 2 года назад +1

    Just out of curiosity
    What if the difference of 1 consecutive set and 1 individual date difference will be same
    In that case it will be partition by the same difference where this individual date difference may get ignored
    Please correct me if am not able to ask this properly
    Thanks for your valuable time

    • @techTFQ
      @techTFQ  2 года назад

      Sorry bro.. I am not clear on what you asked..
      Can you please elaborate?

  • @mominadil2279
    @mominadil2279 2 года назад

    Hello, Thank you for your help,
    I hope you can help me out sir,
    I have 2 tables linked with user_id.
    Table 1: user_id, name, Email, Phone
    Table 2: id, user_id(from table 1), relative_name, relative_email, relative_phone
    I created a query to list all `name` from table 1 into a dropdown list.
    I am trying to make another column where data gets automatically populated from the corresponding name selected from table 1
    For example:
    Selected name A from dropdown list and want to show corresponding relative_name into another column based on the name I selected from Table 1.
    I hope you can help me out.
    Thanks in advance

  • @arunkmrgarg
    @arunkmrgarg 2 года назад

    18:47

  • @techxtreme7630
    @techxtreme7630 2 года назад +1

    hello sir
    can you help me for a query

    • @techTFQ
      @techTFQ  2 года назад

      sorry i do not provide personal support