SQL CASE WHEN或IF ELSE IF

时间:2014-09-06 07:04:56

标签: sql

我在案件中遇到多种情况 我想知道我是否可以使用> <而不是定义每个案例

在这种情况下,当信用评级小于3时,单词" NO TRADE"将被插入 然后大于3,小于5将是" POOR",依此类推等等

SELECT  ClientId, 
    FirstName,
    LastName,
    Gender,
    DateOfBirth,
    CreditRating,

        CASE CreditRating

            WHEN 0 THEN 'NO TRADE'
            WHEN 1 THEN 'NO TRADE'
            WHEN 2 THEN 'NO TRADE'
            WHEN 3 THEN 'POOR'
            WHEN 4 THEN 'POOR'
            WHEN 5 THEN 'AVARAGE'
            WHEN 6 THEN 'AVARAGE'
            WHEN 7 THEN 'GOOD'
            ELSE 'PERFECT' 

            END AS RATING

    FROM dbo.client

1 个答案:

答案 0 :(得分:5)

当然有可能。

        CASE 

        WHEN CreditRating <= 2  THEN 'NO TRADE'
        WHEN CreditRating  <= 4 THEN 'POOR'
        WHEN CreditRating  <= 6 THEN 'AVARAGE'
        WHEN CreditRating  = 7 THEN 'GOOD'

        ELSE 'PERFECT' 

        END AS RATING