mssql指定时间段按日期分组,没有数据补0的解决办法
select convert(varchar,a.d,111) as [日期],
isnull(b.qty,0) as [销量]
from
(
---生成时间段内所有的天数
select dateadd(day,number,'2020-04-19') as d from master..spt_values
where type='p'
and number >=0
and dateadd(day,number,'2020-04-19') between '2020-04-19' and dateadd(day,0,'2020-04-23')
) as a
left join
(
---按天汇总的销量数据
select convert(varchar,addtime,111) as d,count(id) as qty from Visiting_Log group by convert(varchar,addtime,111)
) as b
on convert(varchar,a.d,111) = b.d