职工入职后每年都写职业计划报告 简称IDPUID IDPID Year
张三 1 2006
张三 2 2006
张三 3 2006
张三 4 2007
张三 5 2007
张三 6 2008
张三 7 2008现在要查张三每年的职业计划报告,每年只显示一个,以IDPID最大为准
张三 1 2006
张三 2 2006
张三 3 2006
张三 4 2007
张三 5 2007
张三 6 2008
张三 7 2008现在要查张三每年的职业计划报告,每年只显示一个,以IDPID最大为准
select * from 表名 a where not exists(select 1 from 表名 where uid=a.uid and [Year]=a.[Year] and idpid>a.idpid)
select uid,max(idpid) as idpid,year from tb group by uid,year
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)
*/