filmov
tv
SQL Tutorial - Window Functions - Lag and Lead

Показать описание
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.
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.
Комментарии