问几个SQL问题

rocket204 2008-03-25 11:10:05
table1
id 学号 姓名 课程名 分数
1 001 andy 语文 80
2 002 TOM 语文 85
3 001 andy 语文 80
4 001 andy 数学 80
5 003 张三 语文 80
6 004 李四 英语 60
7 005 王五 语文 82
8 005 王五 语文 82
9 006 罗林 英语 75
10 006 罗林 英语 82
11 003 张三 语文 80
12 003 张三 语文 80


(1).要删除除ID外重复的记录, 如1和3 5,11,和12 要保留重复记录中的一条.
(2).要查出语文成绩和数学成绩都在前5名的同学
(3).要查出学号,姓名,课程名相同,但分数不同的记录,如 9和10

table2 和 table3,字段相同.
(4)要合并查出他们的记录
(5)有字段A ,B,,把2表中字A+B没有重复的记录合并查询出来,
查询出来的记录也不要出现重复.

谢谢!
...全文
98 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlinglong 2008-03-25
  • 打赏
  • 举报
回复
declare @table1 table (id int , 学号 varchar(100) ,  姓名 varchar(100) , 课程名 varchar(100),  分数 numeric(10,2))
insert into @table1 Select

1 , '001' , 'andy', '语文', 80
union
select 2 , '002' , 'TOM', '语文', 85
union
Select
3 , '001' , 'andy' , '语文' , 80
union
Select
4 , '001', 'andy', '数学', 80
union
Select
5 , '003', '张三', '语文', 80
union
Select
6 , '004', '李四', '英语', 60
union
Select
7 , '005', '王五', '语文', 82
union
Select
8 ,'005', '王五', '语文', 82
union
Select
9 , '006', ' 罗林 ', ' 英语', 75
union
Select
10 , '006', '罗林', '英语', 82
union
Select
11 , '003', '张三', '语文', 80
union
Select
12 , '003', '张三', '语文', 80

Select max(id) as id , 学号 , 姓名 , 课程名 into #tab From @table1 Group by 学号 , 姓名 , 课程名
--1.
Delete @table1 Where id not in( Select id From #tab )
drop table #tab

--2.

Select * into #tab1 From @table1 where 课程名 in ('语文','数学') order by 课程名,分数 desc


Select top 5 姓名
From #tab1
Group by 姓名
having Count(姓名)>1


--3.
Select 姓名
From
(Select 姓名, 分数 From @table1 group by 学号,姓名,课程名,分数)
group by 姓名
having count(姓名) > 1

--4.
Select a,b,c,d From @table2
union all
Select a,b,c,d From @table3

--5

Select Distinct c.ab ,a,b,c,d From
(
Select a,b,c,d,(a+b) as ab From @table2
union all
Select a,b,c,d,(a+b) as ab From @table3
)c
sp4 2008-03-25
  • 打赏
  • 举报
回复
大乌龟,你的distinct多余,没有必要,union就够了
sp4 2008-03-25
  • 打赏
  • 举报
回复
那就union 就得了

select A,B from table1
union
select A,B from table2
dawugui 2008-03-25
  • 打赏
  • 举报
回复
(1).要删除除ID外重复的记录, 如1和3 5,11,和12 要保留重复记录中的一条.
delete table1 from table1 t where id not in (select min(id) from table1 where 学号 = t.学号 and 姓名 = t.姓名 and 课程名 = t.课程名 and 分数 = t.分数)

(2).要查出语文成绩和数学成绩都在前5名的同学

select * from table1 where 学号 in
(select 学号 from
(
select 学号 , px = (select count(*) from table1 where 课程名 = '语文' and 分数 > t.分数 ) + 1 from table1 t where 课程名 = '语文'
select 学号 , px = (select count(*) from table1 where 课程名 = '数学' and 分数 > t.分数 ) + 1 from table1 t where 课程名 = '数学'
) m group by 学号 having count(*) = 2
)
(3).要查出学号,姓名,课程名相同,但分数不同的记录,如 9和10

select distinct * 学号,姓名,课程名,分数 from table1 t where exists (select 1 from table1 where 学号=t.学号 and 姓名=t.姓名 and 课程名=t.课程名)

table2 和 table3,字段相同.
(4)要合并查出他们的记录
select * from table1
union all
select * from table2
union all
select * from table3

(5)有字段A ,B,,把2表中字A+B没有重复的记录合并查询出来,
查询出来的记录也不要出现重复.
select distinct A,B from table1
union
select distinct A,B from table2
ojuju10 2008-03-25
  • 打赏
  • 举报
回复
1、
delete a from table1 a
where exists(select 1 from table1 b where a.学号=b.学号 and a.姓名=b.姓名 and a.课程名=b.课程名 and a.分数=b.分数 and a.id>b.id)

2、
select * from table1 a
where 分数 in (select top 5 分数 from table1 where 课程名 in ('语文','数学') group by 分数)

3、
select * from table1 a
where not exists(select 1 from table1 b where a.学号=b.学号 and a.姓名=b.姓名 and a.课程名=b.课程名 and a.分数=b.分数)
and exists(select 1 from table1 b where a.学号=b.学号 and a.姓名=b.姓名 and a.课程名=b.课程名)

4、

select * from table2 b
union all
select * from table3 c

5、

select * from
(select * from table2 b
union all
select * from table3 c
) a
where not exists(select 1 from (select * from table2 b
union all
select * from table3 c
) b where a.A+a.B=b.A+b.B and a.id>b.id)




忆轩辕 2008-03-25
  • 打赏
  • 举报
回复
(5)有字段A ,B,,把2表中字A+B没有重复的记录合并查询出来,
查询出来的记录也不要出现重复.


把结果集union一下就好了
sp4 2008-03-25
  • 打赏
  • 举报
回复
CREATE TABLE table_Pqs(id int, 学号 varchar(10), 姓名 varchar(30), 课程名 varchar(30), 分数 int)
insert into table_pqs
select 1 ,'001', 'andy' ,'语文', 80
union all select 2 ,'002' ,'TOM' ,'语文', 85
union all select 3 ,'001' ,'andy' ,'语文', 80
union all select 4 ,'001' ,'andy' ,'数学', 80
union all select 5 ,'003' ,'张三' ,'语文', 80
union all select 6 ,'004' ,'李四' ,'英语', 60
union all select 7 ,'005' ,'王五' ,'语文', 82
union all select 8 ,'005' ,'王五' ,'语文', 82
union all select 9 ,'006' ,'罗林' ,'英语', 75
union all select 10 ,'006' ,'罗林' ,'英语', 82
union all select 11 ,'003' ,'张三' ,'语文', 80
union all select 12 ,'003' ,'张三' ,'语文', 80

(1).要删除除ID外重复的记录, 如1和3 5,11,和12 要保留重复记录中的一条.
Delete from Table_Pqs
Where exists(
select 1 from Table_Pqs Pqs
Where Table_Pqs.学号=Pqs.学号 and Table_Pqs.课程名=Pqs.课程名
and Table_Pqs.分数=Pqs.分数 and Table_Pqs.[id]>Pqs.[id])

(2).要查出语文成绩和数学成绩都在前5名的同学
select * from
(select top 5 学号,姓名 from table_pqs where 课程名='语文' order by 分数 desc) as t1
inner join
(select top 5 学号,姓名 from table_pqs where 课程名='数学' order by 分数 desc) as t2
on t1.学号=t2.学号

(3).要查出学号,姓名,课程名相同,但分数不同的记录,如 9和10
select * from table_Pqs where exists(select 1 from Table_Pqs Pqs
where Table_Pqs.学号=Pqs.学号 and Table_Pqs.课程名=Pqs.课程名
and Table_Pqs.分数<>Pqs.分数)

table2 和 table3,字段相同.
(4)要合并查出他们的记录
--完全合并
select * from table2 union all select * from table3
--剔除重复数据合并
select * from table2 union select * from table3

(5)有字段A ,B,,把2表中字A+B没有重复的记录合并查询出来,
查询出来的记录也不要出现重复.
--不明白什么意思
忆轩辕 2008-03-25
  • 打赏
  • 举报
回复
(1).要删除除ID外重复的记录, 如1和3 5,11,和12 要保留重复记录中的一条.

提示:临时表

(2).要查出语文成绩和数学成绩都在前5名的同学
(3).要查出学号,姓名,课程名相同,但分数不同的记录,如 9和10

table2 和 table3,字段相同.
(4)要合并查出他们的记录
(5)有字段A ,B,,把2表中字A+B没有重复的记录合并查询出来,
查询出来的记录也不要出现重复.

自己多看看联机帮助。。。。。。。。。。
wangjianming45 2008-03-25
  • 打赏
  • 举报
回复
问题1经过改进后的SQL脚本:
delete a
from (select * from @t ) a
where exists
(
select * from (select * from @t ) b
where a.学号=b.学号
and a.姓名=b.姓名
and a.课程名=b.课程名
and a.分数=b.分数
and a.id>b.id
)
wangjianming45 2008-03-25
  • 打赏
  • 举报
回复
问题1,
这问题的确经典和复杂,请看以下结果:
declare @T table
(
id int identity(1,1)
,学号 char(3)
,姓名 varchar(30)
, 课程名 varchar(6)
,分数 int
)

insert into @t
select
'001','andy', '语文', 80
union all
select
'002','TOM', '语文', 85
union all
select
'001','andy', '语文', 80
union all
select
'001','andy', '数学', 80
union all
select
'003','张三', '语文', 80
union all
select
'004','李四', '英语', 60
union all
select
'005','王五', '语文', 82
union all
select
'005','王五', '语文', 82
union all
select
'006','罗林', '英语', 75
union all
select
'006','罗林', '英语', 82
union all
select
'003','张三', '语文', 80
union all
select
'003','张三', '语文', 80



--select *
--from @t
--where exists
delete
from @t
where id in
(select distinct e.id
from @t as e
inner join
(select c.id,c.学号, c.姓名,c.课程名,c.分数
from @t as c
inner join
(select count(a.id) as count,a.学号, a.姓名,a.课程名,a.分数
from @t as a
group by a.学号, a.姓名,a.课程名,a.分数
having count(a.id)>1
) as b
on c.学号=b.学号 and c.姓名=b.姓名 and c.课程名=b.课程名 and c.分数=b.分数
)as d
on e.学号=d.学号 and e.姓名=d.姓名 and e.课程名=d.课程名 and e.分数=d.分数
where e.id>d.id)

select * from @t

huangjh_love 2008-03-25
  • 打赏
  • 举报
回复
--(1).要删除除ID外重复的记录, 如1和3   5,11,和12 要保留重复记录中的一条. 

delete table1 from table1 a
where exists (select 1 from table1
where studername=a.studername
and Curriculum_name=a.Curriculum_name
and soce=a.soce and id<a.id)

--(2).要查出语文成绩和数学成绩都在前5名的同学

select top 5 * from table1
where Curriculum_name='语文'
and StuderName in (select top 5 studername from table1
where Curriculum_name='数学' order by soce desc )
order by Soce desc
--(3).要查出学号,姓名,课程名相同,但分数不同的记录,如 9和10

select * from table1 a
where exists (select 1 from table1
where studername=a.studername
and Curriculum_name=a.Curriculum_name
and soce<>a.soce)
--(4)要合并查出他们的记录

select * from table1
union all
select * from table2
union all
select * from table3

--(5)有字段A ,B,,把2表中字A+B没有重复的记录合并查询出来,
查询出来的记录也不要出现重复.
--不明白什么意思



34,594

社区成员

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

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