【正文】
t from dual where nullnull。 最常用的莫過于 bitand運算符 。 Oracle中的特殊判式 除了邏輯運算之外, Oracle提供了一些特殊判式。本節(jié)將著重介紹以下幾種判式。 In:集合成員測試。 is null:空值判斷。 exists:存在性判斷。這些值可以為數(shù)值型、字符串和日期型。 select * from t_employees where employee_id between 1 and 5。字符串是按照字母表的順序進行比較,而日期型是按照日期的先后順序進行比較。b39。b39。c39。b39。bc39。c39。但是,效率上要比后者差。 select * from t_employees where status in(39。, 39。)。NEW39。ACT39。 like—— 模式匹配 like判式的最大特點在于,可以使用通配符。 select * from t_employees where employee_name like 39。 如果要求字符串中含有原義字符“ %”,例如,含有百分比的字符串。鐘 \%39。\39。那么在“鐘 \%”中的“ %”不再表示通配符,而是表示原義字符“ %”。 is null—— 空值判斷 在邏輯判斷中,對于列值為空的判斷,不能使用 =或者。例如,為了獲取表 t_employees中員工信息不全的記錄,可以利用如下所示的查詢語句。 exists—— 存在性判斷 in判式用于判斷表的列值是否存在于列表(集合)中。例如,為了查詢出表 t_employees所存儲的員工信息中,哪些員工存在于工資表中,即可利用 exists判式。 all, some, any—— 數(shù)量判斷 all, some和 any判式的作用對象為記錄集合。例如,在員工工資表 t_salary中,為了查找高于 id為 4和 5的工資信息,即可使用 all判式。 select * from t_salary where salary all(select distinct salary from t_salary where employee_id = 4 or employee_id = 5)。 此時的 some判式實際相當于邏輯運算中的 or運算,即salary6000 or salary7000。 Oracle高級函數(shù) —— 分析函數(shù)與窗口函數(shù) Oracle中的分析函數(shù)具有非常強大的功能。窗口函數(shù)總是為查詢過程中的當前記錄提供一個相關記錄集,而且隨著當前記錄的推移,相應的記錄集也會隨之改變,這非常類似于“滑動窗”的概念。本節(jié)將通過實例來講述分析函數(shù)和窗口函數(shù)的使用。常用的排名函數(shù)有 rank()、 dense_rank()和row_number()。 rank()函數(shù)在排名過程中,具有跳躍的特點。 select student_name, rank() over(order by student_age) position from students。 select student_name, row_number() over(order by student_age) position from students。 現(xiàn)欲統(tǒng)計各員工的工資在各自部門的高低情況,則可以利用partition by進行分區(qū),然后利用分析函數(shù)對分區(qū)內的記錄進行統(tǒng)計 select t.*, dense_rank() over(partition by department order by salary) position from salary t order by 另外一種常見需求為,在獲得員工工資的同時,也需要部門所有員工的工資總額 select t.*, sum(salary) over(partition by department) total_salary, round(avg(salary) over(partition by department)) average_salary from salary t order by employee_id 注意, avg(salary) over(partition by department)是不可分割的一個整體。另外,利用 partition by進行分區(qū)之后,當前記錄總是處于某個分區(qū)中,此時的窗口即為該分區(qū)。而對于該記錄集,可以使用窗口子句,來進一步限制窗口范圍。 1. rows子句 select employee_id, employee_name, sum(salary) over(order by employee_id rows between 1 preceding and 1 following) three_total from salary rows子句因為和位置相關,因此,在窗口函數(shù)中必須含有排序子句 order by。例如,對于 employee_id為 1的記錄,排序之后,該記錄為第一條記錄,不存在前一條記錄,因此只返回兩條記錄,而求和操作返回的實際為 employee_id為 1和 2的員工的工資總和 10500。 select employee_id, employee_name, count(1) over(partition by department order by salary range between 500 preceding and 500 following) as employee_count from salary order by employee_id 3. unbounded和 current row 在 rows和 range子句中,除了使用具體的數(shù)值來決定窗口的大小之外,還可以使用關鍵字 unbounded和 current row。 主要的分析函數(shù) 分析函數(shù)作用對象為窗口函數(shù)所捕獲的記錄集,因此,分析函數(shù)具有聚合函數(shù)的特點,大多數(shù)的聚合函數(shù),如 sum()、 count()、 max()等都能作為分析函數(shù)出現(xiàn)。 1. fist_value()函數(shù)的使用 select distinct department, first_value(employee_name) over(partition by department order by salary) employee_name, first_value(salary) over(partition by department order by salary) salary from salary 2. last_value()函數(shù)的使用 3. lead()函數(shù)的使用 select employee_id, employee_name, salary, lead(employee_name, 1, 39。) over (partition by department order by salary) prev_name from salary order by employee_id 4. lag()函數(shù)的使用 本章實例 Oracle雖然內置了很多函數(shù),但是并不能滿足日常開發(fā)中的應用。 create or replace function is_date (param varchar2) return varchar2 is val date。 39。yyyymmdd hh24:mi:ss39。 return 39。 exception when others then return 39。 end。abc39。 select is_date(39。) from dual。對于內置函數(shù),特別需要注意的是聚合函數(shù)的使用。對于特殊判式,尤其應該注意的是 like判式的使用, like判式使用中,通配符只有“ %”和“ _”兩種,要注意通配符和正則表達式的區(qū)別。對于窗口函數(shù),要重點理解分區(qū)和排序的工作流程,尤其需要注意的是,對于排序中,具有相同列值的記錄的處理。在統(tǒng)計和生成復雜報表時,分析函數(shù)和窗口函數(shù)有著廣泛的應用,尤其對于復雜統(tǒng)計,利用這兩種函數(shù)往往可以起到事半功倍的效果。 2.簡述 like判式的使用方法。 4.簡述窗口函