职工入职后每年都写职业计划报告 简称IDPUID    IDPID    Year
张三     1       2006
张三     2       2006
张三     3       2006
张三     4       2007
张三     5       2007
张三     6       2008
张三     7       2008现在要查张三每年的职业计划报告,每年只显示一个,以IDPID最大为准

解决方案 »

  1.   

    select * from tb t where not exists(select 1 from tb where year=t.year and idpid>t.idpid)
      

  2.   

    select * from [Table] a where not exists(select 1 from [Table] where Uid=a.Uid and Year=a.Uid and IDPID>a.IDPID)
      

  3.   


    select * from 表名 a where not exists(select 1 from 表名 where uid=a.uid and [Year]=a.[Year] and idpid>a.idpid)
      

  4.   


    select uid,max(idpid) as idpid,year from tb group by uid,year
      

  5.   

    select * from tb t where IDPID !>all (select IDPID from tb where year=t.year )
      

  6.   

    declare @tb table(UID nvarchar(10),    IDPID  int ,  Year int)
    insert @tb
    select N'张三',    1   ,   2006 union all
    select N'张三' ,   2   ,   2006 union all
    select N'张三' ,   3   ,   2006 union all
    select N'张三' ,   4   ,   2007 union all
    select N'张三' ,   5   ,   2007 union all
    select N'张三' ,   6   ,   2008 union all
    select N'张三'  ,  7   ,   2008 select * from @tb  as  a where not exists(select 1 from @tb where UID=a.UID and Year=a.Year and  IDPID>a.IDPID)
    /*
    UID        IDPID       Year        
    ---------- ----------- ----------- 
    张三         3           2006
    张三         5           2007
    张三         7           2008(3 row(s) affected)
    */
      

  7.   

    select uid ,IDPID= max(IDPID),year from tb group by  uid,year