{ "data": { "question": { "questionId": "4043", "questionFrontendId": "3673", "categoryTitle": "Database", "boundTopicId": 3774328, "title": "Find Zombie Sessions", "titleSlug": "find-zombie-sessions", "content": "
Table: app_events
\n+------------------+----------+\n| Column Name | Type | \n+------------------+----------+\n| event_id | int |\n| user_id | int |\n| event_timestamp | datetime |\n| event_type | varchar |\n| session_id | varchar |\n| event_value | int |\n+------------------+----------+\nevent_id is the unique identifier for this table.\nevent_type can be app_open, click, scroll, purchase, or app_close.\nsession_id groups events within the same user session.\nevent_value represents: for purchase - amount in dollars, for scroll - pixels scrolled, for others - NULL.\n\n\n
Write a solution to identify zombie sessions, sessions where users appear active but show abnormal behavior patterns. A session is considered a zombie session if it meets ALL the following criteria:
\n\n30
minutes.5
scroll events.0.20
.Return the result table ordered by scroll_count
in descending order, then by session_id
in ascending order.
The result format is in the following example.
\n\n\n
Example:
\n\nInput:
\n\napp_events table:
\n\n\n+----------+---------+---------------------+------------+------------+-------------+\n| event_id | user_id | event_timestamp | event_type | session_id | event_value |\n+----------+---------+---------------------+------------+------------+-------------+\n| 1 | 201 | 2024-03-01 10:00:00 | app_open | S001 | NULL |\n| 2 | 201 | 2024-03-01 10:05:00 | scroll | S001 | 500 |\n| 3 | 201 | 2024-03-01 10:10:00 | scroll | S001 | 750 |\n| 4 | 201 | 2024-03-01 10:15:00 | scroll | S001 | 600 |\n| 5 | 201 | 2024-03-01 10:20:00 | scroll | S001 | 800 |\n| 6 | 201 | 2024-03-01 10:25:00 | scroll | S001 | 550 |\n| 7 | 201 | 2024-03-01 10:30:00 | scroll | S001 | 900 |\n| 8 | 201 | 2024-03-01 10:35:00 | app_close | S001 | NULL |\n| 9 | 202 | 2024-03-01 11:00:00 | app_open | S002 | NULL |\n| 10 | 202 | 2024-03-01 11:02:00 | click | S002 | NULL |\n| 11 | 202 | 2024-03-01 11:05:00 | scroll | S002 | 400 |\n| 12 | 202 | 2024-03-01 11:08:00 | click | S002 | NULL |\n| 13 | 202 | 2024-03-01 11:10:00 | scroll | S002 | 350 |\n| 14 | 202 | 2024-03-01 11:15:00 | purchase | S002 | 50 |\n| 15 | 202 | 2024-03-01 11:20:00 | app_close | S002 | NULL |\n| 16 | 203 | 2024-03-01 12:00:00 | app_open | S003 | NULL |\n| 17 | 203 | 2024-03-01 12:10:00 | scroll | S003 | 1000 |\n| 18 | 203 | 2024-03-01 12:20:00 | scroll | S003 | 1200 |\n| 19 | 203 | 2024-03-01 12:25:00 | click | S003 | NULL |\n| 20 | 203 | 2024-03-01 12:30:00 | scroll | S003 | 800 |\n| 21 | 203 | 2024-03-01 12:40:00 | scroll | S003 | 900 |\n| 22 | 203 | 2024-03-01 12:50:00 | scroll | S003 | 1100 |\n| 23 | 203 | 2024-03-01 13:00:00 | app_close | S003 | NULL |\n| 24 | 204 | 2024-03-01 14:00:00 | app_open | S004 | NULL |\n| 25 | 204 | 2024-03-01 14:05:00 | scroll | S004 | 600 |\n| 26 | 204 | 2024-03-01 14:08:00 | scroll | S004 | 700 |\n| 27 | 204 | 2024-03-01 14:10:00 | click | S004 | NULL |\n| 28 | 204 | 2024-03-01 14:12:00 | app_close | S004 | NULL |\n+----------+---------+---------------------+------------+------------+-------------+\n\n\n
Output:
\n\n\n+------------+---------+--------------------------+--------------+\n| session_id | user_id | session_duration_minutes | scroll_count |\n+------------+---------+--------------------------+--------------+\n| S001 | 201 | 35 | 6 |\n+------------+---------+--------------------------+--------------+\n\n\n
Explanation:
\n\nThe result table is ordered by scroll_count in descending order, then by session_id in ascending order.
\n表:app_events
\n+------------------+----------+\n| Column Name | Type | \n+------------------+----------+\n| event_id | int |\n| user_id | int |\n| event_timestamp | datetime |\n| event_type | varchar |\n| session_id | varchar |\n| event_value | int |\n+------------------+----------+\nevent_id 是这张表的唯一主键。\nevent_type 可以是 app_open,click,scroll,purchase 或 app_close。\nsession_id 将事件按同一用户会话分组。\nevent_value 表示:对于 purchase - 美元金额,对于 scroll - 滚动的像素数,对于其它 - NULL。\n\n\n
编写一个解决方案来识别 僵尸会话,即用户看似活跃但表现出异常行为模式的会话。如果会话满足以下所有条件,则被视为 僵尸会话:
\n\n30
分钟。5
次滚动事件。0.20
。返回结果表按 scroll_count
降序 排序,然后按 session_id
升序 排序。
返回格式如下所示。
\n\n\n\n
示例:
\n\n输入:
\n\napp_events 表:
\n\n\n+----------+---------+---------------------+------------+------------+-------------+\n| event_id | user_id | event_timestamp | event_type | session_id | event_value |\n+----------+---------+---------------------+------------+------------+-------------+\n| 1 | 201 | 2024-03-01 10:00:00 | app_open | S001 | NULL |\n| 2 | 201 | 2024-03-01 10:05:00 | scroll | S001 | 500 |\n| 3 | 201 | 2024-03-01 10:10:00 | scroll | S001 | 750 |\n| 4 | 201 | 2024-03-01 10:15:00 | scroll | S001 | 600 |\n| 5 | 201 | 2024-03-01 10:20:00 | scroll | S001 | 800 |\n| 6 | 201 | 2024-03-01 10:25:00 | scroll | S001 | 550 |\n| 7 | 201 | 2024-03-01 10:30:00 | scroll | S001 | 900 |\n| 8 | 201 | 2024-03-01 10:35:00 | app_close | S001 | NULL |\n| 9 | 202 | 2024-03-01 11:00:00 | app_open | S002 | NULL |\n| 10 | 202 | 2024-03-01 11:02:00 | click | S002 | NULL |\n| 11 | 202 | 2024-03-01 11:05:00 | scroll | S002 | 400 |\n| 12 | 202 | 2024-03-01 11:08:00 | click | S002 | NULL |\n| 13 | 202 | 2024-03-01 11:10:00 | scroll | S002 | 350 |\n| 14 | 202 | 2024-03-01 11:15:00 | purchase | S002 | 50 |\n| 15 | 202 | 2024-03-01 11:20:00 | app_close | S002 | NULL |\n| 16 | 203 | 2024-03-01 12:00:00 | app_open | S003 | NULL |\n| 17 | 203 | 2024-03-01 12:10:00 | scroll | S003 | 1000 |\n| 18 | 203 | 2024-03-01 12:20:00 | scroll | S003 | 1200 |\n| 19 | 203 | 2024-03-01 12:25:00 | click | S003 | NULL |\n| 20 | 203 | 2024-03-01 12:30:00 | scroll | S003 | 800 |\n| 21 | 203 | 2024-03-01 12:40:00 | scroll | S003 | 900 |\n| 22 | 203 | 2024-03-01 12:50:00 | scroll | S003 | 1100 |\n| 23 | 203 | 2024-03-01 13:00:00 | app_close | S003 | NULL |\n| 24 | 204 | 2024-03-01 14:00:00 | app_open | S004 | NULL |\n| 25 | 204 | 2024-03-01 14:05:00 | scroll | S004 | 600 |\n| 26 | 204 | 2024-03-01 14:08:00 | scroll | S004 | 700 |\n| 27 | 204 | 2024-03-01 14:10:00 | click | S004 | NULL |\n| 28 | 204 | 2024-03-01 14:12:00 | app_close | S004 | NULL |\n+----------+---------+---------------------+------------+------------+-------------+\n\n\n
输出:
\n\n\n+------------+---------+--------------------------+--------------+\n| session_id | user_id | session_duration_minutes | scroll_count |\n+------------+---------+--------------------------+--------------+\n| S001 | 201 | 35 | 6 |\n+------------+---------+--------------------------+--------------+\n\n\n
解释:
\n\n结果表按 scroll_count 降序排序,然后按 session_id 升序排序。
\n\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"app_events\":[\"event_id\",\"user_id\",\"event_timestamp\",\"event_type\",\"session_id\",\"event_value\"]},\"rows\":{\"app_events\":[[1,201,\"2024-03-01 10:00:00\",\"app_open\",\"S001\",null],[2,201,\"2024-03-01 10:05:00\",\"scroll\",\"S001\",500],[3,201,\"2024-03-01 10:10:00\",\"scroll\",\"S001\",750],[4,201,\"2024-03-01 10:15:00\",\"scroll\",\"S001\",600],[5,201,\"2024-03-01 10:20:00\",\"scroll\",\"S001\",800],[6,201,\"2024-03-01 10:25:00\",\"scroll\",\"S001\",550],[7,201,\"2024-03-01 10:30:00\",\"scroll\",\"S001\",900],[8,201,\"2024-03-01 10:35:00\",\"app_close\",\"S001\",null],[9,202,\"2024-03-01 11:00:00\",\"app_open\",\"S002\",null],[10,202,\"2024-03-01 11:02:00\",\"click\",\"S002\",null],[11,202,\"2024-03-01 11:05:00\",\"scroll\",\"S002\",400],[12,202,\"2024-03-01 11:08:00\",\"click\",\"S002\",null],[13,202,\"2024-03-01 11:10:00\",\"scroll\",\"S002\",350],[14,202,\"2024-03-01 11:15:00\",\"purchase\",\"S002\",50],[15,202,\"2024-03-01 11:20:00\",\"app_close\",\"S002\",null],[16,203,\"2024-03-01 12:00:00\",\"app_open\",\"S003\",null],[17,203,\"2024-03-01 12:10:00\",\"scroll\",\"S003\",1000],[18,203,\"2024-03-01 12:20:00\",\"scroll\",\"S003\",1200],[19,203,\"2024-03-01 12:25:00\",\"click\",\"S003\",null],[20,203,\"2024-03-01 12:30:00\",\"scroll\",\"S003\",800],[21,203,\"2024-03-01 12:40:00\",\"scroll\",\"S003\",900],[22,203,\"2024-03-01 12:50:00\",\"scroll\",\"S003\",1100],[23,203,\"2024-03-01 13:00:00\",\"app_close\",\"S003\",null],[24,204,\"2024-03-01 14:00:00\",\"app_open\",\"S004\",null],[25,204,\"2024-03-01 14:05:00\",\"scroll\",\"S004\",600],[26,204,\"2024-03-01 14:08:00\",\"scroll\",\"S004\",700],[27,204,\"2024-03-01 14:10:00\",\"click\",\"S004\",null],[28,204,\"2024-03-01 14:12:00\",\"app_close\",\"S004\",null]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"