11
(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)

现在在我的最后一行,我怎么能说 10 及以上应该返回为 150?

4

6 回答 6

14

你不能,这CASE YourFunction WHEN ...只是为了平等。如果您需要使用“大于”,则需要以这种方式重写您的表达式:

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
于 2013-04-08T13:41:28.160 回答
8

您正在使用Simple Case statement不允许的逻辑表达式。您需要使用Searched CASE expression. 但是在您的情况下,由于您使用的是函数,因此从每个表达式的函数中获取返回值会有点昂贵。

这是简单案例和搜索案例语法的 MSDN 链接

我建议你使用sub query with a Searched case如下所示。

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
于 2013-04-08T13:40:54.763 回答
1

来自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

您可以在 WHEN 子句中使用任何布尔表达式

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

您应该在评估之前将 [dbo].[YearsInService]([DateEngaged],getdate()) 保存在变量中。

于 2013-04-08T13:44:38.690 回答
1

假设(根据您的评论) when DateEngagedis NULL,它导致YearsInServicebe NULL,那么我将删除您当前的ELSE子句,然后将其用于 for all other cases,例如:

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

如果对未来日期值存在担忧DateEngaged,我会在内部处理而不是尝试在表达式YearsInService中处理它。CASE

于 2013-04-08T13:53:23.237 回答
1

我喜欢@kaf 的回答,只是想补充一点,您可以通过这个减少案例数量

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
于 2013-04-08T15:51:08.670 回答
0
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

这将所有高于 10 的情况等同于函数的值。

于 2018-01-15T08:26:57.923 回答