在SQL Server的where子句中传递多个值

时间:2015-02-24 05:33:57

标签: sql sql-server case multiple-columns where-clause

SELECT
     "dbo"."ClaimDataFormat"."AGE",
     "dbo"."ClaimDataFormat"."RELATION",
     "dbo"."ClaimDataFormat"."NAME",
     "dbo"."ClaimDataFormat"."ENTRY_DATE",
     "dbo"."ClaimDataFormat"."CLAIM_AMT",
     CASE
     WHEN "dbo"."ClaimDataFormat"."Gender" = 'F'
     THEN 'FEMALE'
     ELSE 'MALE'
     END AS GENDER,
     "dbo"."ClaimDataFormat"."STATUS",
     "dbo"."ClaimDataFormat"."APPROVED_AMT"
FROM
     "dbo"."ClaimDataFormat"
WHERE "dbo"."ClaimDataFormat"."AGE" = CASE 
                          WHEN ${TimeGran} = '0-10' 
                               THEN 0 
                          WHEN ${TimeGran} = '0-10'
                               THEN 1
                     WHEN ${TimeGran} = '0-10'
                               THEN 2
                          WHEN ${TimeGran} = '0-10'
                               THEN 3
                          WHEN ${TimeGran} = '0-10'
                               THEN 4
                          WHEN ${TimeGran} = '0-10'
                               THEN 5  
                          WHEN ${TimeGran} = '0-10'
                               THEN 6
                          WHEN ${TimeGran} = '0-10'
                               THEN 7
                          WHEN ${TimeGran} = '0-10'
                               THEN 8
                          WHEN ${TimeGran} = '0-10'
                               THEN 9
                          WHEN ${TimeGran} = '0-10'
                               THEN 10
                          WHEN ${TimeGran} = '11-20'
                               THEN 11 
                          WHEN ${TimeGran} = '11-20'
                               THEN 12
                          WHEN ${TimeGran} = '11-20'
                               THEN 13
                     WHEN ${TimeGran} = '11-20'
                               THEN 14
                          WHEN ${TimeGran} = '11-20'
                               THEN 15
                          WHEN ${TimeGran} = '11-20'
                               THEN 16
                          WHEN ${TimeGran} = '11-20'
                               THEN 17  
                          WHEN ${TimeGran} = '11-20'
                               THEN 18
                          WHEN ${TimeGran} = '11-20'
                               THEN 19
                          WHEN ${TimeGran} = '11-20'
                               THEN 20

                          ELSE 21
                     END 

如果年龄段为0-10,则必须反映0到10之间的所有值

2 个答案:

答案 0 :(得分:1)

对我来说,看起来你需要这样的东西:

WHERE 
  AGE >= convert(int, left(@timeGrain, charindex ('-', @timeGrain) - 1)) and
  AGE <= convert(int, substring(@timeGrain, charindex ('-', @timeGrain) + 1, 9999))

这会将时间分割为 - 并将其用作上限/下限。对于else部分,你可能需要某种OR-clact,但是不能写它,因为在那种情况下我不知道你的可能值是什么。

答案 1 :(得分:1)

试试这个

SELECT dbo.ClaimDataFormat.AGE,
       dbo.ClaimDataFormat.RELATION,
       dbo.ClaimDataFormat.NAME,
       dbo.ClaimDataFormat.ENTRY_DATE,
       dbo.ClaimDataFormat.CLAIM_AMT,
       CASE
         WHEN dbo.ClaimDataFormat.Gender = 'F' THEN 'FEMALE'
         ELSE 'MALE'
       END AS GENDER,
       dbo.ClaimDataFormat.STATUS,
       dbo.ClaimDataFormat.APPROVED_AMT
FROM   dbo.ClaimDataFormat
WHERE  ( dbo.ClaimDataFormat.AGE BETWEEN 0 AND 10
         AND TimeGran = '0-10' )
        OR ( dbo.ClaimDataFormat.AGE BETWEEN 11 AND 20
             AND TimeGran = '11-20' )
        OR ( dbo.ClaimDataFormat.AGE = 21
             AND TimeGran NOT IN ( '0-10', '11-20' ) )