The Power of SQL CASE Statements

Source: Internet
Author: User
Tags ranges

Introduction
As developers creating data-driven Web applications, we 've all written more than our fair share of SQL statements. if I had a nickel for every SELECT statement or UPDATE statement I 've written in my six years as a Web developer, I 'd be a rich man. despite the familiarity with SQL that extends Web developers have, one of the lesser known SQL tools is the CASE statement. the CASE statement is a wily little fellow that can be added to both the SELECT portion of a SELECT statement as well as the order by portion. with CASE you can easily group data into varous ranges, you can beautify the results of a SQL query, and can allow for dynamic sorting of the query's results. CASE statements can also be used to give subtitles to ROLLUP and CUBE queries, and can be used in computed columns to boot.

In this article we'll take a look at the powerful CASE statement, shining light on its utility and usefulness in everyday data-driven Web sites. Read on to learn more!
 

 

CASE Basics
All programming phases ages contain conditional statements to allow program flow to be altered based on the result of some conditional statement, the simplest form of a conditional statement typically being an If statement. one downside of If statements is that if there are a number of different actions that must be taken based on some variable value, the number of If statements can explode, leading to bloated, unreadable code. for example, imagine that you wanted to do some different action based on the hour of the day. if a different action was passed Med for each hour of the day, you wocould end up having 24 If/ElseIf statements like so:

If Hour (Now () = 0 Then
'Do whatever needs to be done at midnight
ElseIf Hour (Now () = 1 Then
'Do whatever needs to be done at am
ElseIf Hour (Now () = 2 Then
...
...
End If

 

The multiple If statements detract from the readability of the code. also, the maintainability of the code suffers. imagine that you needed to change the code so that task x was run at both am and am. this wowould require picking your way through the If statements and adding additional conditional statements where needed.

Extends programming phases ages provide case statements: Visual Basic offers a Select Case, C #/C ++/Java the switch statement. these case statements allow for the myriad of If statements to be compressed into a more readable block of code. an example Select Case statement can be seen below:

Select Case Hour (Now ())
Case 0
'Do whatever needs to be done at midnight
Case 1
'Do whatever needs to be done at am
Case 2
...
...
End Select

 

Case Statements are More than T
In addition to improved readability, case statements also provided an opportunity for optimization. For a more detailed discussion on case statement optimizations check out Switch Statement Performance Consideration for a more thorough discussion.

Not to be outdone, T-SQL contains a CASE statement as well. The T-SQL CASE statement has two flavors:

A simple case statement, whose syntax and semantics closely mirror those case statements found in other programming languages. The generic form for the simple case statement is:
CASE expression
WHEN value1 THEN result1
WHEN value2 THEN result2
...
WHEN valueN THEN resultN
[
ELSE elseResult
]
END

 

 

A "searched CASE form" that instead of specifying a single expression, contains a series of Boolean expressions in each of the WHEN clses:
CASE
WHEN booleanExpression1 THEN result1
WHEN booleanExpression2 THEN result2
...
WHEN booleanExpressionN THEN resultN
[
ELSE elseResult
]
END

 

 

Now that we're 've looked at the generic syntax for T-SQL CASE statements, let's turn our attention to examining how CASE can be used in real-world applications.
Prettying Up Your Output
The simplest use of CASE is for tweaking the output. for example, imagine you have a table with employee information that contains a char (1) field called Gender, with a value of M for males and F for females. you might want to have the output be Male and Female instead of M and F. this can easily be accomplished with CASE as the following example extends strates:

SELECT
FirstName, LastName,
Salary, DOB,
CASE Gender
WHEN 'M' THEN 'male'
WHEN 'F' THEN 'female'
END
FROM Employees

 

For more examples of enhancing the output with CASE be sure to read The Case for CASE.

Formatting Summation Columns
The SQL syntax offers a number of aggregate functions, such as AVG (), SUM (), MAX (), and so forth, allowing for averages, sums, maxes, minimums, and so on to be grouped by FIG field values. returning to the employees example, if each employee worked for a department specified by a Department field, and if each employee had a Salary field, you cocould display the average salary and total salary per department using a query like:

SELECT
Department, AVG (Salary) as AvgSalary
FROM Employees
Group by Department

 

The output of this query might look like:

Department AvgSalary
Sales 78,500.00
Marketing 81,250.00
IT 55,000.50
Executive 91,900.75
You might also want to know the average salary for the entire company, please SS all configurations. SQL provides the ROLLUP keyword to summarize aggregated data. for example, adjusting the query by adding with rollup, as in:
SELECT
Department, AVG (Salary) as AvgSalary
FROM Employees
Group by Department
WITH ROLLUP

 

Might generate the following output:

Department AvgSalary
Sales 78,500.00
Marketing 81,250.00
IT 55,000.50
Executive 91,900.75
NULL 76,662.81
The problem here is that the last row that provides the summary has a NULL value for the Department field. ideally, this wowould have a value like Company Average. well, SQL provides a GROUPING (columnName) function that returns a 1 when the specified column name is for a summarized row. using this inside a CASE statement, you cocould achieve the desired output like so:
SELECT
CASE
When grouping (Department) = 1 THEN 'Company average'
ELSE Department
End as Department,
AVG (Salary) as AvgSalary
FROM Employees
Group by Department
WITH ROLLUP

 

For more on this technique, including how to display summarized data within a DataGrid, be sure to read Dave Long's article Summarizing Data with ROLLUP.

Used to Dynamically Sort Query Results
Ideally when creating data-driven Web applications, all SQL statements are encapsulated within stored procedures. when using an ASP. NET DataGrid, you can easily bind the results of a stored procedure to the DataGrid. however, if you want to implement sorting within the DataGrid, you need some means to dynamically order the stored procedure's results. there are a couple of techniques to accomplish this. A common one is to read the stored procedure results into a DataTable, and then use the DataTable's Sort property to sort the results by the specified column. if you want to stay away from DataTables and use just the more efficient DataReaders, you might pine for some means to pass in the column to sort by into the stored procedure. unfortunately, the following stored procedure is not valid.

Create procedure getEmployees (@ OrderByClause varchar (100)
SELECT EmployeeID, FirstName, LastName, SSN, Salary
FROM Employees
Order by @ OrderByClause

 

What you can do, however, is use a CASE statement in the order by clause to specify what column to sort the results by based upon the passed in @ OrderByClause parameter. The syntax looks like:

Create procedure getEmployees (@ ColumnName varchar (100)
SELECT EmployeeID, FirstName, LastName, SSN, Salary
FROM Employees
ORDER
Case when @ ColumnName = 'lastname' THEN LastName
WHEN @ ColumnName = 'Salary' then convert (varchar (50), Salary)
WHEN @ ColumnName = 'ssn' THEN SSN
END

 

Note that the input parameter is a varchar (100) and can specify one of the column names to sort. the CASE statement in the order by clause determines what column to sort by based upon the value passed in.

There are a couple caveats when using this approach for dynamic ordering. first, dynamic ordering doesn' t work for mixed data types. that is, if you have fields of differing data types that you want to order by-such as varchars and decimals, as is the case with LastName and Salary above-then in the CASE statement you need to use CONVERT to convert the mismatched data type to the same data type as the others. note how in the second WHEN line in the example above, Salary is converted to a varchar (50 ). you'll have to do this with datetime, int and other non-character data types as well.

For a more in-depth look at this technique for dynamically sorting the results of stored procedures, be sure to read: Dynamic order by Statements in Stored Procedures.

Grouping Results Into Ranges
The CASE statement can also be used to group data into ranges. I recently helped a client create a system for sending invoices, and he needed a report that showed the total amount of money a customer was invoiced in the past 30 days, in the past 30-60 days, in the past 60-90 days, and 90 + days past. this was easily accomplished with a series of CASE statements within SUM () functions like so:

SELECT
SUM (CASE
WHEN DateDiff (d, InvoiceDate, getdate () BETWEEN 0 AND 29 THEN InvoiceAmount
END) as Current,
SUM (CASE
WHEN DateDiff (d, InvoiceDate, getdate () BETWEEN 30 AND 59 THEN InvoiceAmount
END) as ThirtyToSixty,
SUM (CASE
WHEN DateDiff (d, InvoiceDate, getdate () BETWEEN 60 AND 89 THEN InvoiceAmount
END) as SixtyToNinty,
SUM (CASE
WHEN DateDiff (d, InvoiceDate, getdate ()> = 90 THEN InvoiceAmount
END) as NintyPlus
FROM Invoices

 

Using CASE in Computed Columns
With SQL Server you can create computed columns that are comprised of data from other columns in the table. for example, in a table storing medical information for patients, there might be Weight and Height fields, with a computed column for the BMI. (BMI, or Body Mass Index, is a ratio of a person's height and weight, and is a figure that is commonly used to ascertain if a patient is underweight, normal weight, overweight, or obese .)

When creating a computed column you can use a CASE statement in the computation. for example, imagine that you had a table with information about classes offered by a local college. this table might have two int fields that recorded the number of students and professors participate in the class (call these fields Students and Professors ). now, you might want to add a computed field that returned the ratio of students to professors, providing the formula Students/Professors. the problem here is that if there is a class with no professors, the computed column will barf since division by 0 is not allowed. to surmount this problem, you cocould use a computed column formula with a CASE statement that returned NULL (or some predefined value, such as 0) if Professors already ED 0. that is, you 'd have your formula become:

CASE
WHEN Faculty = 0 THEN NULL
ELSE Students/Faculty
END

 

For more information on this technique, check out this blog entry: Computed Columns and Divide By Zero Errors.

Conclusion
In this article we looked at SQL's CASE statement, a useful little bugger that, sadly, doesn't get the press he deserves. CASE statements can be used to format the output of your SQL queries, to group data into varous ranges, to allow for dynamic ordering of SQL results in a stored procedure, used as conditionals in computed columns, and used to format summation columns.

Happy Programming!

By Scott Mitchell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.