In MySQL, a window function is a powerful feature that allows you to perform calculations across a set of rows in a query result. It operates on a "window" of rows defined by a specific partition and an optional ordering within that partition. Window functions are commonly used to perform calculations such as running totals, ranking, and moving averages.

window functions mysql

 

Here's an overview of the main components and concepts related to window functions in MySQL:

Syntax: Window functions are typically used in the SELECT clause of a query and follow a specific syntax:

<function_name>(expression) OVER (window_specification)

Function_name: This represents the specific window function you want to use, such as SUM, AVG, ROW_NUMBER, RANK, etc. MySQL provides various window functions for different calculations.


Expression: It is the column or expression on which you want to perform the window function calculation. For example, if you want to calculate the average of a column, the expression would be the column name.


Window_specification: It defines the window or subset of rows on which the window function operates. It consists of two main parts: PARTITION BY and ORDER BY.

 

PARTITION BY: This clause divides the result set into partitions or groups based on one or more columns. The window function is applied separately to each partition. If you omit the PARTITION BY clause, the entire result set is treated as a single partition.


ORDER BY: This clause determines the order of rows within each partition. It specifies the column(s) by which the data should be sorted. The window function calculations are then performed based on this ordering.

 


Examples: Let's say we have a table named "sales" with columns like "region", "year", and "revenue". If we want to calculate the average revenue per region, while maintaining the original row order, we can use the AVG window function as follows:

SELECT region, year, revenue,
       AVG(revenue) OVER (PARTITION BY region ORDER BY year) AS avg_revenue
FROM sales;

This query calculates the average revenue for each region, ordering the data by year within each region partition.

Examples using PHP:

Calculate the running total of revenue for each year

$query = "SELECT year, revenue, SUM(revenue) OVER (ORDER BY year) AS running_total
          FROM sales";

$stmt = $pdo->query($query);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
    echo "Year: " . $row['year'] . ", Revenue: " . $row['revenue'] . ", Running Total: " . $row['running_total'] . "<br>";
}

 

 

Rank the sales representatives based on their total sales

$query = "SELECT rep_name, total_sales,
          RANK() OVER (ORDER BY total_sales DESC) AS sales_rank
          FROM (
              SELECT rep_name, SUM(revenue) AS total_sales
              FROM sales
              GROUP BY rep_name
          ) AS subquery";

$stmt = $pdo->query($query);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
    echo "Sales Rank: " . $row['sales_rank'] . ", Sales Representative: " . $row['rep_name'] . ", Total Sales: " . $row['total_sales'] . "<br>";
}

 

 

Calculate the moving average of revenue for a specific region over a 3-year period

$query = "SELECT region, year, revenue,
          AVG(revenue) OVER (PARTITION BY region ORDER BY year ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_average
          FROM sales";

$stmt = $pdo->query($query);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($result as $row) {
    echo "Region: " . $row['region'] . ", Year: " . $row['year'] . ", Revenue: " . $row['revenue'] . ", Moving Average: " . $row['moving_average'] . "<br>";
}