The DBMS (database management system) is often faster at performing
calculations like summing up or averaging the values in a column of numbers
than, for instance, the application server. However, there are many cases in
which performing the same calculation on all of the data in a column will not
provide the correct result.
This article will show you how to use the CASE expression in SQL queries to
more flexibly perform aggregate calculations and set values based on a
logical statement.
An example of the need to apply the CASE expression in an aggregate function
is an online shopping cart with several item-specific options that, depending
on the options chosen, change the way an item's price is added to the cart
and, therefore, the final total price of the order. I wil... (more)