简体   繁体   English

MySQL查询 - 每天查找“新”用户

[英]MySQL query - find “new” users per day

I have a table of data with the following fields 我有一个包含以下字段的数据表

EventID        : Int, AutoIncrement, Primary Key
EventType      : Int                             ' Defines what happened
EventTimeStamp : DateTime                        ' When the Event Happened
UserID         : Int                             ' Unique

The query needs to tell me how many events occurred with new UserIDs for each day in the whole set. 查询需要告诉我整个集合中每天的新UserID发生了多少事件。 So, for each day, how many events exist which have a UserID which doesn't exist in any prior day. 因此,对于每一天,存在多少具有在任何前一天不存在的UserID的事件。 I've tried lots, and I can get unique users per day, but can't work out how to get 'NEW' users per day. 我已经尝试了很多,而且我每天都可以获得独特的用户,但无法确定如何每天获得“新”用​​户。

Select count(EventId) from table
where 
UserId 
  not in (select UserId from table where EventTimeStamp < now() - interval 1 day)

Good question. 好问题。 I don't have an exact solution but here's the approach I've seen before: 我没有一个确切的解决方案,但这是我以前见过的方法:

Do a SELECT where you compare the EventTimeStamp with MIN(EventTimeStamp) for a given userID, as determined by a nested SELECT statement on the same table to calculate the MIN timestamp for each ID (eg GROUP BY UserID ). 执行SELECT,将EventTimeStampMIN(EventTimeStamp)进行比较以EventTimeStamp给定的userID,由同一表上的嵌套SELECT语句确定,以计算每个ID的MIN时间戳(例如GROUP BY UserID )。

First get a table b with for each user when he first arrived, then join that table to get all events for that user for that day. 首先获得每个用户第一次到达时的表b ,然后加入该表以获取该用户当天的所有事件。

SELECT DATE(a.EventTimeStamp), COUNT(*) FROM table a
JOIN
(SELECT MIN(EventTimeStamp) AS EventTimeStamp, UserID from table group by userID) b
ON a.UserID = b.UserID
AND DATE(a.EventTimeStamp) = DATE(b.EventTimeStamp) 
GROUP BY DATE(a.EventTimeStamp) 

Thank you all for your help - I've voted up the assistance. 谢谢大家的帮助 - 我已经投了很多帮助。 Here's what I did: 这是我做的:

I created these 2 views (I needed to end up with a view, and had to create 2 as it seems you can't nest select statements within views). 我创建了这两个视图(我需要最终得到一个视图,并且必须创建2,因为它似乎无法在视图中嵌套select语句)。

Sightings: 目击:

select min(to_days(`Events`.TimeStamp)) AS Day0,
    `Events`.TimeStamp AS TimeStamp,
    `Events`.UserID AS UserID
from `Events` group by `Events`.UserID order by `Events`.UserID

NewUsers: 新用户:

select count(distinct Sightings.UserID) AS Count,
    date(Sightings.TimeStamp) AS Date from Sightings
    group by date(Sightings.TimeStamp)

86400-(EventTimeStamp)为new_users

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

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