Reputation: 49
Have 2 tables, Table 1 has username, created (timestamp), dim Table 2 has username, dob
Need to join table and get the dim from table 1 with the max(created) for each username.
Upvotes: 0
Views: 272
Reputation: 521239
Using ROW_NUMBER
we can try:
WITH cte AS (
SELECT t2.username, t1.created, t1.dim,
ROW_NUMBER() OVER (PARTITION BY t1.username
ORDER BY t1.created DESC) rn
FROM Table2 t2
INNER JOIN Table1 t1 ON t2.username = t1.username
)
SELECT username, created, dim
FROM cte
WHERE rn = 1;
Upvotes: 1