为每个唯一组合选择最新行

时间:2015-10-23 17:30:22

标签: mysql

假设我有下表:

Id          SubId        Date
----        -----        ----
1           1 
1           1
1           2
1           3
2           1
2           2 
2           2
2           3

如何为每个唯一ID /子ID对选择最新时间戳? 结果应该是:

Id          SubId        Date
----        -----        ----
1           1            latest 1-1
1           2            latest 1-2
1           3            latest 1-3 
2           1
2           2 
2           3

SQL Server有Over Partition语法可以用来做类似的事情,但据我所知MySQL不支持吗?

我想避免执行多个查询,并且如果可能的话,不要依赖某些奇特的MySQL特定语法。

2 个答案:

答案 0 :(得分:1)

如何使用group by?

Select 
Id, SubId, Max(Date)
from myTable
group by Id, SubId

答案 1 :(得分:0)

试试这个并告诉我它是否有效:

SELECT id, DISTINCT(subid) from table ORDER BY date DESC;
相关问题