我需要一些关于点击计数器算法的想法(按时间间隔分组)

发布于 2024-08-26 20:15:37 字数 838 浏览 6 评论 0原文

我的算法是用于“点击计数器”,如果同一个人在一段时间内访问该网站两次,我会尝试不计算该人两次(例如,如果他在 5 分钟内访问两次,我想将其计为 1) 这是我的数据库的

样子

UserIp     UserId          Date of user came
127.0.0.1   new.user.akb    26.03.2010 10:15:44
127.0.0.1   new.user.akb    26.03.2010 10:16:44
127.0.0.1   new.user.akb    26.03.2010 10:17:44
127.0.0.1   new.user.akb    26.03.2010 10:18:44
127.0.0.1   new.user.akb    26.03.2010 10:19:44
127.0.0.1   new.user.akb    26.03.2010 10:20:44
127.0.0.1   new.user.akb    26.03.2010 10:21:44
127.0.0.1   new.user.akb    26.03.2010 10:22:44
127.0.0.1   new.user.akb    26.03.2010 10:23:44

我需要做的是从上表中获取在一个时间间隔内发生的不同 UserIP 的数量。例如,如果我将时间间隔设置为 5 分钟,假设它开始于

26.03.2010 10:15:44

那么我将得到 2 作为结果,因为在 10:15 到 10:20 之间有 1 个不同值,并且 10 中还有 1 个其他不同值:20 到 10:23,

例如,如果我的间隔是 3 分钟,则返回结果将为 3

My algorithm is for a 'hit counter', I am trying to not count the same person twice if that person came to the site twice in a time interval (For example if he comes twice in 5 minutes, I want to count it as 1 hit for this person)

Here's what my database looks like

UserIp     UserId          Date of user came
127.0.0.1   new.user.akb    26.03.2010 10:15:44
127.0.0.1   new.user.akb    26.03.2010 10:16:44
127.0.0.1   new.user.akb    26.03.2010 10:17:44
127.0.0.1   new.user.akb    26.03.2010 10:18:44
127.0.0.1   new.user.akb    26.03.2010 10:19:44
127.0.0.1   new.user.akb    26.03.2010 10:20:44
127.0.0.1   new.user.akb    26.03.2010 10:21:44
127.0.0.1   new.user.akb    26.03.2010 10:22:44
127.0.0.1   new.user.akb    26.03.2010 10:23:44

What I need to do is get number of distinct UserIPs from the table above that occured within a time interval. For example if I set the time interval for 5 minutes, and let's say that it starts at

26.03.2010 10:15:44

Then I will get 2 as the results, since there is 1 distinct value between 10:15 to 10:20 and , 1 other distinct value from 10:20 to 10:23,

For example if my interval is 3 minutes than the return result will be 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2024-09-02 20:15:37

这里有一个几乎相同的问题:按时间间隔分组。

其基本原理是,您需要通过对日期时间应用下限来按时间间隔进行分组,以使间隔变平。

编辑

使用分组解决了这个问题:

SET DATEFORMAT dmy;

DECLARE @table TABLE
(
    UserIp nvarchar(15),
    UserId nvarchar(15),
    VisitDate datetime
)

INSERT INTO @table
VALUES  ('127.0.0.1', 'new.user.akb', '26.03.2010 10:15:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:16:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:17:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:18:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:19:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:20:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:21:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:22:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:23:44')


SELECT UserIp, UserId, MIN(VisitDate) AS firstVisit
FROM    @table
GROUP BY dateadd(mi, (datepart(mi,VisitDate)/5)*5,
            dateadd(hh, datediff(hh,0,VisitDate),0)),
        UserIp, UserId

这给出了以下结果(我的日期格式是ymd):

UserIp          UserId          firstVisit
--------------- --------------- -----------------------
127.0.0.1       new.user.akb    2010-03-26 10:15:44.000
127.0.0.1       new.user.akb    2010-03-26 10:20:44.000

(2 row(s) affected)

这意味着您可以计算这个结果集每5分钟的访问次数。

There's an almost identical question here: Group by Time interval.

The basics of it is that you need to group by the time interval by applying a floor to the datetime to flatten out the interval.

EDIT

Solved it using grouping:

SET DATEFORMAT dmy;

DECLARE @table TABLE
(
    UserIp nvarchar(15),
    UserId nvarchar(15),
    VisitDate datetime
)

INSERT INTO @table
VALUES  ('127.0.0.1', 'new.user.akb', '26.03.2010 10:15:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:16:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:17:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:18:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:19:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:20:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:21:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:22:44')
        ,('127.0.0.1', 'new.user.akb', '26.03.2010 10:23:44')


SELECT UserIp, UserId, MIN(VisitDate) AS firstVisit
FROM    @table
GROUP BY dateadd(mi, (datepart(mi,VisitDate)/5)*5,
            dateadd(hh, datediff(hh,0,VisitDate),0)),
        UserIp, UserId

This gives the following result (my date format is ymd):

UserIp          UserId          firstVisit
--------------- --------------- -----------------------
127.0.0.1       new.user.akb    2010-03-26 10:15:44.000
127.0.0.1       new.user.akb    2010-03-26 10:20:44.000

(2 row(s) affected)

Which means you can count over this result set for a number of visits per 5min.

还在原地等你 2024-09-02 20:15:37

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);
}

20000101 is some startdate:

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

And here's a C# solution using Linq:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文