2

如何在 sql 查询中使用嵌套形式的“case”语句。我正在尝试执行以下查询。

SELECT AccessTabF1.Month, AccessTabF1.Year, AccessTabF1.[Entity Number], 
case when [Exp Year]= 2010 + 1
     then 'Expires in 1 to 5 Years'
     else
          case when [Exp Year]>2010 + 5
               then 'Expires After 5 Years'
               else 'No Expiration Year Listed' 
end
from AccessTabF1
4

1 回答 1

12

在您的情况下,您不需要嵌套多个 CASE 表达式。

SELECT AccessTabF1.Month, AccessTabF1.Year, AccessTabF1.[Entity Number], 
case when [Exp Year] = 2010 + 1 -- why not = 2011 ?
     then 'Expires in 1 to 5 Years' -- this does not match the logic on line above
     when [Exp Year] > 2010 + 5 -- why not > 2015 ?
     then 'Expires After 5 Years'
     else 'No Expiration Year Listed' 
end
from AccessTabF1
于 2012-05-15T23:01:38.173 回答