SQL Tutorial - Window Functions - Lag and Lead

preview_player
Показать описание
Another video brought to you by BeardedDev, bringing you tutorials on Business Intelligence, SQL Programming and Data Analysis.

In this video I talk about using Lag and Lead in Windows Functions.

T-SQL Querying

T-SQL Fundamentals

Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions

This video covers the requirements to pass the exam:
If you would like to follow along with the tutorial please run the SQL below:

IF OBJECT_ID(N'dbo.Sales', N'U') IS NOT NULL
DROP TABLE dbo.Sales;

GO

CREATE TABLE dbo.Sales
(
Sales_Id INT NOT NULL IDENTITY(1, 1)
CONSTRAINT PK_Sales_Sales_Id PRIMARY KEY
, Sales_Customer_Id INT NOT NULL
, Sales_Date DATETIME2 NOT NULL
, Sales_Amount DECIMAL (16, 2) NOT NULL
)

INSERT INTO dbo.Sales (Sales_Customer_Id, Sales_Date, Sales_Amount)
VALUES
(1, '20180102', 54.99)
, (1, '20180103', 72.99)
, (1, '20180104', 34.99)
, (1, '20180115', 29.99)
, (1, '20180121', 67.00)

Lag and Lead are useful for performing trend analysis, in the example I show how we can display a customer spending trend.

Lag will show the previous value.
Lead will show the next value.

Lag and Lead accept multiple parameters as demonstrated in the video:

LAG([Column], [Offset], [Value if NULL])

The example of LAG and LEAD in the video can be shown by executing the below SQL query:

SELECT
Sales_Customer_Id
, Sales_Date
, LAG(Sales_Amount, 2, 0) OVER(PARTITION BY Sales_Customer_Id ORDER BY Sales_Date) AS PrevValue
, Sales_Amount
, LEAD(Sales_Amount, 2, 0) OVER(PARTITION BY Sales_Customer_Id ORDER BY Sales_Date) AS NextValue
FROM dbo.Sales

Please feel free to post comments.
Рекомендации по теме
Комментарии
Автор

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

surajvissa
Автор

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?

houstonvanhoy
Автор

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

muhammadmehmood
Автор

Great video BeardedDev! Very clear and concise!

dannyezechukwu
Автор

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

sarahzimmer
Автор

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!

MrAlfadark
Автор

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

vocabguru
Автор

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

dinhtran
Автор

Brilliant explanation and easy to follow

johnbreslin
Автор

Never heard of those functions! Thank you!

ExperienceWithJalal
Автор

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

murtymvvs
Автор

very helpful video and luxury of having table prepped

subbed and will follow thanks :)

PanFryedEgg
Автор

Thank you so much for this great video!

edwarddizengoff
Автор

Great video series! Keep the good work going

sanyammadaan
Автор

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

jb
Автор

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

jochemcode
Автор

Thank you so much, greetings from Colombia.

valentindelarosa
Автор

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

kumarpratap
Автор

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.

lizbethl
Автор

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?

thejoyxplorers