How to have conditional columns in a select statement with MS SQL Server

Applies to:

  • Microsoft SQL Server 2000
  • Microsoft SQL Server 2005
  • Microsoft SQL Server 2008

How to do a IF-THEN-ELSE in the SQL select statement

  1. SELECT empno, SUM(emptime) AS "Hours Worked", CASE
  2.     WHEN SUM(emptime) < 4 THEN 'Beach bum'
  3.     WHEN SUM(emptime) = 4 THEN 'OK'
  4.     WHEN SUM(emptime) > 4 THEN 'Hard worker'
  5.     END AS "Evaluation"
  6. FROM emp_work
  7. GROUP BY empno
  8. ORDER BY 3;

Data used

  1. SELECT empno, emptime
  2. FROM emp_work ;
empno  emptime
------ ---------------------------------------
000010 0.50
000010 1.00
000010 0.50
000020 1.00
000030 0.50
000030 0.50
000050 0.25
000050 0.75
000070 1.00
000090 1.00
000100 1.00
000110 1.00
000130 1.00
000130 0.50
000140 0.50
000140 1.00
000140 0.50
000140 0.50
000140 0.50
000150 1.00
000150 1.00
000160 1.00
000170 1.00
000170 1.00
000170 1.00
000180 1.00
000190 1.00
000190 1.00
000200 1.00
000200 1.00
000210 0.50
000210 0.50
000220 1.00
000230 1.00
000230 0.50
000230 0.50
000230 0.50
000230 1.00
000240 1.00
000240 1.00
000250 1.00
000250 0.50
000250 0.50
000250 1.00
000250 0.50
000250 1.00
000250 0.25
000250 0.25
000250 0.50
000250 0.50
000260 0.50
000260 1.00
000260 1.00
000260 0.50
000260 0.50
000260 1.00
000260 0.50
000270 0.50
000270 1.00
000270 0.25
000270 0.75
000270 1.00
000270 1.00
000270 0.50
000280 1.00
000290 1.00
000300 1.00
000310 1.00
000320 0.75
000320 0.25
000330 0.25
000330 0.75
000340 0.50
000340 0.50
000020 1.00

(75 row(s) affected)

Case Statement

The case statement:

  • Only applies to one column.
  • You can have more than one case statement per select.
  • You can have a case statement based on another previous case statement.

Evaluate the employee based on the billable hours:

  1. SELECT empno, SUM(emptime) AS "Hours Worked",
  2.     CASE
  3.         WHEN SUM(emptime) < 4 THEN 'Beach bum'
  4.         WHEN SUM(emptime) = 4 THEN 'OK'
  5.         WHEN SUM(emptime) > 4 THEN 'Hard worker'
  6.     END AS 'Evaluation'
  7. FROM emp_work
  8. GROUP BY empno;
empno  Hours Worked                            Evaluation
------ --------------------------------------- -----------
000010 2.00                                    Beach bum
000020 2.00                                    Beach bum
000030 1.00                                    Beach bum
000050 1.00                                    Beach bum
000070 1.00                                    Beach bum
000090 1.00                                    Beach bum
000100 1.00                                    Beach bum
000110 1.00                                    Beach bum
000130 1.50                                    Beach bum
000140 3.00                                    Beach bum
000150 2.00                                    Beach bum
000160 1.00                                    Beach bum
000170 3.00                                    Beach bum
000180 1.00                                    Beach bum
000190 2.00                                    Beach bum
000200 2.00                                    Beach bum
000210 1.00                                    Beach bum
000220 1.00                                    Beach bum
000230 3.50                                    Beach bum
000240 2.00                                    Beach bum
000250 6.00                                    Hard worker
000260 5.00                                    Hard worker
000270 5.00                                    Hard worker
000280 1.00                                    Beach bum
000290 1.00                                    Beach bum
000300 1.00                                    Beach bum
000310 1.00                                    Beach bum
000320 1.00                                    Beach bum
000330 1.00                                    Beach bum
000340 1.00                                    Beach bum

(30 row(s) affected)

Sorting on case

There are 2 ways of sorting on the case statement:

  1. The column number
  2. Explicit column with an inline view

Sorting by column number

  1. SELECT empno, SUM(emptime) AS "Hours Worked", CASE
  2.     WHEN SUM(emptime) < 4 THEN 'Beach bum'
  3.     WHEN SUM(emptime) = 4 THEN 'OK'
  4.     WHEN SUM(emptime) > 4 THEN 'Hard worker'
  5.     END AS "Evaluation"
  6. FROM emp_work
  7. GROUP BY empno
  8. ORDER BY 3;
empno  Hours Worked                            Evaluation
------ --------------------------------------- -----------
000010 2.00                                    Beach bum
000020 2.00                                    Beach bum
000030 1.00                                    Beach bum
000050 1.00                                    Beach bum
000070 1.00                                    Beach bum
000090 1.00                                    Beach bum
000100 1.00                                    Beach bum
000110 1.00                                    Beach bum
000130 1.50                                    Beach bum
000140 3.00                                    Beach bum
000150 2.00                                    Beach bum
000160 1.00                                    Beach bum
000170 3.00                                    Beach bum
000180 1.00                                    Beach bum
000190 2.00                                    Beach bum
000200 2.00                                    Beach bum
000210 1.00                                    Beach bum
000220 1.00                                    Beach bum
000230 3.50                                    Beach bum
000240 2.00                                    Beach bum
000280 1.00                                    Beach bum
000290 1.00                                    Beach bum
000300 1.00                                    Beach bum
000310 1.00                                    Beach bum
000320 1.00                                    Beach bum
000330 1.00                                    Beach bum
000340 1.00                                    Beach bum
000250 6.00                                    Hard worker
000260 5.00                                    Hard worker
000270 5.00                                    Hard worker

(30 row(s) affected)

Sorting by the column number is:

  1. Simpler to write.
  2. More difficult to maintain later in the life of the select statement, you will need to ensure that the case statement always stays the third column.
  3. Could be faster than an inline view. But MS SQL Server will optimize the inline view and rewrite it with sorting by column number.

Sorting by column name

To sort by the column name, you will need to use an inline view see: alias in where clause for detailed explanations of inline views.

  1. SELECT * FROM (
  2. SELECT empno, SUM(emptime) AS "Hours Worked", CASE
  3.     WHEN SUM(emptime) < 4 THEN 'Beach bum'
  4.     WHEN SUM(emptime) = 4 THEN 'OK'
  5.     WHEN SUM(emptime) > 4 THEN 'Hard worker'
  6.     END AS "Evaluation"
  7. FROM emp_work
  8. GROUP BY empno) AS ABC
  9. ORDER BY "Hours Worked";
empno  Hours Worked                            Evaluation
------ --------------------------------------- -----------
000030 1.00                                    Beach bum
000050 1.00                                    Beach bum
000070 1.00                                    Beach bum
000090 1.00                                    Beach bum
000100 1.00                                    Beach bum
000110 1.00                                    Beach bum
000160 1.00                                    Beach bum
000180 1.00                                    Beach bum
000210 1.00                                    Beach bum
000220 1.00                                    Beach bum
000280 1.00                                    Beach bum
000290 1.00                                    Beach bum
000300 1.00                                    Beach bum
000310 1.00                                    Beach bum
000320 1.00                                    Beach bum
000330 1.00                                    Beach bum
000340 1.00                                    Beach bum
000130 1.50                                    Beach bum
000190 2.00                                    Beach bum
000200 2.00                                    Beach bum
000010 2.00                                    Beach bum
000020 2.00                                    Beach bum
000240 2.00                                    Beach bum
000150 2.00                                    Beach bum
000170 3.00                                    Beach bum
000140 3.00                                    Beach bum
000230 3.50                                    Beach bum
000260 5.00                                    Hard worker
000270 5.00                                    Hard worker
000250 6.00                                    Hard worker

(30 row(s) affected)

Sorting by the column name is:

  1. More complicated to understand.
  2. Less prone for problem later in the life of the select statement.
  3. Usually as fast as sorting by column number. But MS SQL Server will optimize the inline view and rewrite it with sorting by column number.