harryg
harryg

Reputation: 24107

SQL get the latest records from a set of data

So I have a table of holdings where each holding has a date column. e.g.

id   |   holding_name   |   value   |   holding_date
----------------------------------------------------
1    |   iShares MSCI   |   2032    |   2013-12-31
2    |   Vanguard Bonds |   5332    |   2013-12-31
3    |   iShares MSCI   |   2241    |   2014-01-31
4    |   Vanguard Bonds |   6236    |   2014-01-31

If I wanted to select only the latest holdings I could go

SELECT * FROM holdings WHERE holdings.holding_date = '2014-01-31'

But what if I don't know that the latest date is 2014-01-31? Is there a way to build this into the query?

Upvotes: 1

Views: 75

Answers (1)

Barmar
Barmar

Reputation: 782693

SELECT *
FROM holdings
WHERE holding_date = (SELECT MAX(holding_date) from holdings)

Upvotes: 6

Related Questions