一道面试题,请高手指教,万分感谢

hxiquan80 2010-08-11 07:47:54
有表test
记录为
索引 课程 分数
1 a 80
2 a 80
3 b 90
4 c 70
5 c 70
要求删掉表test中课程和分数相同,但索引有多条的记录只保留索引号最小的那条,即需要删除后表记录为
1 a 80
3 b 90
4 c 70
谢谢!
...全文
299 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
windxfxx8 2010-08-14
  • 打赏
  • 举报
回复
delete [test]
where index not in (select min(index)
from test
group by 课程)

delete from
test from test a where exists(select * from test b where a.课程=b.课程 and a.index<>b.index)
Rotel-刘志东 2010-08-14
  • 打赏
  • 举报
回复
--创建测试表
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
**/
Mr.Huang…… 2010-08-13
  • 打赏
  • 举报
回复
学习了!
8楼正解
「已注销」 2010-08-13
  • 打赏
  • 举报
回复
如果需要删除重复的行,参考:

DELETE FROM test
WHERE rowID NOT IN (SELECT MIN(rowID) from test
GROUP BY test.course,test.score)
「已注销」 2010-08-13
  • 打赏
  • 举报
回复
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
suibiantha 2010-08-12
  • 打赏
  • 举报
回复
支持8楼
ask_chang 2010-08-12
  • 打赏
  • 举报
回复
DELETE FROM test WHERE id NOT IN (SELECT MIN(id) FROM test GROUP BY course,score)
taotony 2010-08-12
  • 打赏
  • 举报
回复
delete test
from test t
where exists(select 1 from test where kc=t.kc and score=t.score and sy<t.sy)
幸运的意外 2010-08-12
  • 打赏
  • 举报
回复
delete tb
from tb t
where exists(select * from tb where 课程=t.课程 and 分数=t.分数 and 索引<t.索引
)
shadowdudu 2010-08-12
  • 打赏
  • 举报
回复
delete tb where id in (select min(id) from tb group by kc,fs having count(*)>1) 手机发帖不容易阿
ai593423625 2010-08-12
  • 打赏
  • 举报
回复
学习···
crszf 2010-08-12
  • 打赏
  • 举报
回复
Select A.* From Test A Where 索引 =( Select U_Index From (Select Count(1) Cnt,Min(索引) U_Index From Test Where A.[课程] = [课程] AND A.[分数] = [分数]) T Where Cnt > 1)
Lakesy 2010-08-11
  • 打赏
  • 举报
回复
vesion为2008的话,用with&row_number()吧,几天前好像有张相似的帖子
情殇无限 2010-08-11
  • 打赏
  • 举报
回复
delete from test t
where exists(select 1 from test where 课程=t.课程 and 分数=t.分数 and 索引<t.索引)
华夏小卒 2010-08-11
  • 打赏
  • 举报
回复
delete tb
from tb t
where exists(select 1 from tb where 课程=t.课程 and 分数=t.分数 and 索引<t.索引
)
pt_caddy 2010-08-11
  • 打赏
  • 举报
回复

delete tb
where 索引 not in (select min(索引)
from tb
group by 课程,分数)
Mr_Nice 2010-08-11
  • 打赏
  • 举报
回复
/*
[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 行受影响)*/
skypaul888 2010-08-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 pt_caddy 的回复:]
SQL code

delete tb
where 索引 not in (select min(索引)
from tb
group by 课程,分数)
[/Quote]
正解
sanny_txx 2010-08-11
  • 打赏
  • 举报
回复
支持8楼的
gaojie001 2010-08-11
  • 打赏
  • 举报
回复
delete tb
from tb t
where exists(select 1 from tb where 课程=t.课程 and 分数=t.分数 and 索引<t.索引)
加载更多回复(4)

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧