SQL OVER() with PARTITION BY – Clause for Gourmets With Examples

Last modified date:

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

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Category: SQL Commands

About Ing. Jan Zedníček - BI Developer, Finance controller

My name is Jan Zedníček and I work as a BI Developer at Kentico Software in Brno. Mostly you can see me there working in the office, but I also work partly as a freelancer. I have been working as a freelancer for many companies for more than 5 years, but Kentico is the matter of my heart. I used to work as a financial controller at companies like Aero Vodochody or Sberbank and I also used to be a bond program manager in Unicapital Investment group. When I am not at work, I like playing volleyball, chess, doing a workout in the gym and I enjoy tasting of best quality rums. I am trying to summarize all my knowledge on this website not to forget them (because of the rum effect, you know =) and to put them forward to anyone. Don´t worry about asking for help or writing some comments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.