SQL查询唯一设备列表

时间:2014-05-14 13:53:18

标签: sql

提前感谢您一看。我不知道如何搜索这个。 图2测试站,每个测试站都有一个必须保持校准的设备架。设备由stationId,eqPartno和eqLocation标识。位置而不是序列号,因此它可以更换单元。

来自2个表:

station_events(eventId为auto,recUpdated为auto)

"stationEventId","stationId","stationEvent","recUpdated"
1,"1","databaseCreation",2014-05-14 07:51:25 AM
2,"55","databaseCreation",2014-05-14 07:51:25 AM
3,"1","updateCal",2014-05-14 08:11:26 AM
4,"1","updateCal",2014-05-14 08:11:59 AM
5,"1","updateCal",2014-05-14 08:48:26 AM

设备(equipmentId为auto,recUpdated为auto,由equipmentId链接)

"equipmentId","stationEventId","eqPartNo","eqLocation","eqCalExpires","eqRecUpdated"
1,1,"eq1","loc1",2000-01-31,2014-05-14 07:53:59 AM
2,1,"eq2","loc2",2000-02-22,2014-05-14 08:05:52 AM
3,3,"eq1","loc1",2014-04-04,2014-05-14 08:13:37 AM
4,4,"eq2","loc2",2014-04-05,2014-05-14 08:14:25 AM
5,5,"eq1","loc1",2014-04-05,2014-05-14 08:47:04 AM

查询是形成1号站设备的视图,其中包含每个已安装单元的最新记录,以验证校准。

select e.* from equipment e
inner join station_events se on se.stationEventId=e.stationEventId
where se.stationId='1'
group by eqPartNo,eqLocation

结果看起来不错。

"equipmentId","stationEventId","eqPartNo","eqLocation","eqCalExpires","eqRecUpdated"
5,5,"eq1","loc1",2014-04-05,2014-05-14 08:47:04 AM
4,4,"eq2","loc2",2014-04-05,2014-05-14 08:14:25 AM

我刚刚创建了一个幸运的黑客,或者这是正确的吗?我喜欢这个,因为我们有校准历史记录,只要视图保证描述所有最新的设备记录。

最诚挚的问候, 吉姆谢德登

1 个答案:

答案 0 :(得分:0)

这是幸运的。您可以在查询中插入一个MAX这样的聚合

select e.equipmentId, 
       e.stationEventId, 
       e.eqPartNo, 
       e.eqLocation, 
       e.eqCalExpires, 
       MAX(e.eqRecUpdated) from equipment e
inner join station_events se on se.stationEventId=e.stationEventId
where se.stationId='1'
group by eqPartNo,eqLocation