鍾若君 recall in our rdb….many many tables (each table looks like excel sheet) column/field(s)...

26
鍾鍾鍾 reg [email protected]

Upload: rudolf-wright

Post on 08-Jan-2018

229 views

Category:

Documents


0 download

DESCRIPTION

述句變形 select column/function ( 更多樣的呈現 ) from table where comparison (condition) order by column/function

TRANSCRIPT

Page 1: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

鍾若君 [email protected]

Page 2: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Recall

in Our RDB….many many tables(each table looks like Excel Sheet)

Column/field(s)

Row(s)

Page 3: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

述句變形• select column/function ( 更多樣的呈現 ) from table where comparison (condition) order by column/function

Page 4: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Comparison Operations

Operator

=

>

>=

<

<=

<>

Meaning

Equal to

Greater than

Greater than or equal to

Less than

Less than or equal to

Not equal to

Page 5: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Other Comparison Operators

Operator

BETWEEN...AND...

IN(list)

LIKE

IS NULL

Meaning

Between two values (inclusive)

Match any of a list of values

Match a character pattern

Is a null value (**talk later)

Page 6: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

BETWEEN Operator

LAST_NAME SALARY---------- ---------Zlotkey 10500Tucker 10000King 10000Vishney 10500Bloom 10000Baker 10000

SQL> SELECT last_name, salary 2 FROM employees 3 WHERE salary BETWEEN 10000 AND 10500;

Lowerlimit

Higherlimit

Page 7: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Between > <

•between 10000 and 10500• Ie. >= 10000 and <= 10500•以大小於 改寫上例•between 10500 and 10000 ?

Page 8: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

• select column/function ( 更多樣的呈現 ) from table where comparison (condition) order by column/function

述句變形

Page 9: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Built-In Functions

• Restrict the rows returned by using the WHERE clause.

• The WHERE clause follows the FROM clause.

SELECT [DISTINCT] {*| column [alias], ...}FROM table[WHERE condition(s)];

Page 10: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

SQL Functions

FunctionFunctionInputInput

arg 1arg 1

arg 2arg 2

arg arg nn

Function Function performs actionperforms action

OutputOutput

ResultResultvaluevalue

Page 11: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Two Types of SQL Functions

FunctionsFunctions

Single-row Single-row functionsfunctions

Multiple-rowMultiple-rowfunctionsfunctions

Page 12: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Null Value

• NULL is a value that is unavailable,unassigned,unknown,or inapplicable

• NULL is not the same as zero or space

Page 13: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

NULL comparison

• select * • from Employees • where Manager_ID = null

• select * • from Employees • where Manager_ID is null• /* where Manager_ID is not null*/

Page 14: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Function NVL to manage NULL• select nvl(Manager_ID, -999) as Manager_ID • from Employees

• select nvl(Commission_PCT, 0) as Manager_ID • from Employees

• select nvl(Commission_PCT, 0) as CommissionRate, a.* • from Employees a

Page 15: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Null Prob.

• NVL• default value

Page 16: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

String Function• select First_Name || ’ ‘ || Last_Name as Name, LPAD(First_Name ,25, ‘*') as FName, RPAD(First_Name ,25, ‘*') as LName, REVERSE(Last_Name) as RName, Substr(Last_Name, 1, 3) as subName, upper(Last_Name) as UName, lower(Last_Name) as lName, Legnth(Trim(First_Name)) as TNameBtyeFrom Employees

Page 17: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

INITCAP• 將每一個字的字首轉換成大寫• select initcap(‘cAtch up on the latest news

stories ') as CamelCase from dual; 

Page 18: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Date Function

• select to_char(Hire_Date, 'yyyy') as HireY, LAST_DAY (Hire_Dat) as LastDay, Last_Name ||','|| First_Name as Name from Employees where Salary between 1000 and 5000 order by to_char(Hire_Date, 'yyyy') desc, Last_Name

Page 19: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Date Format

YYYY

MM

DD

HH24

SS

Full year in numbers

Two-digit value for month(1~12)

Day of Month (1~31)

Seconds (0~59)

Hour of Day(1~24)

MI Minutes (0-59)

Page 20: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

•select TRUNC(125.815, 0 ) , -- truncated ROUND(123.234, 2) , ROUND(123.235, 2) from Dual

Numeric Function

Page 21: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Case When

• 是 SQL 用來做為 if-then-else 之類邏輯的關鍵字•CASE WHEN sex = '1' THEN ' 男 ' WHEN sex = '2' THEN ' 女 ' ELSE ' 其他 ' END

Page 22: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Case when• select last_name||',' ||first_Name, Salary,• case when salary >= 24000 then ' 豪豪野人 '• when salary >= 17000 then ' 豪野人 '• else '---' • end SalaryComment• from Employees a order by salary desc

• select last_name||',' ||first_Name, Salary,• case when salary >= 17000 then ' 豪野人 '• when salary >= 24000 then ' 豪豪野人 '• else '---' • end SalaryComment• from Employees a order by salary desc

Page 23: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Two Types of SQL Functions

FunctionsFunctions

Single-row Single-row functionsfunctions

Multiple-rowMultiple-rowfunctionsfunctions

Page 24: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Aggregate Function

• Group Function

•select count(*) as counter, sum(Salary) as TotalSalary from Employees;

Page 25: 鍾若君 Recall in Our RDB….many many tables (each table looks like Excel Sheet) Column/field(s) Row(s)

Have a nice Weekend

Exercise(s)

1). 如何以 to_char 來顯示日期欄位的月份2). 請追加 employees 中性別欄位 , 值為 F 與 M3). 根據上例將性別顯示為「男」、「女」4). 請練習 類似於 case when 的 decode