数据库中有个简单的表,两个字段:num 和 endDate
数据Sample如下:
| num | endDate |
|---|---|
| 01 | 2013/8/1 |
| 02 | 2013/8/2 |
| 01 | 2013/8/3 |
| 03 | 2013/8/4 |
现在想取得每个num的最新endDate数据
SQL语句如下:
select * from test as a
where exists
(select 1 from test where num=a.num group by num having max(endDate)=a.endDate)
order by num;
结果如下:
| num | endDate |
|---|---|
| 01 | 2013/8/3 |
| 02 | 2013/8/2 |
| 03 | 2013/8/4 |

本文介绍了如何通过SQL查询来获取数据库表中每个唯一标识符对应的最新日期数据。
1924

被折叠的 条评论
为什么被折叠?



