{ "data": { "question": { "questionId": "4043", "questionFrontendId": "3673", "categoryTitle": "Database", "boundTopicId": 3774328, "title": "Find Zombie Sessions", "titleSlug": "find-zombie-sessions", "content": "

Table: app_events

\n\n
\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\n\n\n

Return the result table ordered by scroll_count in descending order, then by session_id in ascending order.

\n\n

The result format is in the following example.

\n\n

 

\n

Example:

\n\n
\n

Input:

\n\n

app_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\n\n\n

The result table is ordered by scroll_count in descending order, then by session_id in ascending order.

\n
\n", "translatedTitle": "查找僵尸会话", "translatedContent": "

表:app_events

\n\n
\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\n\n\n

返回结果表按 scroll_count 降序 排序,然后按 session_id 升序 排序。

\n\n

返回格式如下所示。

\n\n

 

\n\n

示例:

\n\n
\n

输入:

\n\n

app_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\n\n

结果表按 scroll_count 降序排序,然后按 session_id 升序排序。

\n
\n", "isPaidOnly": false, "difficulty": "Hard", "likes": 0, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python3\": false, \"python\": false, \"javascript\": false, \"typescript\": false, \"csharp\": false, \"c\": false, \"golang\": false, \"kotlin\": false, \"swift\": false, \"rust\": false, \"ruby\": false, \"php\": false, \"dart\": false, \"scala\": false, \"elixir\": false, \"erlang\": false, \"racket\": false, \"cangjie\": false, \"bash\": false, \"html\": false, \"pythonml\": false, \"react\": false, \"vanillajs\": false, \"mysql\": false, \"mssql\": false, \"postgresql\": false, \"oraclesql\": false, \"pythondata\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef find_zombie_sessions(app_events: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"286\", \"totalSubmission\": \"459\", \"totalAcceptedRaw\": 286, \"totalSubmissionRaw\": 459, \"acRate\": \"62.3%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"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]]}}", "metaData": "{\"mysql\":[\"CREATE TABLE app_events (\\n event_id INT,\\n user_id INT,\\n event_timestamp DATETIME,\\n event_type VARCHAR(20),\\n session_id VARCHAR(10),\\n event_value INT\\n)\"],\"mssql\":[\"CREATE TABLE app_events (\\n event_id INT,\\n user_id INT,\\n event_timestamp DATETIME,\\n event_type VARCHAR(20),\\n session_id VARCHAR(10),\\n event_value INT\\n)\"],\"oraclesql\":[\"CREATE TABLE app_events (\\n event_id NUMBER,\\n user_id NUMBER,\\n event_timestamp Date,\\n event_type VARCHAR2(20),\\n session_id VARCHAR2(10),\\n event_value NUMBER\\n)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD HH24:MI:SS'\"],\"database\":true,\"name\":\"find_zombie_sessions\",\"postgresql\":[\"CREATE TABLE app_events (\\n event_id INT PRIMARY KEY,\\n user_id INT,\\n event_timestamp TIMESTAMP,\\n event_type VARCHAR(20),\\n session_id VARCHAR(10),\\n event_value INT\\n);\\n\"],\"pythondata\":[\"app_events = pd.DataFrame({\\n \\\"event_id\\\": pd.Series(dtype=\\\"int\\\"),\\n \\\"user_id\\\": pd.Series(dtype=\\\"int\\\"),\\n \\\"event_timestamp\\\": pd.Series(dtype=\\\"datetime64[ns]\\\"),\\n \\\"event_type\\\": pd.Series(dtype=\\\"string\\\"), # varchar -> string\\n \\\"session_id\\\": pd.Series(dtype=\\\"string\\\"), # varchar -> string\\n \\\"event_value\\\": pd.Series(dtype=\\\"int\\\")\\n})\"],\"database_schema\":{\"app_events\":{\"event_id\":\"INT\",\"user_id\":\"INT\",\"event_timestamp\":\"DATETIME\",\"event_type\":\"VARCHAR(20)\",\"session_id\":\"VARCHAR(10)\",\"event_value\":\"INT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "CREATE TABLE app_events (\n event_id INT,\n user_id INT,\n event_timestamp DATETIME,\n event_type VARCHAR(20),\n session_id VARCHAR(10),\n event_value INT\n)", "Truncate table app_events", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('1', '201', '2024-03-01 10:00:00', 'app_open', 'S001', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('2', '201', '2024-03-01 10:05:00', 'scroll', 'S001', '500')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('3', '201', '2024-03-01 10:10:00', 'scroll', 'S001', '750')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('4', '201', '2024-03-01 10:15:00', 'scroll', 'S001', '600')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('5', '201', '2024-03-01 10:20:00', 'scroll', 'S001', '800')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('6', '201', '2024-03-01 10:25:00', 'scroll', 'S001', '550')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('7', '201', '2024-03-01 10:30:00', 'scroll', 'S001', '900')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('8', '201', '2024-03-01 10:35:00', 'app_close', 'S001', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('9', '202', '2024-03-01 11:00:00', 'app_open', 'S002', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('10', '202', '2024-03-01 11:02:00', 'click', 'S002', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('11', '202', '2024-03-01 11:05:00', 'scroll', 'S002', '400')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('12', '202', '2024-03-01 11:08:00', 'click', 'S002', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('13', '202', '2024-03-01 11:10:00', 'scroll', 'S002', '350')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('14', '202', '2024-03-01 11:15:00', 'purchase', 'S002', '50')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('15', '202', '2024-03-01 11:20:00', 'app_close', 'S002', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('16', '203', '2024-03-01 12:00:00', 'app_open', 'S003', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('17', '203', '2024-03-01 12:10:00', 'scroll', 'S003', '1000')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('18', '203', '2024-03-01 12:20:00', 'scroll', 'S003', '1200')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('19', '203', '2024-03-01 12:25:00', 'click', 'S003', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('20', '203', '2024-03-01 12:30:00', 'scroll', 'S003', '800')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('21', '203', '2024-03-01 12:40:00', 'scroll', 'S003', '900')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('22', '203', '2024-03-01 12:50:00', 'scroll', 'S003', '1100')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('23', '203', '2024-03-01 13:00:00', 'app_close', 'S003', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('24', '204', '2024-03-01 14:00:00', 'app_open', 'S004', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('25', '204', '2024-03-01 14:05:00', 'scroll', 'S004', '600')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('26', '204', '2024-03-01 14:08:00', 'scroll', 'S004', '700')", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('27', '204', '2024-03-01 14:10:00', 'click', 'S004', NULL)", "insert into app_events (event_id, user_id, event_timestamp, event_type, session_id, event_value) values ('28', '204', '2024-03-01 14:12:00', 'app_close', 'S004', NULL)" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"

\\u7248\\u672c\\uff1aMySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"

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" } } }