Listado de la etiqueta: Case


We will discuss about Pyspark – a significant data processing technology that can handle data on a petabyte-scale, PySpark When Otherwise, and SQL Case in PySpark When.

What is PySpark?

Spark is a general-purpose, in-memory, distributed processing engine that allows you to handle the data across several machines efficiently. You can develop Spark applications to process the data and run them on the Spark platform using PySpark. The AWS offers managed EMR and the Spark platform. You may use PySpark to process data and establish an EMR cluster on AWS. PySpark can read the data from various file formats including CSV, parquet, json, and databases. Because Spark is primarily implemented in Scala, creating Spark apps in Scala or Java allows you to access more of its features than writing Spark programs in Python or R. PySpark, for example, does not currently support Dataset. If you’re doing a data science, PySpark is a better option than Scala because there are many popular data science libraries written in Python such as NumPy, TensorFlow, and Scikit-learn.

PySpark “When” and “Otherwise”

“Otherwise” and “when” in PySpark, and SQL Case “when” working with DataFrame PySpark, like SQL and other programming languages, have a mechanism of checking multiple conditions in order and returning a value when the first condition is met using SQL like case and when(). Otherwise() expressions are similar to “Switch” and “if-then-else” statements in their functionality.

PySpark When Otherwise – when() is an SQL function that returns a Column type, and otherwise() is a Column function that produces None/NULL, if otherwise() is not used.

SQL Case in PySpark When – This is similar to an SQL expression, and it is used as follows: IF condition 1 is true, then the result is true, and vice versa.

Example 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

import org.apache.spark.sql.functions.when

 

val df = Seq(

    («A B»,«2019-01-19»),
    («A A», «2019-01-10»),
    («B F», «2019-01-15»),
    («B E», «2019-01-30»),
    («C B», «2019-01-22»),
    («D O», «2019-01-30»),
    («E U», «2019-01-22»)

 

df.withColumn(«ends_with_B»,when($«word».endsWith(«B»),true).otherwise(false))

Example 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

import org.apache.spark.sql.functions.{element_at,split,when}

 

val df = Seq(

    («BA»,«human»),
    («AB», «human»),
    («E_bot», «bot»),
    («D_bot», «bot»),
    («TT», «human»),
    («A_bot», «bot»),
    («C_bot», «bot»)

).toDF(«user», «type»)

df.withColumn(«isBot», when($«user».endsWith(«bot»), element_at(split($«user»,«_»),1)))

Conclusion

We discussed about PySpark, PySpark When, PySpark Otherwise, and SQL Case in PySpark When which are used to check multiple conditions and return the first element that follows the condition, along with some examples.



Source link


SQLite has a case statement which allows us to include conditional logic in our SQL statements. SQLite case expressions execute a list of conditions and return an expression based on the results. SQLite case statements behave identically to IF – THEN – ELSE statements like the other scripting languages. If the phrase is valid, we can use the SQLite case statement inside any of the clauses or SQL statements. In genérico, the SQLite Case statement starts with an optional expression and one or more WHEN… THEN clauses, an optional ELSE clause, and a necessary END keyword.

Syntax of the CASE statement in SQLite:

In SQLite, the case statement usually has one of two forms.

SQLite simple Case statement:

CASE test_statement
WHEN [condition1] THEN [statement1]
WHEN [condition2] THEN [statement2]
……
……
……
WHEN [condition(n)] THEN [statement(n)]
ELSE [statement]
END

We use the syntax above to establish many conditions to reach the desired outcome. When and then clauses are used in the above syntax. They function in a logical order. To return the final output, this method compares each statement to the list of statements. This is a fairly easy approach for executing the case statement according to the user’s requirements. Each condition and statement are interdependent, meaning, when the first condition is true, the statement is only executed after that. This way, all conditions, and statements are executed. If a condition is false, control is transferred to the else part as seen in the above syntax.

To choose the outcome, the query case statement evaluates a list of statements. It’s worth noting that the simple case expression just looks for equity. Whereas, the looked-through case statement can use any form of inspection.

SQLite Search case statement:

CASE test_statement
WHEN [BOOLEAN statement1] THEN [statement1]
WHEN[BOOLEAN statement2] THEN[statement2]
 ELSE [ statement] END

If the Boolean statement in the specified grouping is valid, the Search case statement evaluates it and returns the corresponding result. When no valid statement is found, the query case statement returns the statement in the ELSE condition. If the ELSE clause is neglected, the looked-through case statement returns NULL. When the stated criteria are met, the search case statement terminates the assessment and execution.

Creating table for CASE statement in SQLite:

First, we have created a table and gave the name “Student” with the CREATE query. The table is set with the different columns such as ID with the data type integer and create ID as a primary key, NAME, and EMAIL is set with the data type TEXT. The last column CGPA is assigned a TEXT data type. The table Student and its attributes are shown in the SQLite shell as follows:

CREATE TABLE Student(
   …>       ID INT PRIMARY KEY    ,
   …>       NAME           TEXT   ,
   …>       EMAIL          TEXT   ,
   …>       CGPA           FLOAT
   …>     );

Now, we have to insert the values against each column of the table Student. With the SQLite INSERT query, we have inserted five rows in each of the columns specified in the table Student. The screenshot below is showing the way of inserting records into the table columns.

sqlite> INSERT INTO Student VALUES (2, ‘Ibrahim’,[email protected], 3.20 );
sqlite> INSERT INTO Student VALUES (3, ‘Maha’,[email protected], 3.9);
sqlite> INSERT INTO Student VALUES (4, ‘Jennifer’, [email protected], 2.5);
sqlite> INSERT INTO Student VALUES (5, ‘Rehan’, [email protected], 3.10 );

Let’s view whether the data is recorded in the table Student. By using the SELECT statement, we have retrieved the entire data in the table Student in a table format. The aforementioned statement is represented in the screenshot below:

sqlite> SELECT * FROM Student;

Example 1: Program of using SQLite simple CASE statement:

We have used a simple CASE statement with the SELECT statement to show the workings of the CASE statement in SQLite.

Here, we have used a SELECT statement and selected a column ID, NAME, and CGPA from the table Student. After that, we used a CASE statement that compares the CGPA of the Student. It matches the CGPA with the condition whether the CGPA is greater than or equal to 4.00 or not. If so, then it gives that CGPA an A+. But if the condition is false, then CASE will switch to the next statement and check whether the CGPA is greater than 3.5. If the condition is true, then it assign this CGPA a grade “A”.

Like this, the control is passed to each case statement until the CGPA does not meet the given condition. If all the cases are false, then the else clause will be executed and will print a statement FAIL.

sqlite> SELECT ID, NAME, CGPA,
   …> CASE
   …> WHEN CGPA >= 4.00 THEN «A+»
   …> WHEN CGPA >= 3.5 THEN «A»
   …> WHEN CGPA >= 3.0 THEN «B»
   …> WHEN CGPA >= 2.5 THEN «C»
   …> ELSE «FAIL»
   …> END AS «GRADE»
   …> FROM Student;

Now, we will perform the query and look at the outcome. It should look like this: The GRADE column is included in the table Student along with the values from the CASE statement.

Example 2: Program of using SQLite search CASE statement:

To determine the outcome, the searched CASE statement analyses a set of expressions. The simple CASE expression simply compares for equality. Whereas, the searching CASE expression can compare in any way.

A Boolean case statement is used in the example given. For this, we have a SELECT query through which we have selected three attributes ID, NAME, and CGPA from the table Student. Only these attributes and their corresponding values will appear as the output. Then, we have a CASE keyword for the SQLite CASE statement. The condition is applied with the WHEN keyword. First, case statement checks if the CGPA is greater than 3.5 and then assigns the grade A. If the condition is not satisfied here, then we will move to our second case statement which checks the condition whether is satisfied here or not. If both our cases are false, then the else part will execute and print the grade C.

sqlite> SELECT ID, NAME, CGPA,
   …> CASE
   …> WHEN CGPA > 3.5 THEN «A»
   …> WHEN CGPA >  3.0 AND CGPA < 2.5 THEN «B»
   …> ELSE
   …> «C»
   …> END «GRADE»
   …> FROM Student;

When executing the above CASE query, the results are obtained like this:

Conclusion:

We studied the basic syntax of case statements in this article. We also saw a variety of case statement instances. The rules for case statements were also taught. We learned using the SQLite case statement in this post and when to do so.



Source link