表结构和数据如下:
id workid gcid  optdate 
1   1      1001 2008-12-15 12:12:12
2   7      1001 2008-12-15 13:12:12
3   1      1001 2008-12-15 14:12:12
4   1      1002 2008-12-15 12:12:12
现在要求,同一gcid,把optdate时间最大的一项提取出来(不同gcid的记录optdate可能相同),形成一下的视图
id workid gcid optdate
3   1      1001 2008-12-15 14:12:12
4   1      1002 2008-12-15 12:12:12
求写法。谢谢

解决方案 »

  1.   

    select * from tb t where not exists(select 1 from tb where gcid=t.gcid and optdate>t.optdate)
      

  2.   

    select * 
    from tb t
    where not exists(select * from tb where gcid=t.gcid and optdate>t.optdate)
      

  3.   

    select * from tb t
    where not exists(select 1 from tb where gcid=t.gcid and id>t.id)
      

  4.   


    改一下
    select * from tb t
    where not exists(select 1 from tb where gcid=t.gcid and optdate>t.optdate)