--写得复杂了点.促合看下!
--测试环境
declare @t table (N int ,W bit)
insert into @t select 2005001,0
union all select 2005002,0
union all select 2005003,0
union all select 2005200,1
union all select 2005201,1
union all select 2005202,1
--查询
select * from ( select * from @t a
where exists (select 1 from @t where W=a.W and N>a.N) ) A
where not exists (select 1 from
(select * from @t a
where exists (select 1 from @t where W=a.W and N>a.N)
) B where B.W=A.W and B.N>A.N)
--结果
N W
----------- ----
2005002 0
2005201 1
--刚才写的不严谨,借用一下楼上的数据
declare @t table (N int ,W INT)
insert into @t select 2005001,0
union all select 2005002,0
union all select 2005003,0
union all select 2005200,1
union all select 2005201,1
union all select 2005202,1
union all select 2005202,2
SELECT MAX(N) N,W FROM @t WHERE CONVERT(VARCHAR(10),N)+CONVERT(VARCHAR(10),W) NOT IN(SELECT CONVERT(VARCHAR(10),MAX(N))+CONVERT(VARCHAR(10),W) FROM @t GROUP BY W HAVING COUNT(*)>1) GROUP BY W
--结果
N W
----------- -----------
2005002 0
2005201 1
2005202 2