原oracle语句select * 
from (
      select tr.*,rank() over(partition by tr.sensorid order by tr.collecttime desc) new_bank
      from t_racktemphum tr) trr
where trr.new_bank < 2先按sensorid分组 再按collecttime排序,然后取每组的第一条记录

解决方案 »

  1.   

    SELECT *
    FROM t_racktemphum AS tr
    WHERE 1>(SELECT COUNT(*) FROM t_racktemphum
             WHERE tr.sensortid=sensortid
                AND collectime<tr.collectime);
      

  2.   

    select t.*
    from t_racktemphum t inner jon (select sensorid,max(collecttime) as m_collecttime from t_racktemphum group by sensorid) t2
    on t.sensorid=t2.sensorid and t.collecttime=t2.m_collecttime
      

  3.   

    select * from t_racktemphum a where not exists (select 1 from t_racktemphum b where b.sensorid=a.sensorid and b.collecttime>a.collecttime)
      

  4.   

    MySQL的官方手册中的教程中有这种例子。http://dev.mysql.com/doc/refman/5.1/zh/tutorial.html#example-maximum-row
    3.6.2. 拥有某个列的最大值的行
    3.6.4. 拥有某个字段的组间最大值的行
      

  5.   

    两个都不对呀第一个 column tr.sensortid does not exist第二个查询出来还是用重复的 sensorid
      

  6.   

    select * from t_racktemphum a where not exists (select 1 from t_racktemphum b where b.sensorid=a.sensorid and b.collecttime>a.collecttime)
      

  7.   

    先按sensorid分组 再按collecttime排序,然后取每组的第一条记录
    应该是collecttime最大的一条记录:
    select a.* from tt a inner join
    (select sensorid,max(collecttime) as ma from tt group by sensorid) b
    on a.sensorid=b.sensorid and a.collecttime=b.ma
      

  8.   

    or
    假设只有两个字段sensorid,collecttime
    select a.sensorid,a.collecttime from tt a
    inner join tt b 
    on a.sensorid=b.sensorid and a.collecttime<=b.collecttime
    group by a.sensorid,a.collecttime
    having count(b.sensorid)=1