What is the equivalent of Select Case in Access SQL?

后端 未结 3 1014
温柔的废话
温柔的废话 2020-11-30 04:28

I have a query which includes fields named openingbalance and commissions. I would like to compute values for commissions based on openingbala

相关标签:
3条回答
  • 2020-11-30 04:57

    You could do below:

    select
    iif ( OpeningBalance>=0 And OpeningBalance<=500 , 20, 
    
                      iif ( OpeningBalance>=5001 And OpeningBalance<=10000 , 30, 
    
                           iif ( OpeningBalance>=10001 And OpeningBalance<=20000 , 40, 
    
    50 ) ) ) as commission
    from table
    
    0 讨论(0)
  • 2020-11-30 05:20

    You can use IIF for a similar result.

    Note that you can nest the IIF statements to handle multiple cases. There is an example here: http://forums.devshed.com/database-management-46/query-ms-access-iif-statement-multiple-conditions-358130.html

    SELECT IIf([Combinaison] = "Mike", 12, IIf([Combinaison] = "Steve", 13)) As Answer 
    FROM MyTable;
    
    0 讨论(0)
  • 2020-11-30 05:22

    Consider the Switch Function as an alternative to multiple IIf() expressions. It will return the value from the first expression/value pair where the expression evaluates as True, and ignore any remaining pairs. The concept is similar to the SELECT ... CASE approach you referenced but which is not available in Access SQL.

    If you want to display a calculated field as commission:

    SELECT
        Switch(
            OpeningBalance < 5001, 20,
            OpeningBalance < 10001, 30,
            OpeningBalance < 20001, 40,
            OpeningBalance >= 20001, 50
            ) AS commission
    FROM YourTable;
    

    If you want to store that calculated value to a field named commission:

    UPDATE YourTable
    SET commission =
        Switch(
            OpeningBalance < 5001, 20,
            OpeningBalance < 10001, 30,
            OpeningBalance < 20001, 40,
            OpeningBalance >= 20001, 50
            );
    

    Either way, see whether you find Switch() easier to understand and manage. Multiple IIf()s can become mind-boggling as the number of conditions grows.

    0 讨论(0)
提交回复
热议问题