我正在尝试实现以下运行总计,如下所示。其中a2013是该月的查询数量,2013c是运行总数。
Month a2013 2013C
---------------------
Jan 1 1
Feb 3 4
March 2 6
在类似的帖子中,它建议添加我已经完成的分区,但返回的值仍然不正确。
with years as (
select datename(month,p.enquirydate) as Month, count(*) as count,
count(case when p.enquirydate between '2013-01-01' and '2013-12-31
23:59:59.997' and EnrolmentYear ='2018' then datename(month,p.enquirydate)
else null end) as a2013,
count(case when p.enquirydate between '2014-01-01' and '2014-12-31
23:59:59.997' and EnrolmentYear ='2018' then datename(month,p.enquirydate)
else null end) as a2014,
count(case when p.enquirydate between '2015-01-01' and '2015-12-31
23:59:59.997' and EnrolmentYear ='2018' then datename(month,p.enquirydate)
else null end) as a2015,
count(case when p.txtenquirydate between '2016-01-01' and '2016-12-31
23:59:59.997' and EnrolmentYear ='2018' then datename(month,p.enquirydate)
else null end) as a2016,
count(case when p.enquirydate between '2017-01-01' and '2017-12-31
23:59:59.997' and EnrolmentYear ='2018' then datename(month,p.enquirydate)
else null end) as a2017
from customers p
group by datename(month,p.enquirydate), datepart(month,p.enquirydate)
)
select y.Month,a2013, sum(a2013) over(partition by y.month) as '2013C'
from years y
Month a2013 2013C
NULL 0 0
April 4 4
August 1 1
December 4 4
February 3 3
January 1 1
July 3 3
June 3 3
March 2 2
May 1 1
November 4 4
October 3 3
September 4 4
答案 0 :(得分:0)
也许以下查询可以帮助您使用SUM()和UnBounded Preceding子句在SQL Server上实现SQL Running Sum解决方案
我曾经用于库存老化问题
declare @t as table (Id int, Month varchar(100), a2013 int)
insert into @t values
(1,NULL ,0),
(4,'April', 4 ),
(8,'August', 1 ),
(12,'December', 4 ),
(2,'February', 3 ),
(1,'January', 1 ),
(7,'July', 3 ),
(6,'June', 3 ),
(3,'March', 2 ),
(5,'May', 1 ),
(11,'November', 4 ),
(10,'October', 3 ),
(9,'September', 4 )
select
*,
sum([a2013]) over (order by Id rows unbounded preceding) as [2013c]
from @t
order by Id
如您所见,最后一列显示的是
之前显示的行数答案 1 :(得分:0)
with years as (
select DATEPART(month,enquirydate) as MonthNumber, count(*) as count,
count(case when p.enquirydate between '2013-01-01' and '2013-12-31 23:59:59.997' and EnrolmentYear ='2018' then datename(month,p.enquirydate)
else null end) as a2013,
count(case when p.enquirydate between '2014-01-01' and '2014-12-31 23:59:59.997' and EnrolmentYear ='2018' then datename(month,p.enquirydate)
else null end) as a2014,
count(case when p.enquirydate between '2015-01-01' and '2015-12-31 23:59:59.997' and EnrolmentYear ='2018' then datename(month,p.enquirydate)
else null end) as a2015,
count(case when p.enquirydate between '2016-01-01' and '2016-12-31 23:59:59.997' and EnrolmentYear ='2018' then datename(month,p.enquirydate)
else null end) as a2016,
count(case when p.enquirydate between '2017-01-01' and '2017-12-31 23:59:59.997' and EnrolmentYear ='2018' then datename(month,p.enquirydate)
else null end) as a2017
from @customers p
group by DATEPART(month,enquirydate)
)
select datename(month,dateadd(month, MonthNumber - 1, 0)), a2013, sum(a2013) over(Order By y.MonthNumber ROWS UNBOUNDED PRECEDING) as '2013C'
from years y