简体   繁体   中英

SQL get the latest records from a set of data

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM