SQL多重IF语句(ARE卷起)

时间:2014-06-25 19:54:22

标签: sql if-statement

我如何创建一个Sql命令来遵循下面的if语句?

 if (Period  == 9) {9} else { 
     if (Age >= 94 )  {5} else {
     if (Age >= 63 )  {4} else { 
     if (Age >= 32 )  {3} else {
     if (Age >= 1 )   {2} else {
     if (Age >= -30 ) {1} else {
     if (Age < -30 ) {0} else {0}
    }}}}}}

AS'CalcPeriod'

1 个答案:

答案 0 :(得分:0)

案例陈述。以下是在TSQL中使用case语句的链接:

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

SELECT
  CASE WHEN (Period = 9) THEN
     9
  WHEN (Age >= 94) THEN
     5
  WHEN (Age >= 63) THEN
     4
  WHEN (Age >= 32) THEN
     3
  WHEN (Age >= 1) THEN
     2
  WHEN (Age >= -30) THEN
     1
  ELSE
     0
  END AS CalcPeriod
FROM SomeTableWithFieldsPeriodAndAge