20000101 是某个开始日期:
select dateadd(mi, -d, '20000101') as d, num from
(select count(*) num, datediff(mi ,date_field, '20000101') / 5 * 5 d
from your_table
group by datediff(mi, date_field, '20000101') / 5 * 5 ) as a
order by d
这是使用 Linq 的 C# 解决方案:
var d1 = new Tuple<string, string, DateTime>("127.0.0.1", "new.user.akb", DateTime.Parse("26.03.2010 10:15:44"));
var d2 = new Tuple<string, string, DateTime>("127.0.0.1", "new.user.akb", DateTime.Parse("26.03.2010 10:16:44"));
var d3 = new Tuple<string, string, DateTime>("127.0.0.1", "new.user.akb", DateTime.Parse("26.03.2010 10:17:44"));
var d4 = new Tuple<string, string, DateTime>("127.0.0.1", "new.user.akb", DateTime.Parse("26.03.2010 10:18:44"));
var d5 = new Tuple<string, string, DateTime>("127.0.0.1", "new.user.akb", DateTime.Parse("26.03.2010 10:19:44"));
var d6 = new Tuple<string, string, DateTime>("127.0.0.1", "new.user.akb", DateTime.Parse("26.03.2010 10:20:44"));
var d7 = new Tuple<string, string, DateTime>("127.0.0.1", "new.user.akb", DateTime.Parse("26.03.2010 10:21:44"));
var d8 = new Tuple<string, string, DateTime>("127.0.0.1", "new.user.akb", DateTime.Parse("26.03.2010 10:22:44"));
var d9 = new Tuple<string, string, DateTime>("127.0.0.1", "new.user.akb", DateTime.Parse("26.03.2010 10:23:44"));
var list = new List<Tuple<string, string, DateTime>> {d1, d2, d3, d4, d5, d6, d7, d8, d9};
int interval = 3;
var query = list.GroupBy(data => ((int) (DateTime.Now - data.Item3).TotalMinutes)/interval*interval)
.Select(data => new {IP = data.First().Item1});
foreach (var entry in query)
{
Console.WriteLine(entry.IP);
}