日期:2014-05-18 浏览次数:20543 次
declare @T table (rm varchar(4),gs int,wgs int,xh int,xh1 int) insert into @T select '小明',200,100,1,2 union all select '小明',70,70,1,2 union all select '小华',180,180,2,1 union all select '小红',170,110,4,4 union all select '小丁',160,110,3,5 union all select '小小',150,100,5,3 ;with maco as ( select rm,sum(gs) gs,sum(wgs) wgs from @T group by rm ) select c.* from ( select *, xh=(select count(1) from maco where gs>=a.gs), xh1=(select count(1) from maco where wgs>=a.wgs) from maco a ) b left join @T c on b.rm=c.rm and (b.xh<>c.xh or b.xh1<>c.xh1) where c.rm is not null /* rm gs wgs xh xh1 ---- ----------- ----------- ----------- ----------- 小丁 160 110 3 5 小红 170 110 4 4 小小 150 100 5 3 */