MySQL 查询 - 查找“新”每日用户数
我有一个包含以下字段的数据表
EventID : Int, AutoIncrement, Primary Key
EventType : Int ' Defines what happened
EventTimeStamp : DateTime ' When the Event Happened
UserID : Int ' Unique
查询需要告诉我整个集合中每天使用新 UserID 发生的事件数。因此,每天存在多少个具有前一天不存在的 UserID 的事件。我已经尝试了很多,并且每天可以获得唯一用户,但无法弄清楚如何每天获得“新”用户。
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. So, for each day, how many events exist which have a UserID which doesn't exist in any prior day. I've tried lots, and I can get unique users per day, but can't work out how to get 'NEW' users per day.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
感谢大家的帮助 - 我已投票赞成该帮助。这就是我所做的:
我创建了这 2 个视图(我需要最终得到一个视图,并且必须创建 2 个视图,因为看起来您无法在视图中嵌套 select 语句)。
目击事件:
新用户:
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).
Sightings:
NewUsers:
好问题。我没有确切的解决方案,但这是我之前见过的方法:
执行 SELECT,将给定 userID 的
EventTimeStamp
与MIN(EventTimeStamp)
进行比较,由同一表上的嵌套 SELECT 语句确定,以计算每个 ID 的 MIN 时间戳(例如GROUP BY UserID
)。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
withMIN(EventTimeStamp)
for a given userID, as determined by a nested SELECT statement on the same table to calculate the MIN timestamp for each ID (e.g.GROUP BY UserID
).首先为每个用户第一次到达时获取一个表
b
,然后加入该表以获取该用户当天的所有事件。
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.
86400-(EventTimeStamp) 作为 new_users
86400-(EventTimeStamp) as new_users