CASE Expression
In this page:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
END
CASE column_name
WHEN value1 THEN result1
ELSE default_result
END
Simple CASE Expression
एक simple CASE expression एक value को options की एक list के against compare करता है, एक switch statement जैसा। यह programming में एक switch statement जैसे act करता है, expression को बारी-बारी हर WHEN value के against match करते हुए जब तक एक succeed न हो जाए।
उदाहरण: Simple CASE Expression
CREATE TABLE orders (id INT, status TEXT);
INSERT INTO orders VALUES (1, 'P'), (2, 'S'), (3, 'C');
SELECT id, CASE status
WHEN 'P' THEN 'Pending'
WHEN 'S' THEN 'Shipped'
WHEN 'C' THEN 'Cancelled'
ELSE 'Unknown'
END AS status_label
FROM orders;
Searched CASE Expression
एक searched CASE expression एक single value compare करने के बजाय हर option के लिए complex logical conditions evaluate करता है। यह range checks और कई operators की अनुमति देता है, जैसे numeric ranges के आधार पर scores को letter grades में grade करना।
उदाहरण: Searched CASE Expression
CREATE TABLE scores (id INT, score INT);
INSERT INTO scores VALUES (1, 95), (2, 72), (3, 50);
SELECT id, CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 70 THEN 'B'
ELSE 'F'
END AS grade
FROM scores;
Math Operations के साथ CASE इस्तेमाल करना
आप invalid operations से बचाव के लिए math calculations के अंदर CASE शामिल कर सकते हैं। यह calculation को fail होने देने के बजाय NULL या एक default return करके division by zero जैसी errors से बचने के लिए उपयोगी है।
उदाहरण: Using CASE with Math Operations
CREATE TABLE items (id INT, total INT, quantity INT);
INSERT INTO items VALUES (1, 100, 5), (2, 50, 0);
SELECT id, CASE WHEN quantity = 0 THEN NULL ELSE total / quantity END AS unit_price
FROM items;
Nested CASE Expressions
आप multi-level decision logic के लिए दूसरे CASE expressions के अंदर CASE expressions nest कर सकते हैं। यह hierarchical check requirements handle करने में मदद करता है, हालाँकि deeply nested CASE statements पढ़ना मुश्किल हो सकते हैं और refactor करना worth हो सकता है।
उदाहरण: Nested CASE Expressions
CREATE TABLE users (id INT, role TEXT, active INT);
INSERT INTO users VALUES (1, 'admin', 1), (2, 'admin', 0), (3, 'member', 1);
SELECT id, CASE
WHEN role = 'admin' THEN CASE WHEN active = 1 THEN 'Active Admin' ELSE 'Inactive Admin' END
ELSE 'Member'
END AS label
FROM users;
Aggregations के साथ SELECT में CASE
SUM जैसे aggregate functions के अंदर CASE इस्तेमाल करना आपको conditional counts और custom calculations perform करने देता है, जैसे एक single query में सिर्फ paid mark किए गए orders sum करना बजाय अलग filtered queries चलाने के।
उदाहरण: CASE in SELECT with Aggregations
CREATE TABLE orders (id INT, status TEXT, total INT);
INSERT INTO orders VALUES (1, 'paid', 100), (2, 'unpaid', 50), (3, 'paid', 75);
SELECT SUM(CASE WHEN status = 'paid' THEN total ELSE 0 END) AS paid_total
FROM orders;
ENDभूल जाना, या बिनाENDकेELSEलिखना, जो एक syntax error है।ELSEछोड़ देना और यह surprise होना कि unmatched rowsNULLreturn करती हैं।- एक searched
CASEमें conditions को गलत order में रखना, क्योंकि पहला trueWHENजीतता है।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: