简体   繁体   English

如何找到每个用户 ID 的第二个最小登录时间

[英]How to find the second minimum log in time per each user id

I'm working on a SQL query on BigQuery and trying to find the second minimum login time for each user id.我正在 BigQuery 上进行 SQL 查询,并试图为每个用户 ID 找到第二个最短登录时间。

User_id用户身份 LoginTime登录时间
1 1个 2022-11-06 08:45:17 2022-11-06 08:45:17
1 1个 2022-11-07 09:52:27 2022-11-07 09:52:27
1 1个 2022-11-08 02:08:54 2022-11-08 02:08:54
2 2个 2022-11-04 08:10:38 2022-11-04 08:10:38
2 2个 2022-11-07 16:46:34 2022-11-07 16:46:34
2 2个 2022-11-18 01:26:53 2022-11-18 01:26:53

And I want to find the following result.我想找到以下结果。

User_id用户身份 SecondLoginTime第二次登录时间
1 1个 2022-11-07 09:52:27 2022-11-07 09:52:27
2 2个 2022-11-07 16:46:34 2022-11-07 16:46:34

Could anyone help me figure out how to approach this problem?谁能帮我弄清楚如何解决这个问题?

We can use the RANK() analytic function:我们可以使用RANK()分析函数:

WITH cte AS (
    SELECT *, RANK() OVER (PARTITION BY User_id ORDER BY LoginTime) rnk
    FROM yourTable
)

SELECT User_id, LoginTime AS SecondLoginTime
FROM cte
WHERE rnk = 2
ORDER BY User_id;

Note that, depending on the behavior you want if ties can happen, you may want to use DENSE_RANK() instead of RANK() .请注意,根据您想要的行为,如果关系可能发生,您可能想要使用DENSE_RANK()而不是RANK()

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

相关问题 如何找到第二次登录时间之前的事件总数 - How to find the total number of events before the second login time AWS API Gateway + Lamda - 如何处理每秒 100 万个请求 - AWS API Gateway + Lamda - how to handle 1 million requests per second 在 Bigquery 中计算每天总记录数和每天具有相同时间时间戳和 id 的总记录数的查询 - Query that counts total records per day and total records with same time timestamp and id per day in Bigquery aws cli:如何找到 kms 密钥 ID? - aws cli: how to find kms key id? 如何使用 Event Webhook 查找 Api 密钥 ID - How to find Api key ID with Event Webhook 如何根据日期间隔获取每个 ID 和月份的最新事件列表? - How to get list of latest event per Id and month based on date interval? AWS KMS 请求每秒限制的解决方法 - Workaround for AWS KMS request per second limit 如何在 go 微服务中为每个日志添加跟踪 ID - How to add trace id to each logs in go micro service 如何将 user.uid 作为 id 的文档添加到 Firebase 中的集合中? - How to add a document with user.uid as id to a collection in Firebase? 如何以秒为单位监控 Lambda 并发执行(或找到更好的解决方案来限制 Lambda 并发执行)? - How Could I Monitor Lambda Concurrent Executions on a Second-by-Second Basis (or Find a Better Solution to Limit Lambda ConcurrentExecutions)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM