Introduction to the use of Oracle case functions _oracle

Source: Internet
Author: User

1. Create a Test table:

Copy Code code as follows:

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. View the corresponding data

Copy Code code as follows:

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 statements

Copy Code code as follows:

Update students
Set grade = (
Select grade from
(
Select ID,
Case when Current_credits > Then ' a '
When current_credits > then ' B '
When current_credits > Then ' C '
Else ' d ' End grade
From students
) A
where a.id = Students.id
)
/

4. Post-Update results

Copy Code code as follows:

Sql> SELECT * from students;

ID first_name last_name MAJOR current_credits GR
---------- -------------------- -------------------- ------------------------------ --------------- --
10000 Scott Smith Computer Science
10001 Margaret Mason History b
10002 Joanne Junebug Computer Science C
10003 Manish murgratroid Economics D

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.