34,594
社区成员
发帖
与我相关
我的任务
分享
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
--(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没有重复的记录合并查询出来,
查询出来的记录也不要出现重复.
--不明白什么意思