In order to get the latest record date for every user in the system along with the related value from the user logins particulars table, it is necessary to use either a Subquery or a Common Table Expression Restatement (CTE). The CTE implementation entails the entry of the function ROW_NUMBER() in that it allows for the ordering of any sub - set of results in a desired order, with the ordering done concerning an appropriate column, in this case, dates starting with the earliest date. This means that the row number which is assigned for every user within the subset will be 1 in the row that is associated with the most recent entry. Notwithstanding the use of CTE, one can also go a step further and use a simpler query method whereby the records are being queried are already dated at the maximum possible date for the particular user in order to retrieve those records and their respective values only. Either way, the expectation is achieved.
Using CTE
WITH LatestLogins AS (
SELECT
username,
date,
value,
ROW_NUMBER() OVER (PARTITION BY username ORDER BY date DESC) AS rn
FROM
your_table_name
)
SELECT
username,
date,
value
FROM
LatestLogins
WHERE
rn = 1;
Using SubQuery
SELECT
t.username,
t.date,
t.value
FROM
your_table_name t
WHERE
t.date = (SELECT MAX(date)
FROM your_table_name
WHERE username = t.username);