Pernyataan SQL CASE sangat kuat dan serbaguna. Ini dapat digunakan dalam pernyataan SELECT, UPDATE dan INSERT. Bahkan dapat digunakan dalam klausa ORDER BY dan GROUP BY. Mari kita periksa satu per satu.
Sintaks
Pertama periksa sintaks dari pernyataan CASE:
CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... WHEN conditionN THEN resultN ELSE result END;
Persiapan Data
Pertama pilih database pilihan Anda dan jalankan kueri di bawah ini untuk membuat tabel "Orang" dan mengisi dengan data.
IF OBJECT_ID('Persons', 'U') IS NOT NULL DROP TABLE Persons; GO CREATE TABLE Persons( name varchar(20) ,age int ,sex varchar(2) ) GO INSERT INTO Persons (name,age,sex) values('Bob',5, 'M'); INSERT INTO Persons (name,age,sex) values('Harry',15, 'M'); INSERT INTO Persons (name,age,sex) values('Jasmine',25, 'F'); INSERT INTO Persons (name,age,sex) values('Fanny',65, 'F'); INSERT INTO Persons (name,age,sex) values('Evan',-1, 'N'); GO
PILIH Pernyataan
Mari kita gunakan dalam pernyataan SELECT.
SELECT Name, age as 'Actual Age', CASE WHEN age >= 0 AND age <= 12 THEN 'CHILD' WHEN age >= 13 AND age <= 19 THEN 'TEEN AGE' WHEN age >= 20 AND age <= 40 THEN 'YOUNG' WHEN age >= 41 AND age <= 60 THEN 'MIDDLE AGE' WHEN age >= 61 THEN 'OLD' ELSE 'NOT DEFINED' END as 'New Age using CASE Stmt', sex as 'Actual Gender', CASE WHEN sex = 'M' THEN 'MALE' WHEN sex = 'F' THEN 'FEMALE' WHEN sex = 'N' THEN 'DONT WANT TO DISCLOSE' END as 'New Gender using CASE Stmt' FROM Persons
Keluaran
MASUKKAN Pernyataan
DECLARE @age INT DECLARE @sex VARCHAR(10) SET @age = 40 SET @sex = 'MALE' INSERT INTO Persons (Name, Age, Sex) VALUES( 'Jack', CASE WHEN @age < 0 THEN -1 ELSE @age END, CASE WHEN @sex = 'MALE' THEN 'M' WHEN @sex = 'FEMALE' THEN 'F' ELSE 'N' END )
Keluaran
Jika kita menjalankan pernyataan SELECT pertama kita lagi, maka outputnya adalah:
PERBARUI Pernyataan
DECLARE @age INT SET @age = -4 UPDATE Persons SET age = CASE WHEN @age < 0 THEN -1 ELSE @age END WHERE Name = 'Bob'
Keluaran
Jika kita menjalankan pernyataan SELECT di atas lagi, maka outputnya adalah:
ORDER DENGAN Klausul
CASE can be added in ORDER BY clause in above SELECT statement SELECT Name, age as 'Actual Age', CASE WHEN age >= 0 AND age <= 12 THEN 'CHILD' WHEN age >= 13 AND age <= 19 THEN 'TEEN AGE' WHEN age >= 20 AND age <= 40 THEN 'YOUNG' WHEN age >= 41 AND age <= 60 THEN 'MIDDLE AGE' WHEN age >= 61 THEN 'OLD' ELSE 'NOT DEFINED' END as 'New Age using CASE Stmt', sex as 'Actual Gender', CASE WHEN sex = 'M' THEN 'MALE' WHEN sex = 'F' THEN 'FEMALE' WHEN sex = 'N' THEN 'DONT WANT TO DISCLOSE' END as 'New Gender using CASE Stmt' FROM Persons ORDER BY CASE WHEN sex='M' THEN age END, CASE WHEN sex='F' THEN Name END
Keluaran
GROUP BY Clause
Sekarang saatnya menjelajahi CASE dalam klausa GROUP BY dalam pernyataan SELECT pertama kami:
SELECT CASE WHEN age <= 40 THEN 'YOUNG' WHEN age >= 41 THEN 'OLD' END as 'New Age using CASE Stmt', COUNT(*) FROM Persons GROUP BY CASE WHEN age <= 40 THEN 'YOUNG' WHEN age >= 41 THEN 'OLD' END
Keluaran
Kami membahas semua kasus topik KASUS hari ini.
Tinggal di rumah! Tetap Aman!
Ini pertama kali diterbitkan di blog saya.