Select Latest Data from Table

Problem

Given the following database table: DATA (id, data_date, data_value). The id field is not unique. Write an SQL statement to select the latest value for each id

Solution

Group rows by id then select rows with maximum date

Code

Here is the code

Select id, max(data_date) data_date, data_value
from DATA
group by id
  1. Note: The solution provided was not verified. Possibly the SQL statement is broken.

Leave a Reply

%d bloggers like this: