logo

SQL CASE

The SLUČAJ je izjava koja upravlja tipom if-then-else logičkih upita. Ova izjava vraća vrijednost kada navedeni uvjet ima vrijednost True. Kada nijedan uvjet ne dobije vrijednost True, vraća vrijednost dijela ELSE.

Kada nema ELSE dijela i nijedan uvjet ne daje vrijednost True, vraća vrijednost NULL.

U jeziku strukturiranih upita, naredba CASE koristi se u naredbama SELECT, INSERT i DELETE sa sljedeće tri klauzule:

  1. WHERE klauzula
  2. ORDER BY klauzula
  3. GROUP BY klauzula

Ovu naredbu u SQL-u uvijek prati najmanje jedan par naredbi WHEN i THEN i uvijek završava ključnom riječi END.

U relacijskim bazama podataka postoje dvije vrste naredbe CASE:

  1. Jednostavna izjava CASE
  2. Pretražena izjava CASE

Sintaksa naredbe CASE u SQL-u

 CASE WHEN condition_1 THEN statement_1 WHEN condition_2 THEN statement_2 ……. WHEN condition_N THEN statement_N ELSE result END; 

Ovdje naredba CASE procjenjuje svaki uvjet jedan po jedan.

abeceda s brojevima

Ako izraz odgovara uvjetu prve odredbe WHEN, preskače sve daljnje uvjete WHEN i THEN i vraća iskaz_1 u rezultatu.

Ako izraz ne odgovara prvom WHEN uvjetu, uspoređuje se s drugim WHEN uvjetom. Ovaj proces podudaranja nastavit će se sve dok se izraz ne uskladi s bilo kojim WHEN uvjetom.

Ako nijedan uvjet nije usklađen s izrazom, kontrola automatski ide na ELSE dio i vraća svoj rezultat. U sintaksi CASE dio ELSE nije obavezan.

bellfordov algoritam

U sintaksi, CASE i END su najvažnije ključne riječi koje pokazuju početak i završetak CASE izjave.

Primjeri CASE iskaza u SQL-u

Uzmimo tablicu Student_Details, koja sadrži roll_no, ime, ocjene, predmet i grad učenika.

Rola_br Stu_Ime Stu_Predmet Stu_Marks Stu_City
2001. godine Akshay Znanost 92 Noida
2002. godine Radna memorija matematika 49 Jaipur
2004. godine Shyam Engleski 52 Gurgaon
2005. godine jatin Ne Četiri pet Lucknow
2006. godine Manoj Računalo 70 Ghaziabad
2007. godine Sheetal matematika 82 Noida
2008. godine Kosa Znanost 62 Gurgaon
2009. godine Yogesh Engleski 42 Lucknow
2010 Radna memorija Računalo 88 Delhi
2011 Shyam Ne 35 Kanpur

Primjer 1: Sljedeća SQL naredba koristi jedan uvjet WHEN i THEN za naredbu CASE:

 SELECT Roll_No, Stu_Name, Stu_Subject, Stu_marks, CASE WHEN Stu_Marks >= 50 THEN 'Student_Passed' ELSE 'Student_Failed' END AS Student_Result FROM Student_Details; 

Objašnjenje gornjeg upita:

Ovdje naredba CASE provjerava je li Stu_Marks veće od i jednako 50, vraća Student_prošao inače prelazi na DRUGO dio i vraća Student_Pad u Student_Rezultat stupac.

Izlaz:

Rola_br Stu_Ime Stu_Predmet Stu_Marks Student_Rezultat
2001. godine Akshay Znanost 92 Student_prošao
2002. godine Radna memorija matematika 49 Student_Pad
2004. godine Shyam Engleski 52 Student_prošao
2005. godine jatin Ne Četiri pet Student_Pad
2006. godine Manoj Računalo 70 Student_prošao
2007. godine Sheetal matematika 82 Student_prošao
2008. godine Kosa Znanost 62 Student_prošao
2009. godine Yogesh Engleski 42 Student_Pad
2010 Radna memorija Računalo 88 Student_prošao
2011 Shyam Ne 35 Student_Pad

Primjer 2: Sljedeća SQL naredba dodaje više od jednog uvjeta WHEN i THEN naredbi CASE:

 SELECT Roll_No, Stu_Name, Stu_Subject, Stu_marks, CASE WHEN Stu_Marks &gt;= 90 THEN &apos;Outstanding&apos; WHEN Stu_Marks &gt;= 80 AND Stu_Marks = 70 AND Stu_Marks = 60 AND Stu_Marks = 50 AND Stu_Marks <60 50 then 'bad' when stu_marks < 'failed' end as stu_remarks from student_details; pre> <p> <strong>Explanation of above query:</strong> </p> <p>Here, the CASE statement checks multiple WHEN and THEN conditions one by one. If the value of <strong>Stu_Marks</strong> column is greater than or equals to <strong>90</strong> , it returns <strong>Outstanding</strong> otherwise moves to the further WHEN and THEN conditions.</p> <p>If none of the conditions is matched with the <strong>Student_Details</strong> table, CASE returns <strong>the NULL</strong> value in the <strong>Stu_Remarks</strong> column because there is no ELSE part in the query.</p> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <th>Roll_No</th> <th>Stu_Name</th> <th>Stu_Subject</th> <th>Stu_Marks</th> <th>Stu_Remarks</th> </tr> <tr> <td>2001</td> <td>Akshay</td> <td>Science</td> <td>92</td> <td>Outstanding</td> </tr> <tr> <td>2002</td> <td>Ram Math</td> <td>49</td> <td>Failed</td> </tr> <tr> <td>2004</td> <td>Shyam</td> <td>English</td> <td>52</td> <td>Bad</td> </tr> <tr> <td>2005</td> <td>Yatin</td> <td>Hindi</td> <td>45</td> <td>Failed</td> </tr> <tr> <td>2006</td> <td>Manoj</td> <td>Computer</td> <td>70</td> <td>Good</td> </tr> <tr> <td>2007</td> <td>Sheetal</td> <td>Math</td> <td>82</td> <td>Excellent</td> </tr> <tr> <td>2008</td> <td>Parul</td> <td>Science</td> <td>62</td> <td>Average</td> </tr> <tr> <td>2009</td> <td>Yogesh</td> <td>English</td> <td>42</td> <td>Failed</td> </tr> <tr> <td>2010</td> <td>Ram</td> <td>Computer</td> <td>88</td> <td>Excellent</td> </tr> <tr> <td>2011</td> <td>Shyam</td> <td>Hindi</td> <td>35</td> <td>Failed</td> </tr> </table> <p> <strong>Example 3:</strong> </p> <p>Let&apos;s take another Employee_Details table which contains Emp_ID, Emp_Name, Emp_Dept, and Emp_Salary.</p> <table class="table"> <tr> <th>Emp_Id</th> <th>Emp_Name</th> <th>Emp_Dept</th> <th>Emp_Salary</th> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>9000</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Marketing</td> <td>4000</td> </tr> <tr> <td>3</td> <td>Shyam</td> <td>Sales</td> <td>5000</td> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Coding</td> <td>4000</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Marketing</td> <td>5000</td> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>8000</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Coding</td> <td>6000</td> </tr> <tr> <td>3</td> <td>Shyam</td> <td>Coding</td> <td>4000</td> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Marketing</td> <td>8000</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Finance</td> <td>3000</td> </tr> </table> <p> <strong>The following SQL query uses GROUP BY clause with CASE statement:</strong> </p> <pre> SELECT Emp_Id, Emp_Name, Emp_Dept, sum(Emp_Salary) as Total_Salary, CASE WHEN SUM(Emp_Salary) &gt;= 10000 THEN &apos;Increment&apos; ELSE &apos;Constant&apos; END AS Emp_Remarks FROM Employee_Details GROUP BY Emp_id, Emp_Name; </pre> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <th>Emp_Id</th> <th>Emp_Name</th> <th>Emp_Dept</th> <th>Total_Salary</th> <th>Emp_Remarks</th> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>17000</td> <td>Increment</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Marketing</td> <td>9000</td> <td>Decrement</td> </tr> <tr> <td>3</td> <td>Shyam</td> <td>Sales</td> <td>10000</td> <td>Increment</td> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Coding</td> <td>12000</td> <td>Increment</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Marketing</td> <td>8000</td> <td>Decrement</td> </tr> </table> <p> <strong>Example 4: In this example, we use the ORDER BY clause with a CASE statement in SQL:</strong> </p> <p>Let&apos;s take another Employee_Details table which contains Emp_ID, Emp_Name, Emp_Dept, and Emp_Age.</p> <p>We can check the data of Employee_Details by using the following query in SQL:</p> <pre> Select * From Employee_Details; </pre> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <th>Emp_Id</th> <th>Emp_Name</th> <th>Emp_Dept</th> <th>Emp_Age</th> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>23</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Marketing</td> <td>24</td> </tr> <tr> <td>3</td> <td>Balram</td> <td>Sales</td> <td>25</td> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Coding</td> <td>22</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Marketing</td> <td>23</td> </tr> <tr> <td>6</td> <td>Sheetal</td> <td>Finance</td> <td>24</td> </tr> <tr> <td>7</td> <td>Parul</td> <td>Finance</td> <td>22</td> </tr> <tr> <td>8</td> <td>Yogesh</td> <td>Coding</td> <td>25</td> </tr> <tr> <td>9</td> <td>Naveen</td> <td>Marketing</td> <td>22</td> </tr> <tr> <td>10</td> <td>Tarun</td> <td>Finance</td> <td>23</td> </tr> </table> <p>The following SQL query shows all the details of employees in the ascending order of employee names:</p> <pre> SELECT * FROM Employee_Details ORDER BY Emp_Name; </pre> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <th>Emp_Id</th> <th>Emp_Name</th> <th>Emp_Dept</th> <th>Emp_Age</th> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>23</td> </tr> <tr> <td>3</td> <td>Balram</td> <td>Sales</td> <td>25</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Marketing</td> <td>23</td> </tr> <tr> <td>9</td> <td>Naveen</td> <td>Marketing</td> <td>22</td> </tr> <tr> <td>7</td> <td>Parul</td> <td>Finance</td> <td>22</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Marketing</td> <td>24</td> </tr> <tr> <td>6</td> <td>Sheetal</td> <td>Finance</td> <td>24</td> </tr> <tr> <td>10</td> <td>Tarun</td> <td>Finance</td> <td>23</td> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Coding</td> <td>22</td> </tr> <tr> <td>8</td> <td>Yogesh</td> <td>Coding</td> <td>25</td> </tr> </table> <p>If you want to show those employees at the top who work in the Coding Department, then for this operation, you have to use single WHEN and THEN statement in the CASE statement as shown in the following query:</p> <pre> SELECT * FROM Employee_Details ORDER BY CASE WHEN Emp_Dept = &apos;Coding&apos; THEN 0 ELSE 1 END, Emp_Name; </pre> <p> <strong>Output:</strong> </p> <table class="table"> <tr> <th>Emp_Id</th> <th>Emp_Name</th> <th>Emp_Dept</th> <th>Emp_Age</th> </tr> <tr> <td>4</td> <td>Yatin</td> <td>Coding</td> <td>22</td> </tr> <tr> <td>8</td> <td>Yogesh</td> <td>Coding</td> <td>25</td> </tr> <tr> <td>1</td> <td>Akshay</td> <td>Finance</td> <td>23</td> </tr> <tr> <td>3</td> <td>Balram</td> <td>Sales</td> <td>25</td> </tr> <tr> <td>5</td> <td>Manoj</td> <td>Marketing</td> <td>23</td> </tr> <tr> <td>9</td> <td>Naveen</td> <td>Marketing</td> <td>22</td> </tr> <tr> <td>7</td> <td>Parul</td> <td>Finance</td> <td>22</td> </tr> <tr> <td>2</td> <td>Ram</td> <td>Marketing</td> <td>24</td> </tr> <tr> <td>6</td> <td>Sheetal</td> <td>Finance</td> <td>24</td> </tr> <tr> <td>10</td> <td>Tarun</td> <td>Finance</td> <td>23</td> </tr> </table> <hr></60>

Izlaz:

Emp_Id Emp_Name Emp_Dept Ukupna_Plaća Emp_Remarks
1 Akshay Financije 17000 Povećanje
2 Radna memorija Marketing 9000 Smanjenje
3 Shyam Prodajni 10000 Povećanje
4 jatin Kodiranje 12000 Povećanje
5 Manoj Marketing 8000 Smanjenje

Primjer 4: U ovom primjeru koristimo klauzulu ORDER BY s naredbom CASE u SQL-u:

Uzmimo drugu tablicu Employee_Details koja sadrži Emp_ID, Emp_Name, Emp_Dept i Emp_Age.

brisanje npm predmemorije

Podatke o Employee_Details možemo provjeriti pomoću sljedećeg upita u SQL-u:

 Select * From Employee_Details; 

Izlaz:

Emp_Id Emp_Name Emp_Dept Emp_Age
1 Akshay Financije 23
2 Radna memorija Marketing 24
3 Balram Prodajni 25
4 jatin Kodiranje 22
5 Manoj Marketing 23
6 Sheetal Financije 24
7 Kosa Financije 22
8 Yogesh Kodiranje 25
9 Naveen Marketing 22
10 Tarun Financije 23

Sljedeći SQL upit prikazuje sve pojedinosti o zaposlenicima uzlaznim redoslijedom imena zaposlenika:

 SELECT * FROM Employee_Details ORDER BY Emp_Name; 

Izlaz:

Emp_Id Emp_Name Emp_Dept Emp_Age
1 Akshay Financije 23
3 Balram Prodajni 25
5 Manoj Marketing 23
9 Naveen Marketing 22
7 Kosa Financije 22
2 Radna memorija Marketing 24
6 Sheetal Financije 24
10 Tarun Financije 23
4 jatin Kodiranje 22
8 Yogesh Kodiranje 25

Ako želite prikazati one zaposlenike na vrhu koji rade u Odjelu kodiranja, tada za ovu operaciju morate koristiti jednu izjavu WHEN i THEN u izjavi CASE kao što je prikazano u sljedećem upitu:

 SELECT * FROM Employee_Details ORDER BY CASE WHEN Emp_Dept = &apos;Coding&apos; THEN 0 ELSE 1 END, Emp_Name; 

Izlaz:

Emp_Id Emp_Name Emp_Dept Emp_Age
4 jatin Kodiranje 22
8 Yogesh Kodiranje 25
1 Akshay Financije 23
3 Balram Prodajni 25
5 Manoj Marketing 23
9 Naveen Marketing 22
7 Kosa Financije 22
2 Radna memorija Marketing 24
6 Sheetal Financije 24
10 Tarun Financije 23