SQL Tutorial - Window Functions - Lag and Lead

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024

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

  • @surajvissa5428
    @surajvissa5428 3 года назад +1

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

  • @houstonvanhoy7767
    @houstonvanhoy7767 Год назад +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?

  • @muhammadmehmood2084
    @muhammadmehmood2084 4 года назад +10

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

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

    Great video BeardedDev! Very clear and concise!

  • @sarahzimmer891
    @sarahzimmer891 Год назад +1

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

  • @victoriawillie
    @victoriawillie Год назад +1

    Such a helpful video. Thank you!

  • @ashwin3475
    @ashwin3475 Год назад +1

    Clean and clear❤️

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

    Thanks alot from bottom of my heart

  • @Jalalx
    @Jalalx 5 лет назад +2

    Never heard of those functions! Thank you!

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

    Thank you so much for this great video!

  • @rafaelge
    @rafaelge Год назад +1

    Amazing, thanks!

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

    Thank you so much!! for good content

  • @vocabguru7224
    @vocabguru7224 4 года назад +1

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

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

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

      Welcome to the channel, glad I can help.

  • @dinhtran9249
    @dinhtran9249 5 лет назад +1

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

    • @BeardedDevData
      @BeardedDevData  5 лет назад

      Thanks for the feedback, glad the video helped.

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

    Brilliant explanation and easy to follow

  • @abiydelelew5771
    @abiydelelew5771 3 года назад +1

    Great stuff, thanks again!

  • @roywit7836
    @roywit7836 3 года назад +1

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

  • @valentindelarosa76
    @valentindelarosa76 5 лет назад +1

    Thank you so much, greetings from Colombia.

  • @sanyammadaan2396
    @sanyammadaan2396 4 года назад +1

    Great video series! Keep the good work going

  • @jb4928
    @jb4928 3 года назад +1

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

    • @BeardedDevData
      @BeardedDevData  3 года назад +1

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

    • @jb4928
      @jb4928 3 года назад +1

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

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

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

  • @murtymvvs
    @murtymvvs 3 года назад +1

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

  • @parthasarathypoovendran9270
    @parthasarathypoovendran9270 Год назад +1

    Great

  • @karinshamama5591
    @karinshamama5591 3 года назад +1

    Great! Thank you

  • @srv14evr
    @srv14evr 5 лет назад +1

    You are an awesome teacher.

    • @BeardedDevData
      @BeardedDevData  5 лет назад

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

  • @sk____1958
    @sk____1958 3 года назад +1

    thants a ton for increasing font size

  • @jochemcode4570
    @jochemcode4570 5 лет назад +1

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

    • @BeardedDevData
      @BeardedDevData  5 лет назад +1

      Great stuff, good luck with the certification

    • @jochemcode4570
      @jochemcode4570 5 лет назад +1

      @@BeardedDevData thank you!

    • @BeardedDevData
      @BeardedDevData  5 лет назад +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 5 лет назад +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  5 лет назад +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.

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

    perfect

  • @kumarpratap5115
    @kumarpratap5115 3 года назад +1

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

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

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

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

  • @garglucky
    @garglucky 5 лет назад +1

    thank you!!

  • @lizbethl4
    @lizbethl4 6 лет назад +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  6 лет назад

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

    • @BeardedDevData
      @BeardedDevData  6 лет назад

      Hi lizbethl4, I have just uploaded a video on calculating running totals: ruclips.net/video/qyjxSzVE5eU/видео.html

  • @rakeshkumarsahoo9552
    @rakeshkumarsahoo9552 5 лет назад

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

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

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

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

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

  • @ChrisPTY507
    @ChrisPTY507 6 лет назад +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  6 лет назад

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

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

      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.

  • @llatyntseva
    @llatyntseva 3 года назад +1

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

    • @BeardedDevData
      @BeardedDevData  3 года назад +1

      Yes, you can have a look at my video on NTILE, m.ruclips.net/video/DS8QZ2HEqvk/видео.html

    • @llatyntseva
      @llatyntseva 3 года назад +1

      @@BeardedDevData thank you :)

  • @master-azazel
    @master-azazel 3 года назад +1

    thanks :) my exam is tomorrow

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

      @@BeardedDevData Thanks! Amazing tutorial btw

  • @nishantkumar-lw6ce
    @nishantkumar-lw6ce 5 лет назад +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  5 лет назад

      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 5 лет назад

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

    • @BeardedDevData
      @BeardedDevData  5 лет назад

      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.

  • @shreddersengupta7384
    @shreddersengupta7384 3 года назад +1

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

    • @BeardedDevData
      @BeardedDevData  3 года назад +1

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

    • @shreddersengupta7384
      @shreddersengupta7384 3 года назад +1

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

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

    does this work for mysql on MacBook?

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

      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 6 лет назад

    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  6 лет назад

      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 6 лет назад

      @@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  6 лет назад

      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 6 лет назад

      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  6 лет назад

      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 RUclips app and doesn’t always work correctly.