case () usage

I recently encountered a case statement when writing SQL statements under summary

1.case statements are usually called popular control statements

1 ) For the determination expression, expression equal value1, resultvalue1 returns, when condition if not returned, then return DefaultValue
 Case expression 

      When VALUE1 the then resultvalue1
      When value2 the then resultvalue2
      When value3 the then resultvalue3
      the else    DefaultValue 

  End 

e.g.  Case saletype When  ' 1 '  the then  ' retail single '  when  ' -1 '  the then  ' Return Order '  End 
such reports do not display when '1' and '-1', the text display "single retail 'and' Return Order '

 

2 ) .condition1, condition2, condition3 conditional expressions. case expression for each function tests back from the front. If the condition 1 is true when he returns to a value corresponding to condition 1 resultvalue1, or if condition2 true when he returned resultvalue2 ........ so if you do not meet the conditions to return the default value of the DefaultValue
 Case  

the when condition1 the then resultvalue1 

When condition2 The the then resultvalue2 

When condition3 the then resultvalue3 

the else    DefaultValue 

End 

e.g.  SUM ( Case  When sizeorder = . 1  the then the qty End )
       Case  When  AVG > 100  the then  ' excellent'  The else  ' good '  End

 

Guess you like

Origin www.cnblogs.com/lita07/p/10958839.html