PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - FIRST_VALUE Function

The FIRST_VALUE function in PostgreSQL is a window function that returns the first value in an ordered set of values. You use it to fetch the first value from a defined window of rows related to the current row.

Syntax:

FIRST_VALUE(expression) OVER (
    [PARTITION BY partition_expression, ... ]
    ORDER BY sort_expression [ASC | DESC], ...
)
  • expression: The value you want to retrieve. This can be a column name or a calculation based on column values.

  • PARTITION BY: This divides the result set into partitions to which the FIRST_VALUE function is applied.

  • ORDER BY: This defines the order in which rows in a partition are arranged.

Example:

Consider a table named sales with the following structure and data:

| id | product | sale_date  | amount |
|----|---------|------------|--------|
| 1  | A       | 2023-01-01 | 100    |
| 2  | A       | 2023-01-05 | 150    |
| 3  | B       | 2023-01-02 | 200    |
| 4  | B       | 2023-01-06 | 250    |
| 5  | A       | 2023-01-10 | 50     |

Let's say you want to retrieve each sale along with the first sale date for each product. You can achieve this using the following query:

SELECT
    product,
    sale_date,
    amount,
    FIRST_VALUE(sale_date) OVER (PARTITION BY product ORDER BY sale_date) as first_sale_date
FROM
    sales
ORDER BY
    product, sale_date;

This will produce the following result:

| product | sale_date  | amount | first_sale_date |
|---------|------------|--------|-----------------|
| A       | 2023-01-01 | 100    | 2023-01-01      |
| A       | 2023-01-05 | 150    | 2023-01-01      |
| A       | 2023-01-10 | 50     | 2023-01-01      |
| B       | 2023-01-02 | 200    | 2023-01-02      |
| B       | 2023-01-06 | 250    | 2023-01-02      |

In the result, you can see that for each product, the first_sale_date column shows the earliest sale_date for that product.

Points to Remember:

  • The FIRST_VALUE function is applied to each row in the result set according to the window definition.

  • While the example uses sale_date as the value to retrieve with FIRST_VALUE, you could use this function to retrieve the first value of any expression or column in the defined window.

In summary, the FIRST_VALUE function in PostgreSQL is a handy tool when you need to compare each row in a set to the first row in that set (or a partition of that set), especially in scenarios involving time-series data or cumulative calculations.

  1. PostgreSQL FIRST_VALUE Function example:

    • Description: The FIRST_VALUE function in PostgreSQL is used to get the first value in an ordered set of rows.
    • Code:
      SELECT FIRST_VALUE(column1) OVER (ORDER BY column2) AS first_value
      FROM table_name;
      
  2. How to use FIRST_VALUE Function in PostgreSQL:

    • Description: Use the FIRST_VALUE function to retrieve the first value in an ordered set.
    • Code:
      SELECT FIRST_VALUE(column1) OVER (ORDER BY column2) AS first_value
      FROM table_name;
      
  3. Window functions FIRST_VALUE in PostgreSQL:

    • Description: FIRST_VALUE is a window function that operates on a specific window of rows defined by the OVER clause.
    • Code:
      SELECT column1, FIRST_VALUE(column2) OVER (PARTITION BY column3 ORDER BY column4) AS first_value
      FROM table_name;
      
  4. Getting the first value in a group in PostgreSQL:

    • Description: Use PARTITION BY with FIRST_VALUE to get the first value within each group.
    • Code:
      SELECT column1, column2, FIRST_VALUE(column3) OVER (PARTITION BY column1 ORDER BY column4) AS first_value
      FROM table_name;
      
  5. PARTITION BY with FIRST_VALUE in PostgreSQL:

    • Description: PARTITION BY divides the result set into partitions, and FIRST_VALUE operates within each partition.
    • Code:
      SELECT column1, column2, FIRST_VALUE(column3) OVER (PARTITION BY column1 ORDER BY column4) AS first_value
      FROM table_name;
      
  6. ORDER BY and FIRST_VALUE in PostgreSQL:

    • Description: ORDER BY specifies the sorting order for the window, and FIRST_VALUE retrieves the first value accordingly.
    • Code:
      SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4 DESC) AS first_value
      FROM table_name;
      
  7. Finding the earliest value with FIRST_VALUE:

    • Description: Use FIRST_VALUE to find the earliest value in an ordered set.
    • Code:
      SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4 ASC) AS earliest_value
      FROM table_name;
      
  8. Using FIRST_VALUE for ranking in PostgreSQL:

    • Description: Assign ranks using FIRST_VALUE to get the first value for each rank.
    • Code:
      SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4) AS first_value,
             RANK() OVER (ORDER BY column4) AS rank
      FROM table_name;
      
  9. FIRST_VALUE vs. MIN in PostgreSQL:

    • Description: While FIRST_VALUE retrieves the first value in an ordered set, MIN returns the minimum value.
    • Code:
      SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4) AS first_value,
             MIN(column3) OVER () AS min_value
      FROM table_name;
      
  10. FIRST_VALUE with NULLs in PostgreSQL:

    • Description: FIRST_VALUE includes NULLs, so the result may be NULL if the first value is NULL.
    • Code:
      SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4) AS first_value
      FROM table_name;
      
  11. Combining FIRST_VALUE with other window functions in PostgreSQL:

    • Description: Combine FIRST_VALUE with other window functions for more complex analysis.
    • Code:
      SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4) AS first_value,
             SUM(column5) OVER () AS total_sum
      FROM table_name;
      
  12. Handling ties with FIRST_VALUE in PostgreSQL:

    • Description: When there are ties, FIRST_VALUE returns the first encountered value.
    • Code:
      SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4) AS first_value
      FROM table_name;