简体   繁体   中英

Nested IIF statement issue

Here is the whole statement:

(IIF(i.AvgMonthlyCostOfSales<1,1,(IIF(i.AvgMonthlyCostOfSales)) * (i.StockTurnoverCoefitient-n.OVER_SECOND_RANGE))<0,0,(i.AvgMonthlyCostOfSales) * (i.StockTurnoverCoefitient-n.OVER_SECOND_RANGE) Loss

and getting error:

An expression of non-boolean type specified in a context where a condition is expected, near '('.

What I want to get is: if i.AvgMonthlyCostOfSales value is less than one give me one, else:if multiplication of i.AvgMonthlyCostOfSales and (i.StockTurnoverCoefitient-n.OVER_SECOND_RANGE) is less than zero give me zero. otherwise give me the multiplied value.

Your parentheses were a bit crazy. Perhaps lay it out on separate lines for the condition, true and false:

   IIF( 
     i.AvgMonthlyCostOfSales<1, 
     1,
     IIF(
       i.AvgMonthlyCostOfSales * (i.StockTurnoverCoefitient-n.OVER_SECOND_RANGE)<0,
       0, 
       i.AvgMonthlyCostOfSales * (i.StockTurnoverCoefitient-n.OVER_SECOND_RANGE)
     ) 
   ) Loss

If your SQLserver supports GREATEST it might be simpler to do:

   IIF( 
     i.AvgMonthlyCostOfSales<1, 
     1,
     GREATEST(0, i.AvgMonthlyCostOfSales * (i.StockTurnoverCoefitient-n.OVER_SECOND_RANGE))
   ) Loss

ps; laying your original statement out with indentation makes it easy to see what went wrong:

(
  IIF(
    i.AvgMonthlyCostOfSales<1,
    1,
    (
      IIF(
        i.AvgMonthlyCostOfSales
      )
    ) * (i.StockTurnoverCoefitient-n.OVER_SECOND_RANGE)
  )<0,
  0,
  (i.AvgMonthlyCostOfSales) * (i.StockTurnoverCoefitient-n.OVER_SECOND_RANGE) Loss

it doesn't come back to indent level 0, and you have ) paired with IIF , an IIF that has no bool condition and no true/false etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM