MySQL FIRST_VALUE Function

MySQL FIRST_VALUE Function

MySQL FIRST_VALUE Function

The FIRST_VALUE() function in MySQL is a window function introduced in MySQL 8.0. It returns the first value in an ordered set of rows within a partition. This function is particularly useful when you want to retrieve the first value of a group, based on specific sorting criteria.

Syntax

FIRST_VALUE(expression) OVER ( [PARTITION BY column_name] [ORDER BY column_name] )
  • expression: The column or value you want to retrieve.
  • PARTITION BY: Divides the result set into partitions; the function operates within each partition independently.
  • ORDER BY: Specifies the order in which rows are processed within each partition.

Key Points

  1. FIRST_VALUE() always retrieves the first value in the ordered result set.
  2. The PARTITION BY clause groups rows into partitions.
  3. The ORDER BY clause determines the row order for evaluating the first value.
  4. If no PARTITION BY clause is specified, the function treats the entire result set as a single partition.

Examples

Example 1: Basic Usage

Suppose you have a table sales:

sale_idregionamount
1North100
2North200
3North150
4South300
5South250

To get the first sale amount for each region:

SELECT sale_id, region, amount, FIRST_VALUE(amount) OVER (PARTITION BY region ORDER BY amount) AS first_sale_amount FROM sales;

Output:

sale_idregionamountfirst_sale_amount
1North100100
3North150100
2North200100
5South250250
4South300250

Example 2: Without PARTITION BY

To get the overall first sale amount, regardless of region:

SELECT sale_id, region, amount, FIRST_VALUE(amount) OVER (ORDER BY amount) AS first_sale_amount FROM sales;

Output:

sale_idregionamountfirst_sale_amount
1North100100
3North150100
2North200100
5South250100
4South300100

Example 3: Using FIRST_VALUE() with Custom Order

To get the first sale amount for each region, ordered by sale ID:

SELECT sale_id, region, amount, FIRST_VALUE(amount) OVER (PARTITION BY region ORDER BY sale_id) AS first_sale_by_id FROM sales;

Output:

sale_idregionamountfirst_sale_by_id
1North100100
3North150100
2North200100
4South300300
5South250300

Example 4: Combine with Other Functions

You can combine FIRST_VALUE() with other window functions for deeper analysis. For example, calculate the difference between each sale and the first sale amount for each region:

SELECT sale_id, region, amount, FIRST_VALUE(amount) OVER (PARTITION BY region ORDER BY amount) AS first_sale_amount, amount - FIRST_VALUE(amount) OVER (PARTITION BY region ORDER BY amount) AS difference FROM sales;

Output:

sale_idregionamountfirst_sale_amountdifference
1North1001000
3North15010050
2North200100100
5South2502500
4South30025050

Use Cases

  1. Top Performers:
    • Identify the first or best result in a group.
  2. Historical Context:
    • Retrieve the earliest data point for comparisons.
  3. Trend Analysis:
    • Analyze changes relative to the initial value.

Conclusion

The FIRST_VALUE() function is a powerful tool for retrieving the first value in a partition or the entire result set. By leveraging PARTITION BY and ORDER BY, you can customize its behavior for various analytical use cases, such as identifying top performers, analyzing trends, or calculating differences from the first value.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close