34,594
社区成员
发帖
与我相关
我的任务
分享
--创建测试表
if object_id ('[test]')is not null
drop table [test]
create table test(索引 int, 课程 varchar(10),分数 int)
insert [test]
select 1,'a',80 union all
select 2,'a',80 union all
select 3,'b',90 union all
select 4,'c',70 union all
select 5,'c',70
--查询语句
select min(索引) as '索引',课程,分数 from [test] group by 课程,分数 order by min(索引)
--查询结果
/**
索引 课程 分数
1 a 80
3 b 90
4 c 70
**/
DELETE FROM test
WHERE rowID NOT IN (SELECT MIN(rowID) from test
GROUP BY test.course,test.score)
if object_id('[test]') is not null drop table [test]
create table [test]([rowID] int,[course] varchar(1),[Score] int)
insert [test]
select 1,'a',80 union all
select 2,'a',80 union all
select 3,'b',90 union all
select 4,'c',70 union all
select 5,'c',70
SELECT MIN(rowid) rownum ,course ,score FROM test
GROUP BY course,score
ORDER BY rownum
delete from test t
where exists(select 1 from test where 课程=t.课程 and 分数=t.分数 and 索引<t.索引)
delete tb
from tb t
where exists(select 1 from tb where 课程=t.课程 and 分数=t.分数 and 索引<t.索引
)
delete tb
where 索引 not in (select min(索引)
from tb
group by 课程,分数)
/*
[Author]: OrchidCat[OC]_轻骑兵(向高手学习...)
[Time]: 2010-08-11 07:52:18
[Place]: From Beijing
[Version]:
Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86)
Feb 9 2007 22:47:07
Copyright (c) 1988-2005 Microsoft Corporation
Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
*/
--> 测试数据:[test]
if object_id('[test]') is not null drop table [test]
create table [test]([索引] int,[课程] varchar(1),[分数] int)
insert [test]
select 1,'a',80 union all
select 2,'a',80 union all
select 3,'b',90 union all
select 4,'c',70 union all
select 5,'c',70
select * from [test]
SELECT A.*
FROM test A
WHERE [索引] = ( SELECT MIN([索引])
FROM test
WHERE A.[课程] = [课程]
AND A.[分数] = [分数]
)
/*索引 课程 分数
----------- ---- -----------
1 a 80
3 b 90
4 c 70
(3 行受影响)*/