AlphaPress Publishing
P.O. Box 5942 Albany, NY 12205
Tel: 518-250-3890


Section Menu 

Are you a University or College bookstore and want to order multiple books?  Email us at or call us at 518-250-3890.  We will be happy to talk to you.  

Are you a professor and you need a sample copy?  We will be happy to send you a pdf or printed copy!  Email us at

Are you outside the United States?  We can ship multiple books worldwide in most countries.




Nested iif() functions vs the switch() function

To contact the author click here

This article is from the book "Access 2007 Pure SQL

To download the sample database click here

Working with multiple conditions and criteria

Determine discount amounts based on customer sales volumes


Our sales manager wants to provide order discounts to customers based on their historical sales volume with the company.  Thus, if a customer has a certain recorded sales volume, the next time she orders, she will get a predetermined discount rate regardless of her new order amount. 

If the total orders amount for a customer is less than $200, that customer will receive no discounts.  If it is between $200 and $300, the customer will get a 5% discount.  If it is between $300 and $500, the customer will get a 10% discount.  For anything above that, our manager will offer a generous 25% discount to the customer.  Our job is to create a datasheet that will list customers and show the corresponding discount rate for each customer. 

Solution 1: using nested iif() functions

Our first option is to use a series of nested iif() functions as shown below—four iif() functions, one inside the other to accomplish our goal.  However, the use of nested iif() functions is a convoluted process.  In addition, if we need to change the business logic behind our statement by adding additional categories, the task is not a clear and clean preposition.  The problem originates from the fact that the iif() function takes only two arguments.  For more outcomes or categories, we need to nest, which, in turn, results in complicated statements. 


SELECT lastname, firstname, OrderTotal, IIf([ordertotal]<200,0,IIf([ordertotal]>=200 And [Ordertotal]<300,'5%',IIf([ordertotal]>=300 And [ordertotal]<500,'10%',IIf([ordertotal]>500,'25%')))) AS Discount

FROM Qry_Conditions;




Solution 2: using the switch() function

In this example, the first observation is that the switch() function does not limit the number of expressions we can use, so we need no nesting.  In addition, our logic becomes immediately apparent to both us and anyone else who will need to edit the SQL statement later on.  The cleanliness of the SQL statement is obvious: 


SELECT lastname, firstname, ordertotal,


ordertotal < 200,                                             "0" ,

ordertotal>= 200 and ordertotal < 300,        "5%" ,

ordertotal> 300 and ordertotal <= 500,      "10%" ,

ordertotal> 500 ,                                            "25%"


AS Discount 

FROM qry_conditions