[英]Is there a way to use the distinct keyword inside a conditional case statement of an SQL query?
我有一個SQL語句,其中我想在條件條件語句中使用distinct關鍵字,例如...
SELECT
case when <condition> then distinct t.myfield
else
null
end as my_field
但是,如果嘗試運行查詢,則會收到“缺少表達式”錯誤。
有什么建議么?
提前致謝
與整個select語句不同,而不是單個字段。 您可以執行以下操作:
SELECT DISTINCT Case when <condition> then t.myfield else null end as my_field
但是,這將影響select語句中的其他字段。 一種替代方法是將其添加到單獨的查詢中:
SELECT case when <condition> then t.myfield else null end as my_field
from (select distinct myfield from t) as t
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.