Another fantastic SQL Tutorial brought to you by BeardedDev.
In this video we begin to explore Window Functions and their purpose within SQL Server.
If you would like to follow along, I have published another Window Functions tutorial that will allow you to do this, it can be found here: • SQL Tutorial - Window Functions (Follow Al...
Window Functions are used for performing data analysis calculations and address an important need compared to the GROUP BY clause that we are able to return the underlying data in the same query.
This video shows an example of the differences between the GROUP BY clause and Window Functions.
Window Functions were first introduced in SQL Server 2005 but further enhancements and support was added in SQL Server 2012.
We look at the OVER clause and PARTITION BY.
Window Functions can only be included within SELECT or ORDER BY clauses.
Functions Available:
Aggregate - COUNT, SUM, MIN, MAX, AVG
Offset - FIRST_VALUE, LAST_VALUE, LEAD, LAG
Statistical - PERCENT_RANK, CUME_DIST, PERCENTILE_CONT, PERCENTILE_DIST
Windows Functions also have FRAMES
ROWS
RANGE
Window Functions are a powerful tool within SQL Server and I am excited to bring more videos and tutorials working with Window Functions in the future.
Code:
WITH CTE
AS
(
SELECT
Sales_Id
, SUM(Line_Total) AS Total
FROM Sales_Details
GROUP BY Sales_Id
)
SELECT * FROM CTE AS A
INNER JOIN Sales_Details AS B
ON A.Sales_Id = B.Sales_Id
SELECT
Sales_Id
, Sales_Date
, Item
, Price
, Quantity
, Line_Total
, COUNT(Line_Total) OVER(PARTITION BY Sales_Id) AS Line_Count
, SUM(Line_Total) OVER(PARTITION BY Sales_Id) AS Sales_Total
, SUM(Line_Total) OVER(PARTITION BY Sales_Date) AS Daily_Total
, SUM(Line_Total) OVER() AS Total
FROM Sales_Details
ORDER BY Sales_Total
On this page of the site you can watch the video online SQL Tutorial - Window Functions with a duration of hours minute second in good quality, which was uploaded by the user BeardedDev 12 August 2017, share the link with friends and acquaintances, this video has already been watched 126,179 times on youtube and it was liked by 2.5 thousand viewers. Enjoy your viewing!