求SELECT语句:保留一字段的最大的那行
直接上表结构:
表名:ESD_VOL_MONITOR
列:
EQPID varchar(50): 可能会有很多重复的
ESD_VOL float: 浮点型数字
MONITOR_TIME datetime: 时间
表的内容举例说明如下
EQPID ESD_VOL MONITOR_TIME
1SASY07-P1 40 2013-02-21 18:54:29
1SASY07-P2 30 2013-02-22 18:54:29
1SASY07-P1 20 2013-02-23 18:54:29
1SASY07-P3 10 2013-02-24 18:54:29
1SASY07-P1 50 2013-02-25 18:54:29
1SASY07-P6 20 2013-02-26 18:54:29
1SASY07-P2 40 2013-02-27 18:54:29
1SASY07-P5 40 2013-02-28 18:54:29
1SASY07-P3 40 2013-02-29 18:54:29
我要得到的结果是:
相同EQPID且MONITOR_TIME是最大的,上面的话应该返回
1SASY07-P1 50 2013-02-25 18:54:29
1SASY07-P2 40 2013-02-27 18:54:29
1SASY07-P3 40 2013-02-29 18:54:29
请指点
------解决方案--------------------select * from ESD_VOL_MONITOR a where exists
(select 1 from ESD_VOL_MONITOR where a.EQPID=EQPID
group by EQPID having max(MONITOR_TIME)=a.MONITOR_TIME)
------解决方案--------------------select * from ESD_VOL_MONITOR t where MONITOR_TIME=(select max(MONITOR_TIME) from ESD_VOL_MONITOR where EQPID=t.EQPID)
------解决方案--------------------
select ESD_VOL,EQPID,MONITOR_TIME from ESD_VOL_MONITOR a inner join
(select EQPID,max(MONITOR_TIME ) id from ESD_VOL_MONITOR group by EQPID) b
on a.EQPID=b.EQPID and a.MONITOR_TIME =b.MONITOR_TIME
------解决方案--------------------select ESD_VOL,EQPID,MONITOR_TIME from ESD_VOL_MONITOR a inner join
(select EQPID,max(MONITOR_TIME ) id from ESD_VOL_MONITOR group by EQPID) b
on a.EQPID=b.EQPID and a.MONITOR_TIME =b.MONITOR_TIME
------解决方案--------------------一楼的答案很正确,又学了点东西啊