1.--删除重复值,先group by
方法一:
delete from o_trace_log t2
where t2.rowid<
(select max(rowid) from o_trace_log t1
where t1.trace_no=t2.trace_no and t1.trace_cnt=t2.trace_cnt and t1.tx_date=t2.tx_date
group by t1.trace_no,t1.trace_cnt,t1.tx_date)
方法二:
delete from 表名 t1 where t1.rowid<(select max(rowid) from 表 t2 where t1.id=t2.id)
--删除重复值(工作中使用效率高)
--1)把原表去重,把结果集放在临时表中
create table o_trace_log_bak as select distinct * from o_trace_log
--2)把原表清空
Truncate table o_trace_log
--3)在把数据导进原表
Insert into o_trace_log select * from o_trace_log_bak
--4)删除临时表
drop table o_trace_log_bak;
2.格式转换
学生科目 成绩
student1 语文 80
student1 数学 70
student1 英语 60
student2 语文 90
student2 数学 80
student2 英语 100
将上述表结构信息转换为一下格式
学生 语文 数学 英语
student1 80 70 60
student2 90 80 100
select t1.学生,t1.成绩 as语文 ,t2.成绩 as 数学, t3.成绩 as 英语
from 表 t1,表 t2,表 t3
where t1.科目=’语文’ and t2.科目=‘数学’ and t3.科目=‘英语’ and t1.学生=t2.学生 and t2.学生=t3.学生;
3.从数据库中随机取50条
Select * from (select * from emp order by dbms_random.random) where rownum <= 50
4.在非工作时间不允许对emp表进行增删改操作,工作时间周一~周五 早九晚五
create or replace trigger tg_cfq
before insert or delete or update on emp for each row
begin
if (to_char(sysdate,'day') in('星期六','星期日'))
or (to_char(sysdate,'HH24')<9) or (to_char('HH24')>17) then
raise_application_error(10086,'非工作时间不允许对emp表进行操作,违令者斩'); --引发系统异常
end if;
end;
5.-------分页
--以每5行为一页,查看第二页
select * from
(select round(rownum/5) rn,e.* from emp e)
where rn=2
6.-----行转列
方法一:
把select deptno,count(*) from emp group by deptno; 进行语句行转列显示
select sum(decode(deptno,10,1)) "10",
sum(decode(deptno,20,1)) "20",
sum(decode(deptno,30,1)) "30"
from emp
方法二:
select count(case deptno when 10 then 1 end) "10",
count(case deptno when 20 then 1 end) "20",
count(case deptno when 30 then 1 end) "30"
from emp