sql - GoogleSQL - SELECT IF

Sql - GoogleSQL - SELECT IF

In Google BigQuery (which is part of Google Cloud's SQL offerings), you can use the CASE statement to achieve conditional logic similar to IF statements in other SQL dialects. Here's how you can use CASE in Google BigQuery:

Example Scenario

Assume you have a table Employees with columns EmployeeID, FirstName, LastName, and Salary. You want to create a query that selects employees and assigns a category based on their salary.

SQL Query Using CASE

You can use CASE to create conditional logic in your SELECT statement:

SELECT EmployeeID, FirstName, LastName, Salary, CASE WHEN Salary < 50000 THEN 'Low Salary' WHEN Salary >= 50000 AND Salary < 100000 THEN 'Medium Salary' ELSE 'High Salary' END AS SalaryCategory FROM Employees; 

Explanation

  • CASE ... END: Evaluates a series of conditions and returns a value when the first condition is true.
  • Each WHEN clause specifies a condition followed by the result if the condition is true.
  • ELSE: Optional. Specifies a result if none of the preceding conditions are true.
  • SalaryCategory: Alias for the computed column that categorizes employees based on their salary.

Example Usage:

If Employees contains data like this:

EmployeeIDFirstNameLastNameSalary
1JohnDoe48000
2JaneSmith75000
3MichaelJohnson110000

The query will categorize employees based on their salary:

EmployeeIDFirstNameLastNameSalarySalaryCategory
1JohnDoe48000Low Salary
2JaneSmith75000Medium Salary
3MichaelJohnson110000High Salary

Notes:

  • Adjust the conditions (WHEN Salary < 50000, etc.) and result values ('Low Salary', 'Medium Salary', 'High Salary') based on your specific business logic.
  • Use CASE statements within SELECT queries to provide conditional logic for deriving computed values or categories based on other column values.

This approach effectively allows you to implement conditional logic similar to IF statements using CASE in Google BigQuery SQL. Adjust the query as per your specific requirements and business logic scenarios.

Examples

  1. GoogleSQL SELECT with IF condition

    Description: This query uses the IF function to conditionally select values based on a certain condition.

    SELECT column1, IF(column2 > 10, 'High', 'Low') as column2_status FROM your_table; 
  2. GoogleSQL SELECT with IFNULL function

    Description: The IFNULL function returns an alternative value if the column value is NULL.

    SELECT column1, IFNULL(column2, 'No Value') as column2_checked FROM your_table; 
  3. GoogleSQL SELECT with IF and aggregate function

    Description: Using IF within an aggregate function to conditionally sum values.

    SELECT column1, SUM(IF(column2 > 10, column3, 0)) as conditional_sum FROM your_table GROUP BY column1; 
  4. GoogleSQL SELECT with nested IF statements

    Description: Implementing multiple conditions using nested IF statements.

    SELECT column1, IF(column2 > 10, 'High', IF(column2 > 5, 'Medium', 'Low')) as column2_category FROM your_table; 
  5. GoogleSQL SELECT with CASE statement as IF alternative

    Description: Using the CASE statement as an alternative to the IF function for complex conditions.

    SELECT column1, CASE WHEN column2 > 10 THEN 'High' WHEN column2 > 5 THEN 'Medium' ELSE 'Low' END as column2_status FROM your_table; 
  6. GoogleSQL SELECT with IF and CONCAT function

    Description: Using IF to conditionally concatenate strings.

    SELECT column1, IF(column2 > 10, CONCAT('Value: ', column2), 'Value too low') as column2_info FROM your_table; 
  7. GoogleSQL SELECT with IF and date functions

    Description: Using IF to conditionally format dates.

    SELECT column1, IF(column2 > 10, FORMAT_DATE('%Y-%m-%d', column_date), 'Date not applicable') as formatted_date FROM your_table; 
  8. GoogleSQL SELECT with IF and mathematical operations

    Description: Using IF to conditionally apply mathematical operations.

    SELECT column1, IF(column2 > 10, column3 * 2, column3 / 2) as adjusted_column3 FROM your_table; 
  9. GoogleSQL SELECT with IF in WHERE clause

    Description: Using IF to conditionally filter rows in the WHERE clause.

    SELECT * FROM your_table WHERE IF(column2 > 10, column3 > 5, column3 <= 5); 
  10. GoogleSQL SELECT with IF and string manipulation

    Description: Using IF to conditionally manipulate strings.

    SELECT column1, IF(STRPOS(column2, 'test') > 0, 'Contains test', 'Does not contain test') as test_check FROM your_table; 

More Tags

javascript-objects python azure-storage dfsort hibernate-onetomany regexp-substr pagedlist docker-image material-components-android registry

More Programming Questions

More Cat Calculators

More Date and Time Calculators

More Genetics Calculators

More Chemical thermodynamics Calculators