2017-04-17 92 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

无后顾之忧。请接受和方式给予好评感谢我的回答和帮助其他用户找到很好的答案。 –