怎么实现按照日期(天),统计每天的数据加总,补全当某一天没有数据时,默认为0的状况.
USE [DBTEST]
GO/****** Object: View [dbo].[V_ProductActAll] Script Date: 2018/5/4 7:56:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER VIEW [dbo].[V_ProductActAll]
AS
--DECLARE @start datetime = '2018-01-01'
--DECLARE @end datetime = '2019-01-07'
--DECLARE @sint int
--DECLARE @eint int
--set @sint=DATEPART(day,@start)
--set @eint=DATEdiff(DAY,@start,@end)
--set language N'Simplified Chinese';
WITH TT AS (
SELECT number
FROM master..spt_values //这是一个系统表
WHERE type = 'P'
AND number BETWEEN DATEPART(day,'2017-01-01')-1 AND DATEdiff(DAY,'2017-01-01','2022-01-07')
) //定义一个TT的类 查询出两个日期之间的所天数序号
select CONVERT(varchar(100), b.[date], 23) as Date,QTY=isnull(a.[count]*250,0) from (select CONVERT(varchar(100), End_TIME, 23) End_TIME,[count]=COUNT(*) from [TEST] group by CONVERT(varchar(100), End_TIME, 23)) a
right join
(select [date]=DATEADD(DAY,TT.number,'2017-01-01') from TT) b ON a.[End_TIME]=b.[date]
GO