logo

SQL CASE

The OVITEK je stavek, ki upravlja vrsto logičnih poizvedb if-then-else. Ta stavek vrne vrednost, ko je podani pogoj ovrednoten na True. Če noben pogoj ni ovrednoten na True, vrne vrednost dela ELSE.

Če ni dela ELSE in noben pogoj ni ovrednoten na True, vrne vrednost NULL.

V jeziku strukturiranih poizvedb se stavek CASE uporablja v stavkih SELECT, INSERT in DELETE z naslednjimi tremi stavki:

  1. Klavzula WHERE
  2. Klavzula ORDER BY
  3. GROUP BY klavzula

Tej izjavi v SQL vedno sledi vsaj en par stavkov WHEN in THEN in se vedno konča s ključno besedo END.

Stavek CASE je v relacijskih bazah podatkov dveh vrst:

  1. Enostavna izjava CASE
  2. Iskan stavek CASE

Sintaksa stavka CASE v SQL

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

Tukaj stavek CASE ovrednoti vsak pogoj enega za drugim.

Če se izraz ujema s pogojem prvega stavka WHEN, preskoči vse nadaljnje pogoje WHEN in THEN ter v rezultatu vrne stavek_1.

string.format java niz

Če se izraz ne ujema s prvim pogojem WHEN, se primerja z drugim pogojem WHEN. Ta postopek ujemanja se bo nadaljeval, dokler se izraz ne ujema s katerim koli pogojem WHEN.

Če se z izrazom ne ujema noben pogoj, kontrolnik samodejno preide na del ELSE in vrne rezultat. V sintaksi CASE je del ELSE neobvezen.

V sintaksi sta CASE in END najpomembnejši ključni besedi, ki prikazujeta začetek in zaključek stavka CASE.

Primeri stavka CASE v SQL

Vzemimo tabelo Student_Details, ki vsebuje roll_no, ime, ocene, predmet in mesto študentov.

zvitek_št Stu_Name Stu_Subject Stu_Marks Stu_City
2001 Akshay Znanost 92 Noida
2002 Oven matematika 49 Jaipur
2004 Shyam angleščina 52 Gurgaon
2005 yatin ne Štiri Lucknow
2006 Manoj Računalnik 70 Ghaziabad
2007 Sheetal matematika 82 Noida
2008 Lasje Znanost 62 Gurgaon
2009 Yogesh angleščina 42 Lucknow
2010 Oven Računalnik 88 Delhi
2011 Shyam ne 35 Kanpur

Primer 1: Naslednji stavek SQL uporablja en pogoj WHEN in THEN za stavek CASE:

niz v jsonobject
 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; 

Razlaga zgornje poizvedbe:

Tukaj izjava CASE preveri, ali če je Stu_Marks večje od in enako 50, vrne Študent_uspešno sicer se seli v DRUGEGA del in vrne Študent_neuspešno v Študentski_rezultat stolpec.

Izhod:

zvitek_št Stu_Name Stu_Subject Stu_Marks Študentski_rezultat
2001 Akshay Znanost 92 Študent_uspešno
2002 Oven matematika 49 Študent_neuspešno
2004 Shyam angleščina 52 Študent_uspešno
2005 yatin ne Štiri Študent_neuspešno
2006 Manoj Računalnik 70 Študent_uspešno
2007 Sheetal matematika 82 Študent_uspešno
2008 Lasje Znanost 62 Študent_uspešno
2009 Yogesh angleščina 42 Študent_neuspešno
2010 Oven Računalnik 88 Študent_uspešno
2011 Shyam ne 35 Študent_neuspešno

Primer 2: Naslednji stavek SQL stavku CASE doda več kot en pogoj WHEN in THEN:

 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>

Izhod:

Emp_Id Emp_Name Emp_Dept Skupna_plača Emp_Remarks
1 Akshay Finance 17000 Prirastek
2 Oven Trženje 9000 Zmanjšaj
3 Shyam Prodaja 10000 Prirastek
4 yatin Kodiranje 12000 Prirastek
5 Manoj Trženje 8000 Zmanjšaj

Primer 4: V tem primeru uporabljamo klavzulo ORDER BY s stavkom CASE v SQL:

Vzemimo drugo tabelo Employee_Details, ki vsebuje Emp_ID, Emp_Name, Emp_Dept in Emp_Age.

Podatke Employee_Details lahko preverimo z uporabo naslednje poizvedbe v SQL:

 Select * From Employee_Details; 

Izhod:

Emp_Id Emp_Name Emp_Dept Emp_Age
1 Akshay Finance 23
2 Oven Trženje 24
3 Balram Prodaja 25
4 yatin Kodiranje 22
5 Manoj Trženje 23
6 Sheetal Finance 24
7 Lasje Finance 22
8 Yogesh Kodiranje 25
9 Naveen Trženje 22
10 Tarun Finance 23

Naslednja poizvedba SQL prikazuje vse podrobnosti zaposlenih v naraščajočem vrstnem redu imen zaposlenih:

java spremenljivka spremenljivka
 SELECT * FROM Employee_Details ORDER BY Emp_Name; 

Izhod:

Emp_Id Emp_Name Emp_Dept Emp_Age
1 Akshay Finance 23
3 Balram Prodaja 25
5 Manoj Trženje 23
9 Naveen Trženje 22
7 Lasje Finance 22
2 Oven Trženje 24
6 Sheetal Finance 24
10 Tarun Finance 23
4 yatin Kodiranje 22
8 Yogesh Kodiranje 25

Če želite prikazati zaposlene na vrhu, ki delajo v oddelku za kodiranje, potem morate za to operacijo uporabiti en sam stavek WHEN in THEN v stavku CASE, kot je prikazano v naslednji poizvedbi:

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

Izhod:

Emp_Id Emp_Name Emp_Dept Emp_Age
4 yatin Kodiranje 22
8 Yogesh Kodiranje 25
1 Akshay Finance 23
3 Balram Prodaja 25
5 Manoj Trženje 23
9 Naveen Trženje 22
7 Lasje Finance 22
2 Oven Trženje 24
6 Sheetal Finance 24
10 Tarun Finance 23