表结构
id    设备id   操作时间
1       1        2009-09-09
2       2        2009-09-10
3       1        2010-09-01
4       2        2010-01-01
5       2        2010-01-02
我要通过查询每个设备最近的操作时间获得数据要求结果id    设备id   操作时间3       1        2010-09-015       2        2010-01-02  一定要ID号

解决方案 »

  1.   

    要求结果 id    设备id  操作时间 3      1        2010-09-01 5      2        2010-01-02  是这样的结果?应该是下面这个吧
    3      1        2010-09-01 
    4      2        2010-01-01 SELECT * FROM  TAB
    WHERE (设备id,操作时间) IN (
    SELECT 设备id,min(操作时间) from tab group by 设备id
    )
      

  2.   


    select * from (select t.*,row_number() over(partition by sbid order by operdate desc) num from tab t) where num=1
      

  3.   

    如果最新的记录不是只有一条就将row_number()换成 DENSE_RANK().
      

  4.   

    select * from tbl(nolock)
    where [操作时间] in (SELECT max([操作时间]) from tbl(nolock) group by [设备id])
      

  5.   

    select * from tab order by time desc
      

  6.   


    按楼主的意思,这个查出来应该是符合要求的。若存在一个设备同一天操作两次的情况,则需要增加DISTINCT操作。
    select distinct * from tbl
    where [操作时间] in (SELECT max([操作时间]) from tbl(nolock) group by [设备id])
      

  7.   


    select id,设备id,操作时间,
    row_number() over(partition by 设备id orber by 操作时间 desc) rn
    from table
    where rn=1select id,设备id,操作时间
    from table 
    where 操作时间 in(select a.操作时间 from(select 设备id,max(操作时间) as 操作时间 from table group by 设备id) a)
      

  8.   


    with temp as(
    select 1 id,1 设备id, to_date('2009-09-09','yyyy-mm-dd') 操作时间 from dual
    union all
    select 2 id,2 设备id, to_date('2009-09-10','yyyy-mm-dd') 操作时间 from dual
    union all
    select 3 id,1 设备id, to_date('2010-09-01','yyyy-mm-dd') 操作时间 from dual
    union all
    select 4 id,2 设备id, to_date('2010-01-01','yyyy-mm-dd') 操作时间 from dual
    union all
    select 5 id,2 设备id, to_date('2010-01-02','yyyy-mm-dd') 操作时间 from dual
    )
    select max(id) id,设备id,max(操作时间) 操作时间 from temp group by 设备id