本人在写一个报表,希望在where语句中使用case when,想得到的结果是,当A字段 = ‘37’时B字段=‘24’或者B字段=‘10’,不知道case when then后面可以等于两个结果吗?,即
where
B字段=
case when A字段 = ‘37’ then '24' or '10'
end

解决方案 »

  1.   

    where charindex(','+ B字段+',', case when A字段 = '37' then ',24,10,' else '' end) > 0 
      

  2.   

    create table tb(a int)
    insert into tb select '37'
    insert into tb select '50'
    go
    select b=case when a=37 then (
    select top 1 a from (
    select '24' as a
    union 
    select '10'
    )t order by newid()) else a end
    from tb
      

  3.   


    declare @table table(a int)
    insert into @table
    select 1 union all
    select 37 union all
    select 37 union all
    select 37 union all
    select 67 declare @b table (b int)
    insert into @b
    select 24 union all
    select 10select a,case a when 37 then (select top 1 b from @b order by rand()) else 0 end as b from @table/*
    a           b
    ----------- -----------
    1           0
    37          10
    37          24
    37          10
    67          0
    */
      

  4.   

    分开写2个case语句
    case when A字段 = ‘37’ then '24' 
    case when A字段 = ‘37’ then '10'