这个SQL语句怎么写?
student 表:
ID Sno Sname cji
1 001 张三 58
2 002 李四 69
3 003 王五 89
4 001 张三 58
5 002 李四 69 (1) 把表中除了ID号不同,而其它完全
相同的元组删除。如1 和4 把 4 删除(2) 查询该表,得到如下结果如下: 人数
及格 2
不及格 2
student 表:
ID Sno Sname cji
1 001 张三 58
2 002 李四 69
3 003 王五 89
4 001 张三 58
5 002 李四 69 (1) 把表中除了ID号不同,而其它完全
相同的元组删除。如1 和4 把 4 删除(2) 查询该表,得到如下结果如下: 人数
及格 2
不及格 2
delete a from student a
where exists (select 1 from student where sno = a.sno and sname = a.sname and cji = a.cji and id < a.id)2.
select '及格',sum(case when cji >= 60 then 1 end) as 人数 from student
union
select '不及格',sum(case when cji < 60 then 1 end) from student
2。同上
相同的元组删除。如1 和4 把 4 删除
delete from tb where id in (
select sno,sname,cji,max(id) as id from tb group by sno , sname , cji having count)(*) > 1(2) 查询该表,得到如下结果如下: 人数
及格 2
不及格 2
select 情况='及格',sum(case when cji >= 60 then 1 end) as 人数 from student
union
select 情况='不及格',sum(case when cji < 60 then 1 end) as 人数 from student
相同的元组删除。如1 和4 把 4 删除
if object_id('pubs..tb') is not null
drop table tb
go
create table tb(
ID varchar(10),
Sno varchar(10),
Sname varchar(10),
cji int
)
insert into tb(ID,Sno,Sname,cji) values('1','001','张三',58)
insert into tb(ID,Sno,Sname,cji) values('2','002','李四',69)
insert into tb(ID,Sno,Sname,cji) values('3','003','张三',89)
insert into tb(ID,Sno,Sname,cji) values('4','001','张三',58)
insert into tb(ID,Sno,Sname,cji) values('5','002','李四',69)
godelete from tb where id in
(
select id from
(
select sno,sname,cji,max(id) as id from tb group by sno , sname , cji having count(*) > 1
) t1
)drop table tbID Sno Sname cji
---------- ---------- ---------- -----------
1 001 张三 58
2 002 李四 69
3 003 张三 89(所影响的行数为 3 行)(2) 查询该表,得到如下结果如下: 人数
及格 2
不及格 2
你的结果有错.
应该为
情况 人数
------ -----------
不及格 2
及格 3if object_id('pubs..tb') is not null
drop table tb
go
create table tb(
ID varchar(10),
Sno varchar(10),
Sname varchar(10),
cji int
)
insert into tb(ID,Sno,Sname,cji) values('1','001','张三',58)
insert into tb(ID,Sno,Sname,cji) values('2','002','李四',69)
insert into tb(ID,Sno,Sname,cji) values('3','003','张三',89)
insert into tb(ID,Sno,Sname,cji) values('4','001','张三',58)
insert into tb(ID,Sno,Sname,cji) values('5','002','李四',69)
goselect 情况='及格',sum(case when cji >= 60 then 1 end) as 人数 from tb
union
select 情况='不及格',sum(case when cji < 60 then 1 end) as 人数 from tbdrop table tb情况 人数
------ -----------
不及格 2
及格 3
(所影响的行数为 2 行)
where exists (select 1 from student where sno = a.sno and sname = a.sname and cji = a.cji and id < a.id)所以下面的a指的其实就是上面的student表,
这个查询的思想是运用了自联接
假设现在你要删除记录的表是a,他里面的数据跟student是完全一样的
那这个语句理解起来就好多了
你可以自己画个图考虑一下,那样比较直观
create table student(ID int, Sno char(3), Sname nvarchar(10), cji int)
insert student select
1, '001', '张三', 58
union all select 2, '002', '李四', 69
union all select 3, '003', '王五', 89
union all select 4, '001', '张三', 58
union all select 5, '002', '李四', 69 --1
begin tran
delete student where ID in(
select max(ID) from student
group by Sno, Sname, cji
having count(*)>1
)--2
select 类别='及格', 人数=count(*) from student where cji>=60
union all
select 类别='不及格', 人数=count(*) from student where cji<60
drop table student