2011-01-29 264 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)"