Using tsql greater than sign in CASE Expression

北战南征 提交于 2019-12-30 17:24:07

问题


(case [dbo].[YearsInService]([DateEngaged],getdate())
   when (0) then (0) 
   when (1) then (4) 
   when (2) then (8)
    when (3) then (12)
     when (4) then (32) 
     when (5) then (40) 
     when (6) then (48) 
     when (7) then (56) 
     when (8) then (104) 
     when (9) then (117) 
     when (10) then (150) else (-1) end)

Now in my the last line, how can i say that 10 and above should be returned as 150?


回答1:


You can't, the CASE YourFunction WHEN ... is for equalities only. If you need to use "greater than", you'll need to rewrite your expression this way:

CASE WHEN [dbo].[YearsInService]([DateEngaged],getdate()) = 0 THEN 0
WHEN [dbo].[YearsInService]([DateEngaged],getdate()) = 1 THEN 4
WHEN.....
WHEN [dbo].[YearsInService]([DateEngaged],getdate()) >= 10 THEN 150 ELSE -1 END



回答2:


You are using a Simple Case statement where logical expressions are not allowed. You need to use a Searched CASE expression. But in your case since you are using a function it will be bit costly to get the return value from the function for each expression.

Here is MSDN Link for both Simple Case and Searched CASE Syntax

I would suggest you to use a sub query with a Searched case as bellow.

select case when results = 0 then 0
            when results = 1 then 4
            ...
            when results >= 10 then 150
            else -1 end as CaseResults
from (select [dbo].[YearsInService]([DateEngaged],getdate()) results
      from yourTable
     ) Temp



回答3:


from http://msdn.microsoft.com/en-us/library/ms181765.aspx

SELECT 
  CASE 
     WHEN MIN(value) <= 0 THEN 0 
     WHEN MAX(1/value) >= 100 THEN 1 
  END 
FROM Data

you can use any boolean expression in the WHEN clause

case 
  when ([dbo].[YearsInService]([DateEngaged],getdate()) = 0) then (0) 
  when ([dbo].[YearsInService]([DateEngaged],getdate()) = 1) then (4) 
  when ([dbo].[YearsInService]([DateEngaged],getdate()) = 2) then (8)
  when ([dbo].[YearsInService]([DateEngaged],getdate()) = 3) then (12)
  when ([dbo].[YearsInService]([DateEngaged],getdate()) = 4) then (32) 
  when ([dbo].[YearsInService]([DateEngaged],getdate()) = 5) then (40) 
  when ([dbo].[YearsInService]([DateEngaged],getdate()) = 6) then (48) 
  when ([dbo].[YearsInService]([DateEngaged],getdate()) = 7) then (56) 
  when ([dbo].[YearsInService]([DateEngaged],getdate()) = 8) then (104) 
  when ([dbo].[YearsInService]([DateEngaged],getdate()) = 9) then (117) 
  when ([dbo].[YearsInService]([DateEngaged],getdate()) >= 10) then (150) 
  else (-1) end

you should save [dbo].[YearsInService]([DateEngaged],getdate()) in a variable before evaulation tho.




回答4:


Assuming (from your comment) that when DateEngaged is NULL, it causes YearsInService to be NULL, then I'd remove your current ELSE clause, and then use that for all other cases, something like:

case COALESCE([dbo].[YearsInService]([DateEngaged],getdate()),-1)
when (-1) then (-1)
when (0) then (0) 
when (1) then (4) 
when (2) then (8)
when (3) then (12)
 when (4) then (32) 
 when (5) then (40) 
 when (6) then (48) 
 when (7) then (56) 
 when (8) then (104) 
 when (9) then (117) 
 else (150) end

If there's a concern about future-dated DateEngaged values, I'd deal with that inside of YearsInService rather than try to deal with it in the CASE expression.




回答5:


I like @kaf 's answer, just want add that you may reduce the number of case by this

select case when results BETWEEN 0 AND 3 then results * 4
            when results BETWEEN 4 AND 7 then results * 8
            when results BETWEEN 8 AND 9 then results * 13
            when results >= 10 then 150
            else -1 
        end as CaseResults
from (select [dbo].[YearsInService]([DateEngaged],getdate()) results
      from yourTable
     ) Temp



回答6:


case [dbo].[YearsInService]([DateEngaged],getdate())
   when (0) then (0) 
   when (1) then (4) 
   when (2) then (8)
    when (3) then (12)
     when (4) then (32) 
     when (5) then (40) 
     when (6) then (48) 
     when (7) then (56) 
     when (8) then (104) 
     when (9) then (117) 
     when IIF(case [dbo].[YearsInService]([DateEngaged],getdate()) >= 10 
           ,[dbo].[YearsInService]([DateEngaged],getdate())
           ,10) 
        then (150) 
     else (-1) end

This equates all the cases above 10 to the value of the function.



来源:https://stackoverflow.com/questions/15880480/using-tsql-greater-than-sign-in-case-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!