为什么我无法在0
或星期日(周日没有发生交易)时在列中获得值@processdate date = null
这是我的原始查询,只有当@processdate
被声明为星期一的非星期日或null时才会给我表中的值
当@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)
这里的问题是没有处理的日期,因此计数将为零,数据库中根本就不存在。您期望查询生成不存在的数据。您需要的是一张表格,其中包含您要报告的所有日期的列表,然后根据这些日期匹配计数。
如果您总是在一个日期内传递,则以下情况可行,因为输入日期是您需要的唯一日期:
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