Oracle Case When
1.建立測試表:
DROP SEQUENCE student_sequence;
CREATE SEQUENCE student_sequence START WITH 10000 INCREMENT BY 1;
DROP TABLE students;
CREATE TABLE students (
id NUMBER(5) PRIMARY KEY,
first_name VARCHAR2(20),
last_name VARCHAR2(20),
major VARCHAR2(30),
current_credits NUMBER(3),
grade varchar2(2));
INSERT INTO students (id, first_name, last_name, major, current_credits,grade)
VALUES (student_sequence.NEXTVAL, 'Scott', 'Smith', 'Computer Science', 98,null);
INSERT INTO students (id, first_name, last_name, major, current_credits,grade)
VALUES (student_sequence.NEXTVAL, 'Margaret', 'Mason', 'History', 88,null);
INSERT INTO students (id, first_name, last_name, major, current_credits,grade)
VALUES (student_sequence.NEXTVAL, 'Joanne', 'Junebug', 'Computer Science', 75,null);
INSERT INTO students (id, first_name, last_name, major, current_credits,grade)
VALUES (student_sequence.NEXTVAL, 'Manish', 'Murgratroid', 'Economics', 66,null);
commit;
2.檢視相應資料
SQL> select * from students;
ID FIRST_NAME LAST_NAME MAJOR CURRENT_CREDITS GR
------ ----------------- ------------------- ----------- ----------------- -----
10000 Scott Smith Computer Science 98
10001 Margaret Mason History 88
10002 Joanne Junebug Computer Science 75
10003 Manish Murgratroid Economics 66
3.更新語句
update students
set grade = (
select grade from
(
select id,
case when current_credits > 90 then 'a'
when current_credits > 80 then 'b'
when current_credits > 70 then 'c'
else 'd' end grade
from students
) a
where a.id = students.id
)
/
4.更新後結果
SQL> select * from students;
ID FIRST_NAME LAST_NAME MAJOR CURRENT_CREDITS GR
------------- ---------------- -------------------- ---------------------------- ----
10000 Scott Smith Computer Science 98 a
10001 Margaret Mason History 88 b
10002 Joanne Junebug Computer Science 75 c
10003 Manish Murgratroid Economics 66 d
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/92289/viewspace-1012939/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ORACLE CASE WHEN 及 SELECT CASE WHEN的用法Oracle
- Oracle的order by case whenOracle
- oracle plsql case when_end case小記OracleSQL
- Oracle case when改寫SQLOracleSQL
- ORACLE多欄位CASE WHENOracle
- 案例:oracle中case when的用法Oracle
- SQL Case WhenSQL
- mysql case when then 使用MySql
- Oracle vs PostgreSQL Develop(30) - Index&Case whenOracleSQLdevIndex
- oracle面試題[關於case when的用法]Oracle面試題
- ORACLE SQL開發where子句之case-whenOracleSQL
- Case when 支援變數變數
- case when遇上null值Null
- Oracle 條件索引 case when 報錯解決方案Oracle索引
- sqlserver與oracle case when else ,isnull語法差別SQLServerOracleNull
- mysql中case when的使用MySql
- mysql中的case when 與if()MySql
- SQL中的CASE WHEN使用SQL
- PL/SQL Case when應用SQL
- sql case when, Exist ,group by ,聚合SQL
- SQL Server CASE WHEN ... THEN ... ELSE ... ENDSQLServer
- plsql_case when_end case學習小例SQL
- SQLServer使用case when中的order bySQLServer
- SQL Case when 的使用方法SQL
- plsql_case when_if else endifSQL
- sql中case when的小學SQL
- SQL中的case when then else end用法SQL
- sql server select case when的用法SQLServer
- Oracle group by與case when統一單位後統計數量Oracle
- MySQL 的CASE WHEN 語句使用說明MySql
- PLSQL條件(CASE WHEN)語句小應用SQL
- ORA-00937——Oracle中GROUP BY搭配CASE WHEN的一則SQL報錯OracleSQL
- oracle caseOracle
- 136-MySQL5.17 update更新[case when then end]的使用MySql
- 【06】把 Elasticsearch 當資料庫使:CASE WHEN 聚合Elasticsearch資料庫
- sqlserver使用order by case when進行優先順序排序SQLServer排序
- SQL語句case when外用sum與count的區別SQL
- SQL中的case when then else end用法 【詳細】轉載SQL