SELECT SDate,sum(SaleGroup) as SaleGroup,sum(SzG) as SzG FROM dbo.Saleday Dbo_saleday
WHERE SDate>='01.12.2016' and SDate<='01.02.2017'
GROUP BY SDate
order by SDate
上面是原SQL
我想让数据每5天为一行统计如何写
SELECT SDate = MIN(SDate) ,sum(SaleGroup) as SaleGroup,sum(SzG) as SzG FROM dbo.Saleday Dbo_saleday
WHERE SDate>='01.12.2016' and SDate<='01.02.2017'
GROUP BY DATEDIFF(Day, '01.12.2016', SDate) / 5
order by SDate
SELECT SDateFrom = MIN(SDate) ,SDateTo = MAX(SDate) ,sum(SaleGroup) as SaleGroup,sum(SzG) as SzG FROM dbo.Saleday Dbo_saleday
WHERE SDate>='01.12.2016' and SDate<='01.02.2017'
GROUP BY DATEDIFF(Day, '01.12.2016', SDate) / 5
declare @dt1 datetime, @dt2 datetime
select @dt1='2016-12-01', @dt2='2017-02-01'
declare @T table([date] datetime)
while @dt1<=@dt2
begin
insert @T select @dt1
set @dt1=@dt1+5
end
SELECT A.[date] as SDate, isnull(sum(SaleGroup), 0) as SaleGroup, isnull(sum(SzG), 0) as SzG
FROM @T as A
left join dbo.Saleday Dbo_saleday as B on B.SDate between A.[date] and A.[date]+5
WHERE B.SDate between @dt1 and @dt2
GROUP BY A.[date]