只是徘徊,是否可以在SQL Server中按名称将数据输入每5分钟组。
示例:我在表格中有以下数据:
import Cookies from 'universal-cookie';
const cookies = new Cookies();
cookies.set('cookieName', {key: value}, {path: '/', expires: new Date(Date.now()+2592000)});
如何在SQL Server中获得如下结果?
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
答案 0 :(得分:1)
按名称分组,组日期时间为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