SQL Tutorial - Window Functions - Lag and Lead

Sdílet
Vložit
  • čas přidán 11. 09. 2024

Komentáře • 87

  • @surajvissa5428
    @surajvissa5428 Před 3 lety +1

    Great stuff, BeardedDev. Huge fan of your channel. Kudos.

  • @muhammadmehmood2084
    @muhammadmehmood2084 Před 4 lety +10

    you've got a knack for simplifying things; keeping them interesting, concise and to the point. Brilliant !

  • @houstonvanhoy7767
    @houstonvanhoy7767 Před rokem +1

    Hello, fellow viewers.
    If you will study window functions and practice them again and again until you have them cold, you'll be far ahead of most of your co-workers and fellow job seekers. I've been seeing numerous job postings requiring the ability to create SQL table joins, but never the ability to use SQL Server window functions - so far, at least. I can only assume that the hiring managers don't want to scare off people, or that they are not familiar with the topic. ** (Your thoughts are welcome - especially you, Brad.) It does take a while to sink into my thick skull, but with repetition I am making progress. It takes a while to soften up the rock, I suppose.
    Brad's step-wise instructional approach is excellent, and this channel - and specifically this series - is the best online training that I have seen for SQL Server window functions. 👍
    ** Similarly, job postings often specify Excel VLOOKUP, and never specify Excel XLOOKUP, or even INDEX MATCH. Obviously, a lot of time is being wasted out there by not using the newer capabilities of software. Inertia?

  • @dannyezechukwu1175
    @dannyezechukwu1175 Před rokem +2

    Great video BeardedDev! Very clear and concise!

  • @sarahzimmer891
    @sarahzimmer891 Před rokem +1

    Learning SQL, truly helped me understand Lag and Lead. Thank you!

  • @victoriawillie
    @victoriawillie Před rokem +1

    Such a helpful video. Thank you!

  • @ashwin3475
    @ashwin3475 Před rokem +1

    Clean and clear❤️

  • @vocabguru7224
    @vocabguru7224 Před 4 lety +1

    Excellent work! Things are just crystal clear after watching this. Keep posting more.

  • @MrAlfadark
    @MrAlfadark Před 2 lety +1

    Thank you so much mate, this helped me a lot when I was trying to calculate the count days since the last price change. Greetings from Chile!

  • @wildslots2373
    @wildslots2373 Před 2 lety +1

    Thanks alot from bottom of my heart

  • @Jalalx
    @Jalalx Před 5 lety +2

    Never heard of those functions! Thank you!

  • @dinhtran9249
    @dinhtran9249 Před 5 lety +1

    Thank you for an awesome explanation, windows function has never been so clear too me like this. Thanks a lot.

  • @edwarddizengoff8414
    @edwarddizengoff8414 Před 2 lety +1

    Thank you so much for this great video!

  • @rafaelge
    @rafaelge Před rokem +1

    Amazing, thanks!

  • @sanyammadaan2396
    @sanyammadaan2396 Před 4 lety +1

    Great video series! Keep the good work going

  • @johnbreslin4521
    @johnbreslin4521 Před 2 lety +1

    Brilliant explanation and easy to follow

  • @huanpinghuang1256
    @huanpinghuang1256 Před 2 lety +1

    Thank you so much!! for good content

  • @roywit7836
    @roywit7836 Před 3 lety +1

    very helpful video and luxury of having table prepped
    subbed and will follow thanks :)

  • @abiydelelew5771
    @abiydelelew5771 Před 3 lety +1

    Great stuff, thanks again!

  • @murtymvvs
    @murtymvvs Před 3 lety +1

    Appreciate your structured and detailed approach. Could you plz make a video calculating ratios in windows, using self joins

  • @jb4928
    @jb4928 Před 3 lety +1

    Amazing content !!! Please do keep creating SQL videos like this.

    • @BeardedDevData
      @BeardedDevData  Před 3 lety +1

      Don't worry, I'm currently working on my Power BI certification then I will be back posting videos regularly.

    • @jb4928
      @jb4928 Před 3 lety +1

      @@BeardedDevData One of the best content on CZcams for Windows functions. I am going to go over all the videos because its always something new I learned from your videos.

    • @houstonvanhoy7767
      @houstonvanhoy7767 Před rokem

      @@BeardedDevData Please let us know HOW you are studying for your Power BI certification. Thank you.

  • @valentindelarosa76
    @valentindelarosa76 Před 5 lety +1

    Thank you so much, greetings from Colombia.

  • @srv14evr
    @srv14evr Před 5 lety +1

    You are an awesome teacher.

    • @BeardedDevData
      @BeardedDevData  Před 5 lety

      Thanks for the feedback. Let me know if there are any particular areas you are interested in.

  • @jochemcode4570
    @jochemcode4570 Před 5 lety +1

    I am watching this one day before my 70-461 and it's so much clearer now

    • @BeardedDevData
      @BeardedDevData  Před 5 lety +1

      Great stuff, good luck with the certification

    • @jochemcode4570
      @jochemcode4570 Před 5 lety +1

      @@BeardedDevData thank you!

    • @BeardedDevData
      @BeardedDevData  Před 5 lety +1

      Let me know how you get on and what you thought of the exam, would be good to give you a shoutout on the channel.

    • @jochemcode4570
      @jochemcode4570 Před 5 lety +1

      @@BeardedDevData I scored a 770! The video definitely helped understanding better. The exam was pretty hard for me, you've to sign an NDA before the exam so I've to lookup what I can say and not

    • @BeardedDevData
      @BeardedDevData  Před 5 lety +1

      Congratulations, yes, don’t reveal any questions. I meant more was it what you expected? Did you find some topics more difficult than others? Would you recommended it to people? Let me know if you want a shoutout.

  • @parthasarathypoovendran9270

    Great

  • @sk____1958
    @sk____1958 Před 3 lety +1

    thants a ton for increasing font size

  • @sabaamanollahi5901
    @sabaamanollahi5901 Před 2 lety +1

    perfect

  • @karinshamama5591
    @karinshamama5591 Před 3 lety +1

    Great! Thank you

  • @kumarpratap5115
    @kumarpratap5115 Před 3 lety +1

    really the best videos for studying analytical functions.... can u also make videos on cube and roll up functions?

  • @joyxplorers
    @joyxplorers Před 3 lety +2

    Hi BeardedDev, very helpful video. Can you explain what will be the result of lag and lead if the column of order by (sales_date in this case) has the same value for 2 rows?

    • @BeardedDevData
      @BeardedDevData  Před 3 lety

      that's a really good question, let's say we have the following data, id, date, amount, 1, 21/05/2021, 10.00, 2, 21/05/2021, 20.00, 3, 21/05/2021, 30.00, 4, 21/05/2021, 40.00, if I run the following query SELECT *, LAG(Amount) OVER(ORDER BY Date) AS R FROM [table] then what we end up with is a nondeterministic result meaning each time we run the query we may get different results but they would still be considered correct, when I ran the query I got the following id, date, amount, r, 1, 21/05/2021, 10.00, NULL, 2, 21/05/2021, 20.00, 10.00, 3, 21/05/2021, 30.00, 20.00, 4, 21/05/2021, 40.00, 30.00 but I could have got different results and it would have been correct, there are two important things, lag will not apply grouping, you will have to do that yourself and you should apply deterministic ordering, in this case you can use a tiebreaker like id, hopefully that helps

    • @joyxplorers
      @joyxplorers Před 3 lety +1

      @@BeardedDevData So basically you're saying to have multiple order by fields in such cases to make the sorting unique. Got it.. Thanks!

  • @lizbethl4
    @lizbethl4 Před 6 lety +1

    Thanks for the video on lag and lead. How to use function to see the difference between the succeeding value using lag?
    If you wanted to see difference between the sales amount of each row.

    • @BeardedDevData
      @BeardedDevData  Před 6 lety

      Hi lizbethl4, that's a great question, I will post a video shortly on how to calculate the difference.

    • @BeardedDevData
      @BeardedDevData  Před 6 lety

      Hi lizbethl4, I have just uploaded a video on calculating running totals: czcams.com/video/qyjxSzVE5eU/video.html

  • @ChrisPTY507
    @ChrisPTY507 Před 6 lety +1

    I am a web developer with 3 years of experience. Days ago I got chosen to a Jr BI Job consisting mainly on SQL and reports. Tell me your experience and opinión about it please., I read you were also a web developer years ago before transitioning into BI

    • @BeardedDevData
      @BeardedDevData  Před 6 lety

      May I ask where you read that I used to be a Web Developer?

  • @CaribouDataScience
    @CaribouDataScience Před 2 lety +1

    Can I do something like sale_amount - LAG(sales_amount)?

    • @BeardedDevData
      @BeardedDevData  Před 2 lety +1

      Absolutely, commonly done for period over previous period calculations e.g. month over previous month either as a value or a percentage.

    • @CaribouDataScience
      @CaribouDataScience Před 2 lety +1

      @@BeardedDevData I watched your video covering this after I posted this question.

  • @garglucky
    @garglucky Před 5 lety +1

    thank you!!

  • @ghoul3227
    @ghoul3227 Před 3 lety +1

    Men this is so helpful, but can you tell me how the heck you could create that window frame. I am 3 days trying to solve it but i cant. Could you help me please (im using mysql)

    • @BeardedDevData
      @BeardedDevData  Před 2 lety

      It should work in mysql without a problem, can you let me know what query tool you are using and the query you are trying to run.

  • @rakeshkumarsahoo9552
    @rakeshkumarsahoo9552 Před 5 lety

    Please if you can share an video on list_aggregate function please.. Your videos are very helpful in understanding

  • @haneulkim4902
    @haneulkim4902 Před 3 lety +1

    Thanks! Lag and lead only works with windowing functions? Is there a way to use them on single col other than using ot like 'lag(column) over()'

    • @BeardedDevData
      @BeardedDevData  Před 3 lety +1

      You can use without a partition by but you must specify an order by however you can overcome this by using the following statement lag(col) over(order by (select null)), but usually it’s best to order by a column that means something when working with lag and lead.

    • @haneulkim4902
      @haneulkim4902 Před 3 lety +1

      @@BeardedDevData Thanks! Amazing tutorial btw

  • @llatyntseva
    @llatyntseva Před 3 lety +1

    Thank you!
    Is there a video of how to calculate Quartile (1 to 4) ?

    • @BeardedDevData
      @BeardedDevData  Před 3 lety +1

      Yes, you can have a look at my video on NTILE, m.czcams.com/video/DS8QZ2HEqvk/video.html

    • @llatyntseva
      @llatyntseva Před 3 lety +1

      @@BeardedDevData thank you :)

  • @shreddersengupta7384
    @shreddersengupta7384 Před 3 lety +1

    Great work :D , why dont you teach at Udemy or other paid portals too ?

    • @BeardedDevData
      @BeardedDevData  Před 3 lety +1

      I have thought about it and it might be something I pursue next year.

    • @shreddersengupta7384
      @shreddersengupta7384 Před 3 lety +1

      @@BeardedDevData That will be great , thanks for helping new comers :)

  • @nishantkumar-lw6ce
    @nishantkumar-lw6ce Před 5 lety +1

    Hi can you explain me the query if I had to use with self join without lag ? I will be joining on customer id and mentioned t1.salesdate < t2.salesdate?

    • @BeardedDevData
      @BeardedDevData  Před 5 lety

      Hi Nishant, you have explained how to perform the self join on customerid and saledate, can you give me a bit more detail on what you are asking?

    • @nishantkumar-lw6ce
      @nishantkumar-lw6ce Před 5 lety

      Hi I’m trying to run the same query that you’ve done with lag using self join

    • @BeardedDevData
      @BeardedDevData  Před 5 lety

      The problem with that is you have a one to many join, the max date in the table will be joined to all other dates. To make this one to one you would also need to have a greater than, if you don’t have another date column to use you could use the DATEADD function. This would assume you have consecutive dates for every customer though otherwise it would be a lot more difficult.

  • @master-azazel
    @master-azazel Před 3 lety +1

    thanks :) my exam is tomorrow

  • @MrCliffdog13
    @MrCliffdog13 Před 2 lety

    does this work for mysql on MacBook?

    • @BeardedDevData
      @BeardedDevData  Před 2 lety

      It won't matter that you are using a Mac Book. They should be supported on mysql but if you're using quite an old version they may not work.

  • @kirknicholsonofficial
    @kirknicholsonofficial Před 6 lety

    Hi, Thanks for the AWESOME tutorial! Howver, I am having a bit of trouble.
    I am able to bring back LIST successfully, but I need to bring back the prev/next for one ItemID, but I cannot get anything but NULL values (even though I can confirm that the previous and next record, have a value)
    What am i missing? Here is my code...
    SELECT
    ItemID
    , ItemModelID
    , LAG(ItemID) OVER(ORDER BY ItemYear) AS PrevValue
    , ItemID
    , LEAD(ItemID) OVER(ORDER BY ItemYear) AS NextValue
    FROM dbo.item_info
    WHERE ItemID = 320696
    Thanks for any help!

    • @BeardedDevData
      @BeardedDevData  Před 6 lety

      Hi, thanks for the question, I have noticed you have a WHERE clause in your query and it filters on an ID column so I’m guessing only returns one row. As window functions are part of the SELECT they are calculated after filtering is applied therefore correct to return NULL because there is nothing above or below.

    • @kirknicholsonofficial
      @kirknicholsonofficial Před 6 lety

      @@BeardedDevData That makes perfect sense :) So is there any way to get a single record response, with the prev / next included?
      I am using this on a ASP page, and yesterday as a solution I used an IF statement to CHOOSE the ItemID from the record set on the actual page instead of the WHERE in the SQL, so that worked. But I feel like it is not very efficient because on some pages I will be pulling back hundreds of records in the background just to get the prev/next for the single item.

    • @BeardedDevData
      @BeardedDevData  Před 6 lety

      You will need to first run the query without the WHERE clause and then apply the filter to the result set. Depending on the size of the data I would probably put it within a CTE.

    • @kirknicholsonofficial
      @kirknicholsonofficial Před 6 lety

      This is a bit outside my experience level as I am transitioning to more SQL stored procedures using less ASP... I appreciate your quick replies! Would you be able to show me a bit of an example using my previous code? (apologies if this is asking too much)

    • @BeardedDevData
      @BeardedDevData  Před 6 lety

      That’s okay, I’m happy to help, using a CTE the query would be: WITH CTE AS (SELECT ItemID, ItemModelID, LAG(ItemID) OVER(ORDER BY ItemYear) AS PrevValue, LEAD(ItemID) OVER(ORDER BY ItemYear) AS NextValue FROM dbo.item_info) SELECT ItemID, ItemModelID, PrevValue, NextValue FROM CTE WHERE ItemID = 320696, apologies for the spacing, I’m on the CZcams app and doesn’t always work correctly.