簡體   English   中英

SQL Case語句的2+列的值大於1

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

我需要在三列中連續添加值。 值大於1時,需要填充特定的詞組。

所以...例子

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

我目前有以下情況,這只會給我一個錯誤。

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

如果任何計數為NULL您將得到'error' 因此,這可能會解決您的問題:

(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

但是,將計數精確為“ 1”視為錯誤是很奇怪的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM