如何在SQL Server中每5分钟将数据导入一次?

如何在SQL Server中每5分钟将数据导入一次?,sql,sql-server,Sql,Sql Server,在SQL Server中,每5分钟就有可能按名称将数据放入一个组中 示例:我在表中有以下数据: id name message datetime --- -------- ------- -------- 1 David test 1 2017-12-18 10:00 2 David test 2 2017-12-18 10:01 3 David

在SQL Server中,每5分钟就有可能按名称将数据放入一个组中

示例:我在表中有以下数据:

id    name          message       datetime
---   --------      -------       --------   
1     David         test 1        2017-12-18 10:00
2     David         test 2        2017-12-18 10:01
3     David         test 3        2017-12-18 10:03
4     Alvin         bluh 1        2017-12-18 10:04
5     Alvin         bluh 2        2017-12-18 10:04
6     David         test 4        2017-12-18 10:06
如何在SQL Server中获得以下结果

id    name          message       datetime
---   --------      -------       --------   
1     David         test 1        2017-12-18 10:00
2     David         test 2        
3     David         test 3        
4     Alvin         bluh 1        2017-12-18 10:04
5     Alvin         bluh 2        
6     David         test 4        2017-12-18 10:06

按名称分组,按5分钟分组日期时间

select  id, name, message, 
        datetime    = case when rn = 1 then datetime end    
from    (
            select  *, rn = row_number() over (partition by name, 
                                dateadd(minute, datediff(minute, 0, datetime) / 5 * 5, 0) 
                               order by datetime)
            from    yourtable t
        ) d
order by id

10:00
10:04
不是相隔5分钟,为什么它仍然显示?@Squirrel group by name和message“test 1”与“test 2”不是同一条消息您的
每5分钟
是固定的(例如10:00-->10:04,10:05->10:09),或者它取决于第一行的
datetime
值?如果第一行是
2017-12-18 09:59
,结果是什么?问题仍然不清楚。。请添加更多的解释…我需要它是由名称和日期时间组(在5分钟内)。参考上述示例,脚本将返回日期为2017-12-18 10:00的3组David记录和日期为2017-12-18 10:00的1组David记录:06@JinYong. 编辑查询