0

这是我的第一个 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 中的分组或分析函数来完成这项工作?

4

2 回答 2

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 年及以后有效。

于 2013-11-15T08:27:36.613 回答
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
于 2013-11-15T08:37:24.157 回答