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

Post on 08-Jan-2018

229 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

鍾若君 reginajongs@gmail.com

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

Comparison Operations

Operator

=

>

>=

<

<=

<>

Meaning

Equal to

Greater than

Greater than or equal to

Less than

Less than or equal to

Not equal to

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)

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

Between > <

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

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

述句變形

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)];

SQL Functions

FunctionFunctionInputInput

arg 1arg 1

arg 2arg 2

arg arg nn

Function Function performs actionperforms action

OutputOutput

ResultResultvaluevalue

Two Types of SQL Functions

FunctionsFunctions

Single-row Single-row functionsfunctions

Multiple-rowMultiple-rowfunctionsfunctions

Null Value

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

• NULL is not the same as zero or space

NULL comparison

• select * • from Employees • where Manager_ID = null

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

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

Null Prob.

• NVL• default value

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

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

stories ') as CamelCase from dual; 

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

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)

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

Numeric Function

Case When

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

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

Two Types of SQL Functions

FunctionsFunctions

Single-row Single-row functionsfunctions

Multiple-rowMultiple-rowfunctionsfunctions

Aggregate Function

• Group Function

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

Have a nice Weekend

Exercise(s)

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

top related