oracle筆試

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章