SQL入門之3 函式2
-- decode函式
select customer#,state
,decode(state,'CA',.08,'FL',.07,0) sales_tr
from customers;
-- 使用DECODE比對字串
SELECT last_name, job_id, salary,
DECODE(job_id, 'IT_PROG', 1.10*salary,
'ST_CLERK', 1.15*salary,
'SA_REP', 1.20*salary,
salary)
REVISED_SALARY
FROM employees;
-- 使用DECODE測定數值範圍
SELECT last_name, salary,
DECODE (TRUNC(salary/2000, 0),
0, 0.00,
1, 0.09,
2, 0.20,
3, 0.30,
4, 0.40,
5, 0.42,
6, 0.44,
0.45) TAX_RATE
FROM employees
WHERE department_id = 80;
-- 例子1:下面哪個SQL顯示結果一樣嗎?
select to_char(orderdate,'MM/DD') from orders;
select to_char(orderdate,'fmMM/DD') from orders;
-- 例子2:為什麼nvl(sysdate,'aa')會返回一個錯誤?
-- 不一樣,fm在to_char函式中的作用是禁用fill mode
-- 使用nvl時型別需要一致或者可轉換
-- 有關null的函式
NVL 'Converts a null value to an actual value'
NVL2 'If expr1 is not null, NVL2 returns expr2. If expr1 is null, NVL2 returns expr3. The argument expr1can have any data type.'
NULLIF 'Compares two expressions and returns null if they are equal, or the first expression if they are not equal'
COALESCE 'Returns the first non-null expression in the expression list'
-- 在使用COALESCE時,也要注意list中的型別要一致
NULLIF 類似 CASE WHEN expr1 = expr2 THEN NULL ELSE expr1 END
-- case when函式
select case when 1=2 then null else 'abc' end from dual;
select case 'abc' when 'abc' then 'ok' else 'err' end from dual;
-- 入職日期長短的圖示
select ename,lpad('*',round(months_between(sysdate,hiredate)/6),'*') from emp;
-- 一個測試題,要求寫一個SQL,結果如下
ENAME sql
---------- ---------------
SMITH $$$$$$$$$$$$800
ALLEN $$$$$$$$$$$1600
WARD $$$$$$$$$$$1250
JONES $$$$$$$$$$$2975
MARTIN $$$$$$$$$$$1250
BLAKE $$$$$$$$$$$2850
CLARK $$$$$$$$$$$2450
SCOTT $$$$$$$$$$$3000
KING $$$$$$$$$$$5000
TURNER $$$$$$$$$$$1500
ADAMS $$$$$$$$$$$1100
JAMES $$$$$$$$$$$$950
FORD $$$$$$$$$$$3000
MILLER $$$$$$$$$$$1300
-- 參考
select ename,lpad(trim(to_char(sal,'$99999')),15,'$') from emp;
-- 一個測試題
-- 原有資料
select ename,hiredate from emp;
ENAME HIREDATE
---------- ---------
SMITH 17-DEC-80
ALLEN 20-FEB-81
WARD 22-FEB-81
JONES 02-APR-81
MARTIN 28-SEP-81
BLAKE 01-MAY-81
CLARK 09-JUN-81
SCOTT 09-DEC-82
KING 17-NOV-81
TURNER 08-SEP-81
ADAMS 12-JAN-83
JAMES 03-DEC-81
FORD 03-DEC-81
MILLER 23-JAN-82
-- 要求列出入職日期6個月後的第一個monday 結果如下,如何寫SQL?
ENAME HIREDATE mydate
---------- --------- ----------------------------------
SMITH 17-DEC-80 Seventeenth of December , 1980
ALLEN 20-FEB-81 Twentieth of February , 1981
WARD 22-FEB-81 Twenty-Second of February , 1981
JONES 02-APR-81 Second of April , 1981
MARTIN 28-SEP-81 Twenty-Eighth of September , 1981
BLAKE 01-MAY-81 First of May , 1981
CLARK 09-JUN-81 Ninth of June , 1981
SCOTT 09-DEC-82 Ninth of December , 1982
KING 17-NOV-81 Seventeenth of November , 1981
TURNER 08-SEP-81 Eighth of September , 1981
ADAMS 12-JAN-83 Twelfth of January , 1983
JAMES 03-DEC-81 Third of December , 1981
FORD 03-DEC-81 Third of December , 1981
MILLER 23-JAN-82 Twenty-Third of January , 1982
-- 參考答案
select ename,hiredate,
to_char(
next_day(add_months(hiredate,6),'monday'),
'fmDdspth "of" Month "," yyyy'
) mydate from emp
-- 一個測試題
-- 源資料
SQL> select ename,comm from emp;
ENAME COMM
---------- ----------
SMITH
ALLEN 300
WARD 500
JONES
MARTIN 1400
BLAKE
CLARK
SCOTT
KING
TURNER 0
ADAMS
JAMES
FORD
MILLER
-- 寫SQL,結果如下
ENAME DECODE(TO_CHAR
---------- --------------
SMITH No Commission
ALLEN 300
WARD 500
JONES No Commission
MARTIN 1400
BLAKE No Commission
CLARK No Commission
SCOTT No Commission
KING No Commission
TURNER No Commission
ADAMS No Commission
JAMES No Commission
FORD No Commission
MILLER No Commission
-- 參考答案
SQL> select ename,decode(to_char(nvl(comm,0)),0,'No Commission',to_char(comm)) from emp;
/** 又一個考題
12. Create a query that displays the employees’ last names and indicates the amounts of their annual
salaries with asterisks. Each asterisk signifies a thousand doll ars. Sort the data in descending order of
salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.
SMITH********
ALLEN****************
WARD*************
JONES******************************
MARTIN*************
BLAKE*****************************
CLARK*************************
SCOTT******************************
KING**************************************************
TURNER***************
ADAMS***********
JAMES**********
FORD******************************
MILLER*************
*/
-- 我的參考答案
select ename||lpad('*',round(sal/100),'*') from emp;
/*
13. Using the DECODE function, write a query that displays the grade of all employees based on the value
of the column JOB_ID, as per the following data:
Job Grade
AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
None of the above 0
JOB D
--------- -
CLERK B
SALESMAN 0
SALESMAN 0
MANAGER C
SALESMAN 0
MANAGER C
MANAGER C
ANALYST A
PRESIDENT D
SALESMAN 0
CLERK B
CLERK B
ANALYST A
CLERK B
14. Rewrite the statement in the preceding question using the CASE syntax.
*/
-- 我的參考答案
select job,decode(job,'ANALYST','A','CLERK','B','MANAGER','C','PRESIDENT','D','0') FROM EMP;
SELECT JOB,
case job
when 'ANALYST' THEN 'A'
WHEN 'CLERK' THEN 'B'
WHEN 'MANAGER' THEN 'C'
ELSE '0' END
FROM EMP
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/271063/viewspace-908569/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL入門之2 函式1SQL函式
- PHP入門之函式PHP函式
- 3.JavaScript函式入門JavaScript函式
- Python 3 快速入門 2 —— 流程控制與函式Python函式
- SQL輕鬆入門(5):視窗函式SQL函式
- Python入門之函式呼叫(二)Python函式
- go語言入門之-函式和方法Go函式
- 尤拉函式入門函式
- 生成函式入門函式
- 前端入門——函式前端函式
- 11函式入門函式
- 《MySQL 入門教程》第 14 篇 MySQL 常用函式之數學函式MySql函式
- SQL入門之6 sql*plusSQL
- Python入門-函式Python函式
- 集合與函式入門函式
- Oracle PL/SQL 之 函式OracleSQL函式
- Sql Server函式全解(2):數學函式SQLServer函式
- [譯] 函式式 JavaScript 快速入門函式JavaScript
- Go語言入門系列(六)之再探函式Go函式
- ES6入門之函式的擴充套件函式套件
- Python 入門之經典函式例項(二)Python函式
- SQL入門之11 DatabaseTransactionsSQLDatabase
- SQL入門之7 鎖SQL
- python入門:range函式Python函式
- JavaScript入門-函式function(二)JavaScript函式Function
- day07-函式入門函式
- JavaScript入門③-函式(2)原理{深入}執行上下文JavaScript函式
- MySQL入門系列:查詢簡介(三)之表示式和函式MySql函式
- Sql Server函式全解(五)之系統函式SQLServer函式
- 3-Python 函式(2)Python函式
- php函式入門學習(陣列常見函式2 & 檔案基礎讀寫)PHP函式陣列
- zookeeper入門(3)API常用函式功能與引數詳解API函式
- 數論函式從入門到進門函式
- SQL入門之10 MERGESQL
- SQL入門之9使用defaultSQL
- python入門必會的助手函式:dir()函式Python函式
- C++入門記-建構函式和解構函式C++函式
- Python3 《零基礎小白從入門到實戰》之“初識函式”Python函式