簡體   English   中英

where子句中具有“不相等”條件的案例聲明

[英]Case statement in where clause with “not equal” condition

這只是我的實際查詢的一個非常簡單的版本,我想知道如何為以下選擇只編寫一個查詢而不是兩個查詢?

 If @ShowZero = 0 
     Select Value From Metrics 
 Else
     Select Value From Metrics Where Value <> 0

我嘗試了一些

  Select Value From Metrics Where Value = Case @ShowZero = 0 then Value Else (here I'm stuck).

@ShowZero是0和1的標志。

謝謝!

您不需要這種case

Select Value
From Metrics 
Where @ShowZero = 0 or Value <> 0;

編輯:

之所以有效,是因為布爾邏輯是這樣工作的。 換句話說,如果您為表達式做了一個邏輯表,它將看起來像:

@ShowZero    Value       Action
   0           0         Show row
   0         other       Show row
 other         0         Filter out row
 other       other       Show row

有多種表達方式。 if語句就是其中之一。 你可以說:“我要告訴行,除非@ShowZero不為0 值是0 ”。 where子句為:

where not (@ShowZero <> 0 and Value = 0)

或者,您可以說:“當以下任一情況成立時,我將顯示一行:

  • @ShowZero = 0
  • Value <> 0

如果兩者都是真的很棒! 我仍然顯示行。”這是我使用的where子句。

暫無
暫無

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

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