如何连接2个db表并同时执行group by?

5sxhfpxr  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(264)

我有两张table:
Equipment(PK int id,nvarchar name) EquipmentAction(int EquipmentId, datetime Timestamp) 我想展示最早的 Timestamp 对于每个指定的 Equipment 我想不通。
我能做到 select EquipmentId, min(TimeStamp) from EquipmentAction group by(EquipmentId) 我能做到 select e.name, a.TimeStamp from Equipment e inner join EquipmentAction a on e.id=a.EquipmentId 但我不知道如何把这两个想法结合起来。

ev7lccsx

ev7lccsx1#

你只需添加 group by ,如下所示:

select e.name, min(a.TimeStamp)
from Equipment e inner join
     EquipmentAction a
     on e.id = a.EquipmentId
group by e.name

相关问题