SQL TSQL - 如何对 2 列使用 case 语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/605304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:20:11  来源:igfitidea点击:

TSQL - How to use a case statement for 2 columns?

sqltsql

提问by Malcolm

I want to do a case on the result of 2 columns. How do I do this?

我想对 2 列的结果做一个案例。我该怎么做呢?

e.g.:

例如:

SELECT CASE amount=100 AND DATE IS NOT NULL WHEN 0 THEN 'Something' ELSE ''

Something like that?

类似的东西?

回答by Marc Gravell

select case
    when amount = 100 and date is not null then 'something'
    else 'something else'
    end

This is a "searched case expression" (see MSDN):

这是一个“搜索案例表达式”(参见 MSDN):

    CASE
      WHEN Boolean_expression THEN result_expression [ ...n ] 
      [ ELSE else_result_expression ]  END

回答by Mitch Wheat

select someColumnName,
       case 
         when amount = 100  AND someothercondition then 'XXX'
         when amount = 1000  AND anothercondition then 'YYY'
         else 'WWW' 
       end as "MyColumnName"
from myTable

回答by Mufaka

select 
case 
    when 
        amount = 100 
        and date is not null    
    then 
        '0'
    else 
        'something else'
end  

回答by Suhail

select case when amount = 100 and date is not null then 'something' else 'something when amount=0 then 'something else' end as MyColumnName

选择 case when amount = 100 and date is not null then 'something' else 'something when amount=0 then 'something else' 以 MyColumnName 结尾