简体   繁体   English

SQL Case语句的2+列的值大于1

[英]Sql Case statement with value of 2+ columns greater than 1

I need to add the values in a row from three columns. 我需要在三列中连续添加值。 When the value is greater than 1, it needs to populate a specific phrase. 值大于1时,需要填充特定的词组。

So ... example 所以...例子

From temp_table_tx = count_of_x    
From temp_table_ty = count_of_y    
From temp_table_tz = count_of_z

I currently have the following, which only gives me an error. 我目前有以下情况,这只会给我一个错误。

CASE 
    WHEN (tx.count_of_x + ty.count_of_y + tz.count_of_z) >1 THEN 'Exception_present'
    WHEN (tx.count_of_x + ty.count_of_y + tz.count_of_z) <1 THEN  'No_Exceptions'
ELSE 'error'
End As exceptions

You would get 'error' if any counts were NULL . 如果任何计数为NULL您将得到'error' So, this might fix your problem: 因此,这可能会解决您的问题:

(CASE WHEN coalesce(tx.count_of_x, 0) + coalesce(ty.count_of_y, 0) + coalesce(tz.count_of_z, 0) > 1
      THEN 'Exception_present'
      WHEN coalesce(tx.count_of_x, 0) + coalesce(ty.count_of_y, 0) + coalesce(tz.count_of_z, 0) < 1 THEN 'No_Exceptions'
      ELSE 'error'
 END) As exceptions

It seems odd, though, that a count of exactly "1" would be considered an error. 但是,将计数精确为“ 1”视为错误是很奇怪的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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