SQL Tutorial - Window Functions

Veröffentlicht am: 12 August 2017
auf dem Kanal: BeardedDev
126,179
2.5k

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


Auf dieser Seite können Sie das Online-Video SQL Tutorial - Window Functions mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer BeardedDev 12 August 2017 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 126,179 Mal angesehen und es wurde von 2.5 tausend den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!