The WINDOW Clause

If you are dealing with data analysis or simply you want to make your code faster i suppose you already use WINDOW functions. If not, you should definitely look at.

To learn more, you should take a look at one of the best resources you could find on the topic and this is "T-SQL Window Functions: For data analysis and beyond" by Itzik Ben-Gan (https://itziktsql.com/t-sql-winfun-2nd-edition).

The WINDOW clause allows you to shorten your code by avoiding the repetition of identical parts of your window specifications.
The WINDOW clause is located between the query’s HAVING and ORDER BY clauses:

SELECT
FROM
WHERE
GROUP BY
HAVING
WINDOW
ORDER BY

Syntax:

WINDOW window_name AS ([ reference_window_name ][ <PARTITION BY clause> ][ <ORDER BY clause> ][ <ROW or RANGE clause> ])
<PARTITION BY clause> ::= PARTITION BY value_expression , ... [ n ]
<ORDER BY clause> ::= ORDER BY order_by_expression [ COLLATE collation_name ] [ ASC | DESC ] [ , ...n ]
<ROW or RANGE clause> ::= { ROWS | RANGE } <window frame extent>

We will use the demo database AdventureWorks2019 with the compatibility level 160+ (MSSQL 2022) and we will retrieve all orders, order number for the customer, first and last date of order, sum of all orders as well as subtotal.

Before the existence of the WINDOW Clause the code to accomplish the task was:

SELECT 
	h.SalesOrderID
	, h.CustomerID
	, h.OrderDate
	, h.TotalDue
	, ROW_NUMBER() OVER (PARTITION BY h.CustomerID ORDER BY  h.OrderDate, h.SalesOrderID) AS Order_Number
	, MIN(h.OrderDate) OVER (PARTITION BY h.CustomerID) AS Customer_First_Order_Date
	, MAX(h.OrderDate) OVER (PARTITION BY h.CustomerID) AS Customer_Last_Order_Date
	, SUM(h.TotalDue) OVER (PARTITION BY h.CustomerID) AS Customer_Total_Due
	, SUM(h.TotalDue) OVER (PARTITION BY h.CustomerID ORDER BY h.OrderDate, h.SalesOrderID ROWS UNBOUNDED PRECEDING) AS Running_Sum_Total_Due
FROM 
	[Sales].[SalesOrderHeader] h
ORDER BY
	h.CustomerID, h.OrderDate, h.SalesOrderID;

And now:

SELECT 
	h.SalesOrderID
	, h.CustomerID
	, h.OrderDate
	, h.TotalDue
	, ROW_NUMBER() OVER Part_Cust_OrederBy AS Order_Number
	, MIN(h.OrderDate) OVER Part_Cust AS Customer_First_Order_Date
	, MAX(h.OrderDate) OVER Part_Cust AS Customer_Last_Order_Date
	, SUM(h.TotalDue) OVER Part_Cust AS Customer_Total_Due
	, SUM(h.TotalDue) OVER Part_Cust_OrederBy_Unb AS Running_Sum_Total_Due
FROM 
	[Sales].[SalesOrderHeader] h
WINDOW
	Part_Cust AS (PARTITION BY h.CustomerID)
	, Part_Cust_OrederBy AS (P ORDER BY  h.OrderDate, h.SalesOrderID)
	, Part_Cust_OrederBy_Unb AS (PO ROWS UNBOUNDED PRECEDING)
ORDER BY
	h.CustomerID, h.OrderDate, h.SalesOrderID;

Notice that I intentionally used relatively long names for the window names (Part_Cust, Part_Cust_OrederBy and Part_Cust_OrederBy_Unb), but simply to make the code more understandable. I could have used much shorter names like (A, B and C) which would have made the code much shorter.

The result of the code is completely identical, and this does not give any novelty in the actual use of WINDOW functions. But the code becomes shorter, reusable and more readable.