简体   繁体   English

SQL - 按时间间隔对结果进行分组

[英]SQL - Group results by time interval

With the given table structure ( eventTime is in seconds) I want to group the results by a given time frame: 使用给定的表结构( eventTime以秒为单位),我想按给定的时间范围对结果进行分组:

For example: 5 minutes 例如:5分钟

0 to 5:   1 
5 to 10:  2
10 to 15: 3
.....

How can this be done in SQL Server 2012? 如何在SQL Server 2012中完成?

Thanks! 谢谢!

Florian 弗洛里安

CREATE TABLE [dbo].[evalHubSupply](
[evalHubSupplyId] [int] IDENTITY(1,1) NOT NULL,
[projectId] [int] NOT NULL,
[scenarioId] [int] NOT NULL,
[iterationId] [int] NOT NULL,
[evalExportId] [int] NOT NULL,
[eventType] [varchar](50) NOT NULL,
[eventTime] [int] NOT NULL,
[stopId] [varchar](50) NOT NULL,
[stopName] [varchar](50) NULL,
[vehicleId] [varchar](50) NOT NULL,
[transitLineId] [varchar](50) NULL,
[transitRouteId] [varchar](50) NULL,
[capacity] [int] NULL,
[arrivalTimeAtStop] [int] NULL,
[agentsOnBoard] [int] NULL)

Sample data (interval 1 hour): 样本数据(间隔1小时):

https://dl.dropbox.com/u/481455/table_data.xlsx or https://dl.dropbox.com/u/481455/table_data_open.ods https://dl.dropbox.com/u/481455/table_data.xlsxhttps://dl.dropbox.com/u/481455/table_data_open.ods

The tab "Table data" contains sample data from evalHubSupply. “表数据”选项卡包含来自evalHubSupply的示例数据。 The column "Time interval" calculates the interval (in this case per hour), to which the eventTime relates. “时间间隔”列计算eventTime与之相关的间隔(在本例中为每小时)。 The results tabs counts how many events are related to a specific interval. 结果选项卡计算与特定间隔相关的事件数。

this will give number of records in every 5 min group 这将给出每5分钟组中的记录数

select eventTime/5,count(*)
from   evalHubSupply
group by eventTime/5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM