2017-04-17 93 views
1

爲什麼我不能得到價值0列時@processdate date = null或週日(該交易不是發生在星期日)列值沒有顯示出來

這是我原來的查詢,只有給我的價值觀當@processdate聲明爲不是星期日或空 我需要它給我看一個0表時@processdate= null或週日

alter procedure USP_CTG_VolumeReport 
    @processdate date = '1/8/2017' 
as 

declare @date date = coalesce(@processdate, cast(getdate() as date)) 

select 'apa' as Source, 
      cast(AccountID_AssignDT as date)as 'Date', 
      count(*) as Total_Count 
from apa_2000 
where 
    cast(AccountID_AssignDT as date) = @date 
group by 
    cast(AccountID_AssignDT as date) 

我應該能夠得到適當的表中的任何星期表與子查詢,但由於某種原因 - 它不工作 以下是我的子查詢是給我相同的結果,我以前的查詢

select 
    'apa' as Source, 
    cast(AccountID_AssignDT as date)as 'Date', 
    isnull((select count(*) 
      from apa_2000 
      where cast(AccountID_AssignDT as date) = @date) 
      , 0) as Total_Count 
from apa_2000 
where 
    cast(AccountID_AssignDT as date) = @date 
group by 
    cast(AccountID_AssignDT as date) 
+0

你能表現出一定的樣本數據,你所得到的結果,什麼你期待? –

+0

出處/日期/ TOTAL_COUNT 是我的結果,還有那些列在下面沒有值時processdate = null或例如「2017年1月8日」(星期日)....我想看到「0」 TOTAL_COUNT下如果processdate爲空或任何一天,這將是週日(基本上任何時候,當沒有什麼可以指望我想看看0 TOTAL_COUNT下,而不是一個空行 – D26

+0

你總是在一個日期路過?如果是這樣,這將使它更容易解決。 –

回答

0

這裏的問題是,在沒有處理髮生的地方,所以計數將爲零,這些日子根本不存在於數據庫中。您期望查詢生成不在那裏的數據。你需要的是一個表,其中包括所有你要舉報打擊,然後來匹配這些日期的計數的日期列表。

如果你總是傳遞一個日期,下面會的工作,由於輸入的日期是你唯一需要的日期:

declare @date as date = '20170416' 

select 
    'apa' as Source, 
    @date as 'Date', 
    count(apa_2000.AccountID_AssignDT) as Total_Count 
from 
    (select 1 as x)x 
    LEFT OUTER JOIN 
    apa_2000 ON cast(AccountID_AssignDT as date) = @date 
+0

對不起,我在我的第一個崗位上做出了一些錯誤。固定現在, –

+0

這是工作!非常感謝! – D26

+0

無後顧之憂。請接受和方式給予好評感謝我的回答和幫助其他用戶找到很好的答案。 –