[英]SQL Server 2005 ignores else in case statement
我有一個SQL查詢,該查詢可運行以生成退回電子郵件地址的列表。 我遇到的問題是,無論我做什么,當b.SubscriberKey
為0或不存在時,我都不會得到任何b.SubscriberKey
。 當b.SubscriberKey
大於0時,這可以正常工作。
我相信這與在可能沒有匹配行的表上進行聯接有關,但是我相信這將導致計數為0或為null。 當我更改查詢以對此進行測試時,我仍然一無所獲。
編輯:我正在尋找字符串'No Bounces'出現查詢運行在我知道實際上沒有反彈發生的一天。 當前運行時,結果完全為空白。
Select
case
when count(b.SubscriberKey) is not null
then b.SubscriberKey
else 'No bounces'
end as SubscriberKey
from
_bounce b
Join
_Job j with (nolock) on j.JobID = b.JobID
where
convert(date, b.EventDate) = convert(date, dateadd(dd, -1, getdate()))
and j.EmailID = 66653
group by
b.SubscriberKey
據我了解,您還需要在結果中顯示值為0或NULLS的Subscriberkeys,並且您還聲明了該Join表中可能沒有記錄。 正如您自己所說的,在這種情況下,最好的選擇是左聯接而不是內聯接
Select case when b.SubscriberKey is not null then b.SubscriberKey else 'No bounces' end as SubscriberKey
from _Job j with (nolock)
LEFT Join _bounce b
on j.JobID = b.JobID
where convert(date,b.EventDate)=convert(date,dateadd(dd,-1,getdate()))
and j.EmailID = 66653
group by b.SubscriberKey
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.