这个可以用递归么
id name num
1 a 2.5
2 a 2.2
3 a 2.6
4 a 2.4
5 a 2.8
6 a 2.3
7 a 2.1
8 a 3.0
9 a 3.1
2 b 2.3
3 b 2.1
4 b 2.4
6 b 2.5
7 b 2.3
9 b 2.7
...
结果:往下大的保留
id name num
1 a 2.5
3 a 2.6
5 a 2.8
8 a 3.0
9 a 3.1
2 b 2.3
4 b 2.4
6 b 2.5
9 b 2.7
...
------解决方案--------------------create table #t(id int,name varchar(50),num float)
insert into #t
select 1,'a',2.5 union all
select 2,'a',2.2 union all
select 3,'a',2.6 union all
select 4,'a',2.4 union all
select 5,'a',2.8 union all
select 6,'a',2.3 union all
select 7,'a',2.1 union all
select 8,'a',3 union all
select 9,'a',3.1 union all
select 2,'b',2.3 union all
select 3,'b',2.1 union all
select 4,'b',2.4 union all
select 6,'b',2.5 union all
select 7,'b',2.3 union all
select 9,'b',2.7
begin
with t as
(select *,ROW_NUMBER() over(partition by name order by id) rownum from #t)
select id,name,num from t where not exists(select 1 from t t1 where t1.name=t.name and t.rownum-1=t1.rownum and t.num<t1.num )
end
drop table #t
------解决方案----------------------> 测试数据:#tb
IF OBJECT_ID('TEMPDB.DBO.#tb') IS NOT NULL DROP TABLE #tb
GO
CREATE TABLE #tb([id] INT,[name] VARCHAR(1),[num] NUMERIC(2,1))
INSERT #tb
SELECT 1,'a',2.5 UNION ALL
SELECT 2,'a',2.2 UNION ALL
SELECT 3,'a',2.6 UNION ALL
SELECT 4,'a',2.4 UNION ALL
SELECT 5,'a',2.8 UNION ALL
SELECT 6,'a',2.3 UNION ALL
SELECT 7,'a',2.1 UNION ALL
SELECT 8,'a',3.0 UNION ALL
SELECT 9,'a',3.1 UNION ALL
SELECT 2,'b',2.3 UNION ALL
SELECT 3,'b',2.1 UNION ALL
SELECT 4,'b',2.4 UNION ALL
SELECT 6,'b',2.5 UNION ALL
SELECT 7,'b',2.3 UNION ALL
SELECT 9,'b',2.7
--------------开始查询--------------------------
SELECT *
FROM #tb AS t
WHERE NOT EXISTS ( SELECT 1
FROM #tb