• 26. 11. 2019
  • Ing. Jan Zedníček - Data Engineer & Controlling
  • 0

OVER clause can belong to commands working with so-called Window functions in SQL. These types of functions enable us to look at the data not only in their aggregated form (using GROUP BY), but we can also look at functional operations via multiple other attributes. We, in fact, create windows of some sort and we apply various computational operations above this bordered data. It sounds complicated but an example will later show you how easy it actually is:

We can apply the OVER command on these types of functions

  1. Aggregate functions
  2. Ranking functions
  3. Offset functions

Practical Example OVER (with or without PARTITION BY)

The source will look like this:

-- Database Adventureworks
SELECT c.CustomerKey, SalesOrderNumber, d.CalendarYear, SalesAmount
FROM FactInternetSales a
     INNER JOIN DimCustomer c ON a.CustomerKey=c.CustomerKey
     INNER JOIN DimDate d ON a.OrderDateKey=d.DateKey
WHERE a.CustomerKey IN (14718, 19383);

We display data for 2 customers [CustomerKey] (number 14718 and 19383). These customers have multiple different orders [SalesOrderNumber] and we earned money for them [SalesAmount].

sql over with partition by example source data

The task is to write a script that will give us the same result as shown on the screenshot. 7 rows plus 2 columns added:

  • [CustomerTotal] – earnings for every customer
  • [TotalSales] – total sales

Solution

SELECT
  c.CustomerKey
  ,SalesOrderNumber
  ,d.CalendarYear
  ,SalesAmount
  ,SUM(a.SalesAmount) OVER (PARTITION BY a.CustomerKey) AS Customertotal
  ,SUM(a.SalesAmount) OVER () AS TotalSales
FROM FactInternetSales a
     INNER JOIN DimSalesTerritory b
       ON a.SalesTerritoryKey=b.SalesTerritoryKey
     INNER JOIN DimCustomer c
       ON a.CustomerKey=c.CustomerKey
     INNER JOIN DimDate d
       ON a.OrderDateKey=d.DateKey
WHERE a.CustomerKey IN (14718, 19383);

SQL OVER with PARTITION BY example solution

Practical example using OVER, PARTITION BY  and ROWS BETWEEN command (cumulation)

Let’s try to add one more column [RunningTotal] into the previous task. It will count the accumulation of a given row via customers. It will be done so in a manner wherewith increasing the number of rows will be the value [SalesAmount] added up to the sum of values from previous rows within the same customer.

sql over example with partition by and rows between command

5/5 - (1 vote)

Ing. Jan Zedníček - Data Engineer & Controlling

My name is Jan Zedníček and I have been working as a freelancer for many companies for more than 10 years. I used to work as a financial controller, analyst and manager at many different companies in field of banking and manufacturing. When I am not at work, I like playing volleyball, chess, doing a workout in the gym.

🔥 If you found this article helpful, please share it or mention me on your website

Leave a Reply

Your email address will not be published. Required fields are marked *