--删除重复记录
--创建student表
create table student(sno number(6) ,sname varchar2(10),sage int);
insert into student values(1,'AA',21);
insert into student values(2,'BB',22);
insert into student values(3,'CC',23);
insert into student values(3,'CC',34);
insert into student values(3,'CC',35);
insert into student values(3,'CC',36);
--删除重复的记录,第一种方法,rowid
--先查看表的rowid,rowid里的信息有:数据库对象号、数据文件号、数据块号、行号。作用唯一标识一行。
--查询rowid是运行最快的方式。
select student.* ,rowid from student;
--第一种方式,删除sno重复并且保证rowid不是最小的
delete student where sno in(select sno from student group by sno having count(*)>1)
and rowid not in
(select min(rowid) from student group by sno having count(*)>1);
--第二种方式,自连接(筛选左表rowid>右边rowid的记录)
select a.sno,a.sname,a.sage,b.sno,b.sname,b.sage from student a,student b where a.sno=b.sno;
delete from student where rowid in (select a.rowid from student a,student b where a.sno=b.sno and a.rowid>b.rowid);
--第三种,嵌套查询(删除rowid>最小rowid的集)
delete from student d where d.rowid>
(select min(rowid) from student x where d.sno=x.sno);
--第四种,嵌套查询,找出学号为3,年龄大于23的)
delete (select * from (select * from student where sno=3) where sage>23);
select * from student;
--group by grouping sets的使用
/*可以用group by grouping sets来进行分组自定义汇总,可以用它来指定你需要的总数组合
其格式为 group by grouping sets ((list),(list)...) 这里的(list) 是圆括号中的一个列序列,
这个组合生成一个总数。要增加一个总和,必须增加一个(null)分组集。
*/