[Solved] SQL select column of attributes with latest date [closed]


Since you didn’t specify which RDBMS you are working on and the table schema, then you can do this:

SELECT t1.tenantId, t1.ExpiryDate
FROM tenants AS t1
INNER JOIN
(
   SELECT tenantId, MAX(ExpiryDate) AS LatestExpiryDate
   FROM tablename
   GROUP BY tenantId
) AS t1 ON t1.tenantId = t2.tenantId, t1.ExpiryDate = t2.LatestExpiryDate;

The inner join will give the latest date for each tenant id, and then you can join with the original query to get only those with the latest date.

(You didn’t specify the table schema so I had to guess the columns’ names, but I hope you got the idea).

1

solved SQL select column of attributes with latest date [closed]