选择每个ID具有最新日期的数据

时间:2019-07-30 14:23:36

标签: sql ms-access select group-by

我目前正在使用Microsoft Access,但正在努力做自己想做的事情。 我有这张桌子A:

Table A

id    title  name   date
123   azer   dfgd   1
123   afg    qsd    5
123   arr    poi    7
123   aur    qhg    3
456   aoe    aer    3
456   iuy    zer    4

我想获取每个ID具有最新日期(最高编号)的ID,标题和名称列

在该示例中,查询将给出

id  title name date
123  arr  poi   7
456  iuy  zer   4

希望您能为我提供帮助。

谢谢!

2 个答案:

答案 0 :(得分:0)

我会推荐一个相关的子查询:

select a.*
from a
where a.date = (select max(a2.date) from a as a2 where a2.id = a.id);

为了提高性能,您希望在a(id, date)上建立索引。

答案 1 :(得分:0)

不存在:

select t.*
from tablename t
where not exists (
  select 1 from tablename
  where id = t.id and date > t.date
)