简体   繁体   English

SQL Server 2005忽略case语句中的else

[英]SQL Server 2005 ignores else in case statement

I have a SQL query that runs to produce a list of bounced email addresses. 我有一个SQL查询,该查询可运行以生成退回电子邮件地址的列表。 The issue I'm having is that no matter what I do I don't get back anything when b.SubscriberKey is either 0 or non-existent. 我遇到的问题是,无论我做什么,当b.SubscriberKey为0或不存在时,我都不会得到任何b.SubscriberKey This works fine when b.SubscriberKey is greater than 0. b.SubscriberKey大于0时,这可以正常工作。

I believe it has something to do with doing a join on a table that may have no matching rows, but I would've believed that would have resulted in either a count of 0 or null. 我相信这与在可能没有匹配行的表上进行联接有关,但是我相信这将导致计数为0或为null。 When I changed my query to test this, I still get nothing. 当我更改查询以对此进行测试时,我仍然一无所获。

Edit: I'm looking for the string 'No Bounces' to appear the query runs on a day when I know in fact no bounces have occurred. 编辑:我正在寻找字符串'No Bounces'出现查询运行在我知道实际上没有反弹发生的一天。 When run currently the results is completely blank. 当前运行时,结果完全为空白。

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

from what I understood, you need Subscriberkeys with value 0 or NULLS also to be displayed in your result and you have also stated that it may not have records in that Join table. 据我了解,您还需要在结果中显示值为0或NULLS的Subscriberkeys,并且您还声明了该Join表中可能没有记录。 As you itself stated, in this scenario the best bet is LEFT Join and not INNER 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.

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