filmov
tv
SQL Tutorial - Window Functions

Показать описание
Another fantastic SQL Tutorial brought to you by BeardedDev.
In this video we begin to explore Window Functions and their purpose within SQL Server.
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
In this video we begin to explore Window Functions and their purpose within SQL Server.
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
Комментарии