2011-01-29 51 views
1

我用VB6创建一个前端,我的数据库是Sybase。使用DSN我创建了一些小的exe来在网格中填充报表。它工作正常。在SQL比较中比较空值

但是,如果我使用下面的查询,我只得到小时和应答数据。如果我在SQL Query中执行查询,那么完整的数据即将到来。

我相信总和(情况下不会在VB6工作,请指导我一个备用。

"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)" 

回答

5

不能使用when user_id <> Null。你必须使用user_id Is Nulluser_id Is Not Null。任何=或<>为Null在未知的结果被视为假的案例表达

+0

我有一个便条贴在我的电脑,上面写着“ NULL总是未知“。总是看到它是有帮助的。 – 2011-01-29 18:37:37

+0

出色答卷 - 这一个总是分不清新手 –

+0

托马斯...由于一吨... – DhilK

0

一般规则:。涉及NULL生成NULL任何操作,所有NULL涉及比较失败,无论测试是否为正(“==”)或负('<>')唯一的例外是明确的t通过IS [NOT] NULL或使用COALESCE()/ ISNULL()来检验无效性。

1

我猜sysbase和sql-server是一样的。

还有就是(老?)SYBASE默认

set ansi_nulls off 

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

和ANSI行为之间切换设置。

set ansi_nulls on 

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

今天这个问题很难说哪个设置更优雅,但是哪个设置会带来更多的麻烦。

+0

这个问题已经解决。感谢所有...我打开了另一个问题,请帮助我.... – DhilK

0

尼古拉斯,我同意你的反对无效是比较应该永远是假的,但我想一个ASE 15.0.3服务器上的下面的代码,并得到了一个令人惊讶的结果

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

它的返回1第二个选择,我没想到..

0

只要你知道伯爵(SEQNUM)的回答是不是回答看点UIP呼入呼叫的正确定义。如果您检查标记为SwitchDispId的字段,它可能会有1或2个等于放弃呼叫。因此没有接听电话。我确实看到你正在使用user_id not null来接听电话,但我只是想让你知道这一点。

您也可以加入acdcalldetail表服务表让看起来更像是企业用来看到这样的名字:

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)"