comparing null in SQL comparisons

三世轮回 提交于 2019-12-02 07:38:55

You cannot use when user_id <> Null. You must use user_id Is Null or user_id Is Not Null. Anything = or <> to Null results in Unknown which is treated as false for the Case expression.

I guess for sysbase it is the same as for sql-server.

There is a setting to switch between (old?) sybase default

set ansi_nulls off

select case when null = null then 1 else 0 end
-- returns 1

and ansi behaviour.

set ansi_nulls on

select case when null = null then 1 else 0 end
-- returns 0

Today the question is hardly which setting is more elegant, but with which setting causes more trouble.

General Rule: any operation involving NULL yields NULL. All comparisons involving NULL fail, regardless of whether or not the test is positive ('==') or negative ('<>'). The sole exception being explicit tests for nullity via IS [NOT] NULL or use of COALESCE()/ISNULL().

Nicholas, I agree with you in that comparisons against null should always be false, however I tried the following code on an ASE 15.0.3 server and got a surprising result

declare @xx int
select @xx = null
select @xx
select 1 where null = @xx

It return 1 for the second select which I was not expecting..

Just so you know the count(seqnum) as Answered is not the correct definition for Aspect UIP inbound calls answered. If you check the field labeled SwitchDispId it could have a 1 or 2 in it which would equate to an abandon call. Hence not a answered call. I do see you are using user_id not null to get answered calls but I just wanted to make you aware of that.

Also you can join the acdcalldetail table to the service table to get names that look more like what the business is used to seeing like this:

SELECT 
     Service_c
    ,SUM(CASE WHEN acd.SwitchDispID IN (1,2) THEN 1 ELSE 0 END as Abandons
    ,SUM(CASE WHEN acd.user_id is not null THEN 1 ELSE 0 END as Answered
FROM acdcalldetail acd
JOIN service s
    ON s.service_id = acd.service_id
    AND s.sourceid = acd.sourceid
WHERE acd.CallStartDt between '20170501' AND '20170530'
AND s.Service_id NOT IN  (37,39,47,51,57,58,96,215,374,375)
GROUP BY 
    s.Service_c

"select datepart (hh, callstartdt) as Hour, " _
    & " count(seqnum) as Anaswered," _
    & " sum(case when user_id <> NULL then 1 else 0 end) as answered_calls ," _
    & " sum(case when user_id <> NULL and  datediff (ss, callstartdt, QueueEndDt) <= 20 then 1 else 0 end) , " _
    & " sum(case when user_id = NULL then 1 else 0 end), " _
    & " sum(case when user_id <> NULL and datediff (ss, callstartdt, QueueEndDt) <= 20 then 1 else 0 end)  / count(seqnum), " _
    & " sum(Case when user_id <> NULL then 1 else 0 end ) / count(seqnum) from acdcalldetail " _
    & " where callstartdt between '" & fromDt & "' and '" & toDt & "' " _
    & " and service_id not in (37,39,47,51,57,58,96,215,374,375) " _
    & " group by datepart (hh, callstartdt) " _
    & " order by datepart (hh, callstartdt)"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!