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
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.
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.
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.
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.
PostgreSQL FIRST_VALUE Function example:
FIRST_VALUE
function in PostgreSQL is used to get the first value in an ordered set of rows.SELECT FIRST_VALUE(column1) OVER (ORDER BY column2) AS first_value FROM table_name;
How to use FIRST_VALUE Function in PostgreSQL:
FIRST_VALUE
function to retrieve the first value in an ordered set.SELECT FIRST_VALUE(column1) OVER (ORDER BY column2) AS first_value FROM table_name;
Window functions FIRST_VALUE in PostgreSQL:
FIRST_VALUE
is a window function that operates on a specific window of rows defined by the OVER
clause.SELECT column1, FIRST_VALUE(column2) OVER (PARTITION BY column3 ORDER BY column4) AS first_value FROM table_name;
Getting the first value in a group in PostgreSQL:
PARTITION BY
with FIRST_VALUE
to get the first value within each group.SELECT column1, column2, FIRST_VALUE(column3) OVER (PARTITION BY column1 ORDER BY column4) AS first_value FROM table_name;
PARTITION BY with FIRST_VALUE in PostgreSQL:
PARTITION BY
divides the result set into partitions, and FIRST_VALUE
operates within each partition.SELECT column1, column2, FIRST_VALUE(column3) OVER (PARTITION BY column1 ORDER BY column4) AS first_value FROM table_name;
ORDER BY and FIRST_VALUE in PostgreSQL:
ORDER BY
specifies the sorting order for the window, and FIRST_VALUE
retrieves the first value accordingly.SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4 DESC) AS first_value FROM table_name;
Finding the earliest value with FIRST_VALUE:
FIRST_VALUE
to find the earliest value in an ordered set.SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4 ASC) AS earliest_value FROM table_name;
Using FIRST_VALUE for ranking in PostgreSQL:
FIRST_VALUE
to get the first value for each rank.SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4) AS first_value, RANK() OVER (ORDER BY column4) AS rank FROM table_name;
FIRST_VALUE vs. MIN in PostgreSQL:
FIRST_VALUE
retrieves the first value in an ordered set, MIN
returns the minimum value.SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4) AS first_value, MIN(column3) OVER () AS min_value FROM table_name;
FIRST_VALUE with NULLs in PostgreSQL:
FIRST_VALUE
includes NULLs, so the result may be NULL if the first value is NULL.SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4) AS first_value FROM table_name;
Combining FIRST_VALUE with other window functions in PostgreSQL:
FIRST_VALUE
with other window functions for more complex analysis.SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4) AS first_value, SUM(column5) OVER () AS total_sum FROM table_name;
Handling ties with FIRST_VALUE in PostgreSQL:
FIRST_VALUE
returns the first encountered value.SELECT column1, column2, FIRST_VALUE(column3) OVER (ORDER BY column4) AS first_value FROM table_name;