logo

SQL CASE

De SAK er en setning som opererer logiske spørringer av typen if-then-else. Denne setningen returnerer verdien når den angitte betingelsen evalueres til True. Når ingen betingelse evalueres til True, returnerer den verdien til ELSE-delen.

Når det ikke er noen ELSE-del og ingen betingelse evalueres til True, returnerer den en NULL-verdi.

I Structured Query Language brukes CASE-setningen i SELECT-, INSERT- og DELETE-setninger med følgende tre klausuler:

  1. HVOR klausul
  2. BESTILL ETTER Klausul
  3. GROUP BY klausul

Denne setningen i SQL følges alltid av minst ett par WHEN- og THEN-setninger og avsluttes alltid med nøkkelordet END.

CASE-setningen er av to typer i relasjonsdatabaser:

hacking-behandling
  1. Enkel CASE-uttalelse
  2. Søkte i CASE-uttalelsen

Syntaks for CASE-setning i SQL

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

Her evaluerer CASE-setningen hver tilstand en etter en.

Hvis uttrykket samsvarer med betingelsen til den første WHEN-setningen, hopper det over alle de ytterligere WHEN- og THEN-betingelsene og returnerer setningen_1 i resultatet.

Hvis uttrykket ikke samsvarer med den første WHEN-betingelsen, sammenlignes det med den andre WHEN-betingelsen. Denne matchingsprosessen vil fortsette til uttrykket matches med en hvilken som helst WHEN-betingelse.

Hvis ingen betingelse samsvarer med uttrykket, går kontrollen automatisk til ELSE-delen og returnerer resultatet. I CASE-syntaksen er ELSE-delen valgfri.

sammenlignbar java

I syntaks er CASE og END de viktigste nøkkelordene som viser begynnelsen og avslutningen av CASE-setningen.

Eksempler på CASE-setning i SQL

La oss ta Student_Details-tabellen, som inneholder roll_no, navn, karakterer, emne og studentby.

sove i js
Roll_No Stu_Name Stu_Subject Stu_Marks Stu_City
2001 Akshay Vitenskap 92 Noida
2002 RAM Matte 49 Jaipur
2004 Shyam Engelsk 52 Gurgaon
2005 yatin Nei Fire fem Lucknow
2006 Manoj Datamaskin 70 Ghaziabad
2007 Sheetal Matte 82 Noida
2008 Håret Vitenskap 62 Gurgaon
2009 Yogesh Engelsk 42 Lucknow
2010 RAM Datamaskin 88 Delhi
2011 Shyam Nei 35 Kanpur

Eksempel 1: Følgende SQL-setning bruker enkelt WHEN- og THEN-betingelse til CASE-setningen:

 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; 

Forklaring av søket ovenfor:

Her kontrollerer CASE-uttalelsen at hvis Stu_Marks er større enn og lik 50, returnerer den Student_Bestått ellers flytter til ELLERS del og returnerer Student_Failed i Student_Result kolonne.

Produksjon:

Roll_No Stu_Name Stu_Subject Stu_Marks Student_Result
2001 Akshay Vitenskap 92 Student_Bestått
2002 RAM Matte 49 Student_Failed
2004 Shyam Engelsk 52 Student_Bestått
2005 yatin Nei Fire fem Student_Failed
2006 Manoj Datamaskin 70 Student_Bestått
2007 Sheetal Matte 82 Student_Bestått
2008 Håret Vitenskap 62 Student_Bestått
2009 Yogesh Engelsk 42 Student_Failed
2010 RAM Datamaskin 88 Student_Bestått
2011 Shyam Nei 35 Student_Failed

Eksempel 2: Følgende SQL-setning legger til mer enn én WHEN- og THEN-betingelse til CASE-setningen:

 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>

Produksjon:

Emp_Id Emp_Name Emp_Dept Total_Lønn Emp_Remarks
1 Akshay Finansiere 17 000 Øke
2 RAM Markedsføring 9000 Redusere
3 Shyam Salg 10 000 Øke
4 yatin Koding 12000 Øke
5 Manoj Markedsføring 8000 Redusere

Eksempel 4: I dette eksemplet bruker vi ORDER BY-leddet med en CASE-setning i SQL:

sql velg fra flere tabeller

La oss ta en annen Employee_Details-tabell som inneholder Emp_ID, Emp_Name, Emp_Dept og Emp_Age.

Vi kan sjekke dataene til Employee_Details ved å bruke følgende spørring i SQL:

 Select * From Employee_Details; 

Produksjon:

Emp_Id Emp_Name Emp_Dept Emp_Age
1 Akshay Finansiere 23
2 RAM Markedsføring 24
3 Balram Salg 25
4 yatin Koding 22
5 Manoj Markedsføring 23
6 Sheetal Finansiere 24
7 Håret Finansiere 22
8 Yogesh Koding 25
9 Naveen Markedsføring 22
10 Tarun Finansiere 23

Følgende SQL-spørring viser alle detaljene til ansatte i stigende rekkefølge av ansattes navn:

 SELECT * FROM Employee_Details ORDER BY Emp_Name; 

Produksjon:

Emp_Id Emp_Name Emp_Dept Emp_Age
1 Akshay Finansiere 23
3 Balram Salg 25
5 Manoj Markedsføring 23
9 Naveen Markedsføring 22
7 Håret Finansiere 22
2 RAM Markedsføring 24
6 Sheetal Finansiere 24
10 Tarun Finansiere 23
4 yatin Koding 22
8 Yogesh Koding 25

Hvis du vil vise de ansatte øverst som jobber i kodingsavdelingen, må du for denne operasjonen bruke enkelt WHEN- og THEN-setning i CASE-setningen som vist i følgende spørring:

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

Produksjon:

Emp_Id Emp_Name Emp_Dept Emp_Age
4 yatin Koding 22
8 Yogesh Koding 25
1 Akshay Finansiere 23
3 Balram Salg 25
5 Manoj Markedsføring 23
9 Naveen Markedsføring 22
7 Håret Finansiere 22
2 RAM Markedsføring 24
6 Sheetal Finansiere 24
10 Tarun Finansiere 23