SQL
Retrieve the Latest Record for Each Group
Discover how to efficiently fetch the most recent entry for each distinct group within your dataset, often used for latest user activity or product updates.
WITH LatestRecords AS (
SELECT
id,
group_id,
event_timestamp,
data_column,
ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY event_timestamp DESC) as rn
FROM
your_table
)
SELECT
id,
group_id,
event_timestamp,
data_column
FROM
LatestRecords
WHERE
rn = 1;
How it works: This SQL snippet uses a Common Table Expression (CTE) and the `ROW_NUMBER()` window function to identify the latest record for each distinct `group_id`. `PARTITION BY group_id` divides the dataset into groups based on the `group_id`, and `ORDER BY event_timestamp DESC` sorts records within each group by their `event_timestamp` in descending order. `ROW_NUMBER()` then assigns a sequential number to each record within its partition, with `1` indicating the latest entry. The outer query subsequently selects only those records where `rn = 1`, effectively retrieving the most recent record for each group.