在SQL Server中汇总值

时间:2013-11-15 08:18:11

标签: sql-server

这是我的第一个sql语句的结果:

SELECT 
    count(*) countQuarter, Hour, Quarter,
    ROW_NUMBER() OVER(ORDER BY Hour, Quarter ASC) AS rownum
FROM
     (SELECT [ID] ,[simulationID] ,[time],
      replace(str(time/3600,len(ltrim(time/3600))+abs(sign(time/359999)-1)) + ':' +         str((time/60)%60,2) + ':' + str(time%60,2),' ','0') dtString, 
      (time/3600) Hour, (time/60)%60 Minute, case  when (time/60)%60<15 then 15 when 
      (time/60)%60<30 then 30 when (time/60)%60<45 then 45 when (time/60)%60<60 then 60 end 
      Quarter ,[person] ,[link] ,[vehicle] FROM [TEST].[dbo].[evtLinks]
      WHERE simulationID=@simulationID) B
 GROUP BY Hour, Quarter

给出了以下结果:

Count     Hour  Quarter Rownum
497         0     15      1
842         0     30      2
1033        0     45      3
1120        0     60      4
1235        1     15      5
1267        1     30      6
1267        1     45      7
1267        1     60      8
1267        2     15      9
1267        2     30     10

我想要一个结果,其中列fullCount是实际行的Count和下一个3的总和!

Count    Hour  Quarter  Rownum  Fullcount
497         0     15      1      3492
842         0     30      2      4230
1033        0     45      3      4655 
1120        0     60      4       ...
1235        1     15      5
1267        1     30      6
1267        1     45      7
1267        1     60      8
1267        2     15      9
1267        2     30     10

如何在SQL Server中使用分组或分析功能完成此操作?

2 个答案:

答案 0 :(得分:1)

对于SQL Server 2012,是的,可以这样做:

declare @t table ([Count] int,[Hour] int,[Quarter] int,Rownum int)
insert into @t([Count],[Hour],[Quarter],Rownum) values
(497    ,    0  ,  15  ,   1 ),
(842    ,    0  ,  30  ,   2 ),
(1033   ,    0  ,  45  ,   3 ),
(1120   ,    0  ,  60  ,   4 ),
(1235   ,    1  ,  15  ,   5 ),
(1267   ,    1  ,  30  ,   6 ),
(1267   ,    1  ,  45  ,   7 ),
(1267   ,    1  ,  60  ,   8 ),
(1267   ,    2  ,  15  ,   9 ),
(1267   ,    2  ,  30  ,  10 )

select *,SUM([Count]) OVER (
                         ORDER BY rownum
                         ROWS BETWEEN CURRENT ROW AND
                                      3 FOLLOWING)
from @t

这里我使用@t作为您当前的结果集 - 您可以将其调整为当前查询,或者可能必须将当前查询放在CTE中。

不幸的是,ROWS BETWEEN语法仅在2012及更高版本有效。

答案 1 :(得分:1)

测试了逻辑场景并且它有效,但是我没有你的数据,所以在你的情况下它看起来应该大致如下:

;WITH CTE as (SELECT count(*) countQuarter,Hour,Quarter,
       ROW_NUMBER() OVER(ORDER BY Hour, Quarter ASC) AS rownum
FROM
     (SELECT [ID] ,[simulationID] ,[time],
      replace(str(time/3600,len(ltrim(time/3600))+abs(sign(time/359999)-1)) + ':' +         str((time/60)%60,2) + ':' + str(time%60,2),' ','0') dtString, 
      (time/3600) Hour, (time/60)%60 Minute, case  when (time/60)%60<15 then 15 when 
      (time/60)%60<30 then 30 when (time/60)%60<45 then 45 when (time/60)%60<60 then 60 end 
      Quarter ,[person] ,[link] ,[vehicle] FROM [TEST].[dbo].[evtLinks]
      WHERE simulationID=@simulationID) B
 GROUP BY Hour, Quarter)
SELECT *, CA.Fullcount 
FROM CTE
CROSS APPLY (SELECT SUM(countQuarter) Fullcount FROM CTE C WHERE C.ID BETWEEN CTE.ID AND CTE.ID+3) CA