Window Functions - Calculate Running Differences

Описание к видео Window Functions - Calculate Running Differences

Another video brought to you by BeardedDev, bringing you tutorials on Business Intelligence, SQL Programming and Data Analysis.

You can now support me on patreon -   / beardeddev  

If you are new to working with window functions check out this video:    • SQL Tutorial - Window Functions  

In this video I talk about how to calculate a running difference using LAG and Window Functions.

Also covered in this video:
CTE - Common Table Expressions
Aggregate Functions - Average

WITH CTE
AS
(
SELECT
Sales_Customer_Id
, Sales_Date
, Sales_Amount
, LAG(Sales_Amount) OVER(PARTITION BY Sales_Customer_Id ORDER BY Sales_Date) AS PrevValue
, Sales_Amount - LAG(Sales_Amount) OVER(PARTITION BY Sales_Customer_Id ORDER BY Sales_Date) AS RunningDifference
FROM dbo.Sales
)

SELECT
Sales_Customer_Id
, AVG(RunningDifference) AS AverageDifference
FROM CTE
GROUP BY Sales_Customer_Id
ORDER BY AverageDifference DESC;

Please feel free to post comments.

Комментарии

Информация по комментариям в разработке