{ "data": { "question": { "questionId": "3961", "questionFrontendId": "3617", "categoryTitle": "Database", "boundTopicId": 3723935, "title": "Find Students with Study Spiral Pattern", "titleSlug": "find-students-with-study-spiral-pattern", "content": "

Table: students

\n\n
\n+--------------+---------+\n| Column Name  | Type    |\n+--------------+---------+\n| student_id   | int     |\n| student_name | varchar |\n| major        | varchar |\n+--------------+---------+\nstudent_id is the unique identifier for this table.\nEach row contains information about a student and their academic major.\n
\n\n

Table: study_sessions

\n\n
\n+---------------+---------+\n| Column Name   | Type    |\n+---------------+---------+\n| session_id    | int     |\n| student_id    | int     |\n| subject       | varchar |\n| session_date  | date    |\n| hours_studied | decimal |\n+---------------+---------+\nsession_id is the unique identifier for this table.\nEach row represents a study session by a student for a specific subject.\n
\n\n

Write a solution to find students who follow the Study Spiral Pattern - students who consistently study multiple subjects in a rotating cycle.

\n\n\n\n

Return the result table ordered by cycle length in descending order, then by total study hours in descending order.

\n\n

The result format is in the following example.

\n\n

 

\n

Example:

\n\n
\n

Input:

\n\n

students table:

\n\n
\n+------------+--------------+------------------+\n| student_id | student_name | major            |\n+------------+--------------+------------------+\n| 1          | Alice Chen   | Computer Science |\n| 2          | Bob Johnson  | Mathematics      |\n| 3          | Carol Davis  | Physics          |\n| 4          | David Wilson | Chemistry        |\n| 5          | Emma Brown   | Biology          |\n+------------+--------------+------------------+\n
\n\n

study_sessions table:

\n\n
\n+------------+------------+------------+--------------+---------------+\n| session_id | student_id | subject    | session_date | hours_studied |\n+------------+------------+------------+--------------+---------------+\n| 1          | 1          | Math       | 2023-10-01   | 2.5           |\n| 2          | 1          | Physics    | 2023-10-02   | 3.0           |\n| 3          | 1          | Chemistry  | 2023-10-03   | 2.0           |\n| 4          | 1          | Math       | 2023-10-04   | 2.5           |\n| 5          | 1          | Physics    | 2023-10-05   | 3.0           |\n| 6          | 1          | Chemistry  | 2023-10-06   | 2.0           |\n| 7          | 2          | Algebra    | 2023-10-01   | 4.0           |\n| 8          | 2          | Calculus   | 2023-10-02   | 3.5           |\n| 9          | 2          | Statistics | 2023-10-03   | 2.5           |\n| 10         | 2          | Geometry   | 2023-10-04   | 3.0           |\n| 11         | 2          | Algebra    | 2023-10-05   | 4.0           |\n| 12         | 2          | Calculus   | 2023-10-06   | 3.5           |\n| 13         | 2          | Statistics | 2023-10-07   | 2.5           |\n| 14         | 2          | Geometry   | 2023-10-08   | 3.0           |\n| 15         | 3          | Biology    | 2023-10-01   | 2.0           |\n| 16         | 3          | Chemistry  | 2023-10-02   | 2.5           |\n| 17         | 3          | Biology    | 2023-10-03   | 2.0           |\n| 18         | 3          | Chemistry  | 2023-10-04   | 2.5           |\n| 19         | 4          | Organic    | 2023-10-01   | 3.0           |\n| 20         | 4          | Physical   | 2023-10-05   | 2.5           |\n+------------+------------+------------+--------------+---------------+\n
\n\n

Output:

\n\n
\n+------------+--------------+------------------+--------------+-------------------+\n| student_id | student_name | major            | cycle_length | total_study_hours |\n+------------+--------------+------------------+--------------+-------------------+\n| 2          | Bob Johnson  | Mathematics      | 4            | 26.0              |\n| 1          | Alice Chen   | Computer Science | 3            | 15.0              |\n+------------+--------------+------------------+--------------+-------------------+\n
\n\n

Explanation:

\n\n\n\n

The result table is ordered by cycle_length in descending order, then by total_study_hours in descending order.

\n
\n", "translatedTitle": "查找具有螺旋学习模式的学生", "translatedContent": "

表:students

\n\n
\n+--------------+---------+\n| Column Name  | Type    |\n+--------------+---------+\n| student_id   | int     |\n| student_name | varchar |\n| major        | varchar |\n+--------------+---------+\nstudent_id 是这张表的唯一主键。\n每一行包含有关学生及其学术专业的信息。\n
\n\n

表:study_sessions

\n\n
\n+---------------+---------+\n| Column Name   | Type    |\n+---------------+---------+\n| session_id    | int     |\n| student_id    | int     |\n| subject       | varchar |\n| session_date  | date    |\n| hours_studied | decimal |\n+---------------+---------+\nsession_id 是这张表的唯一主键。\n每一行代表一个学生针对特定学科的学习时段。\n
\n\n

编写一个解决方案来找出遵循 螺旋学习模式 的学生——即那些持续学习多个学科并按循环周期进行学习的学生。

\n\n\n\n

返回结果表按循环长度 降序 排序,然后按总学习时间 降序 排序。

\n\n

结果格式如下所示。

\n\n

 

\n\n

示例:

\n\n
\n

输入:

\n\n

students 表:

\n\n
\n+------------+--------------+------------------+\n| student_id | student_name | major            |\n+------------+--------------+------------------+\n| 1          | Alice Chen   | Computer Science |\n| 2          | Bob Johnson  | Mathematics      |\n| 3          | Carol Davis  | Physics          |\n| 4          | David Wilson | Chemistry        |\n| 5          | Emma Brown   | Biology          |\n+------------+--------------+------------------+\n
\n\n

study_sessions 表:

\n\n
\n+------------+------------+------------+--------------+---------------+\n| session_id | student_id | subject    | session_date | hours_studied |\n+------------+------------+------------+--------------+---------------+\n| 1          | 1          | Math       | 2023-10-01   | 2.5           |\n| 2          | 1          | Physics    | 2023-10-02   | 3.0           |\n| 3          | 1          | Chemistry  | 2023-10-03   | 2.0           |\n| 4          | 1          | Math       | 2023-10-04   | 2.5           |\n| 5          | 1          | Physics    | 2023-10-05   | 3.0           |\n| 6          | 1          | Chemistry  | 2023-10-06   | 2.0           |\n| 7          | 2          | Algebra    | 2023-10-01   | 4.0           |\n| 8          | 2          | Calculus   | 2023-10-02   | 3.5           |\n| 9          | 2          | Statistics | 2023-10-03   | 2.5           |\n| 10         | 2          | Geometry   | 2023-10-04   | 3.0           |\n| 11         | 2          | Algebra    | 2023-10-05   | 4.0           |\n| 12         | 2          | Calculus   | 2023-10-06   | 3.5           |\n| 13         | 2          | Statistics | 2023-10-07   | 2.5           |\n| 14         | 2          | Geometry   | 2023-10-08   | 3.0           |\n| 15         | 3          | Biology    | 2023-10-01   | 2.0           |\n| 16         | 3          | Chemistry  | 2023-10-02   | 2.5           |\n| 17         | 3          | Biology    | 2023-10-03   | 2.0           |\n| 18         | 3          | Chemistry  | 2023-10-04   | 2.5           |\n| 19         | 4          | Organic    | 2023-10-01   | 3.0           |\n| 20         | 4          | Physical   | 2023-10-05   | 2.5           |\n+------------+------------+------------+--------------+---------------+\n
\n\n

输出:

\n\n
\n+------------+--------------+------------------+--------------+-------------------+\n| student_id | student_name | major            | cycle_length | total_study_hours |\n+------------+--------------+------------------+--------------+-------------------+\n| 2          | Bob Johnson  | Mathematics      | 4            | 26.0              |\n| 1          | Alice Chen   | Computer Science | 3            | 15.0              |\n+------------+--------------+------------------+--------------+-------------------+\n
\n\n

解释:

\n\n\n\n

结果表以 cycle_length 降序排序,然后以 total_study_hours 降序排序。

\n
\n", "isPaidOnly": false, "difficulty": "Hard", "likes": 0, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false, \"cangjie\": 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": "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_study_spiral_pattern(students: pd.DataFrame, study_sessions: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"61\", \"totalSubmission\": \"127\", \"totalAcceptedRaw\": 61, \"totalSubmissionRaw\": 127, \"acRate\": \"48.0%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"students\":[\"student_id\",\"student_name\",\"major\"],\"study_sessions\":[\"session_id\",\"student_id\",\"subject\",\"session_date\",\"hours_studied\"]},\"rows\":{\"students\":[[1,\"Alice Chen\",\"Computer Science\"],[2,\"Bob Johnson\",\"Mathematics\"],[3,\"Carol Davis\",\"Physics\"],[4,\"David Wilson\",\"Chemistry\"],[5,\"Emma Brown\",\"Biology\"]],\"study_sessions\":[[1,1,\"Math\",\"2023-10-01\",2.5],[2,1,\"Physics\",\"2023-10-02\",3.0],[3,1,\"Chemistry\",\"2023-10-03\",2.0],[4,1,\"Math\",\"2023-10-04\",2.5],[5,1,\"Physics\",\"2023-10-05\",3.0],[6,1,\"Chemistry\",\"2023-10-06\",2.0],[7,2,\"Algebra\",\"2023-10-01\",4.0],[8,2,\"Calculus\",\"2023-10-02\",3.5],[9,2,\"Statistics\",\"2023-10-03\",2.5],[10,2,\"Geometry\",\"2023-10-04\",3.0],[11,2,\"Algebra\",\"2023-10-05\",4.0],[12,2,\"Calculus\",\"2023-10-06\",3.5],[13,2,\"Statistics\",\"2023-10-07\",2.5],[14,2,\"Geometry\",\"2023-10-08\",3.0],[15,3,\"Biology\",\"2023-10-01\",2.0],[16,3,\"Chemistry\",\"2023-10-02\",2.5],[17,3,\"Biology\",\"2023-10-03\",2.0],[18,3,\"Chemistry\",\"2023-10-04\",2.5],[19,4,\"Organic\",\"2023-10-01\",3.0],[20,4,\"Physical\",\"2023-10-05\",2.5]]}}", "metaData": "{\"mysql\":[\"CREATE TABLE if not exists students (\\n student_id INT,\\n student_name VARCHAR(255),\\n major VARCHAR(100)\\n)\",\"CREATE TABLE study_sessions (\\n session_id INT,\\n student_id INT,\\n subject VARCHAR(100),\\n session_date DATE,\\n hours_studied DECIMAL(4, 2)\\n)\"],\"mssql\":[\"CREATE TABLE students (\\n student_id INT,\\n student_name VARCHAR(255),\\n major VARCHAR(100)\\n)\",\"CREATE TABLE study_sessions (\\n session_id INT,\\n student_id INT,\\n subject VARCHAR(100),\\n session_date DATE,\\n hours_studied DECIMAL(4, 2)\\n)\"],\"oraclesql\":[\"CREATE TABLE students (\\n student_id NUMBER,\\n student_name VARCHAR2(255),\\n major VARCHAR2(100)\\n)\",\"\\nCREATE TABLE study_sessions (\\n session_id NUMBER,\\n student_id NUMBER,\\n subject VARCHAR2(100),\\n session_date DATE,\\n hours_studied NUMBER(4, 2)\\n)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"find_study_spiral_pattern\",\"postgresql\":[\"CREATE TABLE IF NOT EXISTS students (\\n student_id INTEGER,\\n student_name VARCHAR(255),\\n major VARCHAR(100)\\n);\\n\",\"CREATE TABLE IF NOT EXISTS study_sessions (\\n session_id INTEGER,\\n student_id INTEGER,\\n subject VARCHAR(100),\\n session_date DATE,\\n hours_studied DECIMAL(4, 2)\\n);\\n\"],\"pythondata\":[\"students = pd.DataFrame({'student_id': pd.Series(dtype='int'), 'student_name': pd.Series(dtype='str'), 'major': pd.Series(dtype='str')})\",\"study_sessions = pd.DataFrame({'session_id': pd.Series(dtype='int'), 'student_id': pd.Series(dtype='int'), 'subject': pd.Series(dtype='str'), 'session_date': pd.Series(dtype='datetime64[ns]'), 'hours_studied': pd.Series(dtype='float')})\"],\"database_schema\":{\"students\":{\"student_id\":\"INT\",\"student_name\":\"VARCHAR(255)\",\"major\":\"VARCHAR(100)\"},\"study_sessions\":{\"session_id\":\"INT\",\"student_id\":\"INT\",\"subject\":\"VARCHAR(100)\",\"session_date\":\"DATE\",\"hours_studied\":\"DECIMAL(4, 2)\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "CREATE TABLE if not exists students (\n student_id INT,\n student_name VARCHAR(255),\n major VARCHAR(100)\n)", "CREATE TABLE study_sessions (\n session_id INT,\n student_id INT,\n subject VARCHAR(100),\n session_date DATE,\n hours_studied DECIMAL(4, 2)\n)", "Truncate table students", "insert into students (student_id, student_name, major) values ('1', 'Alice Chen', 'Computer Science')", "insert into students (student_id, student_name, major) values ('2', 'Bob Johnson', 'Mathematics')", "insert into students (student_id, student_name, major) values ('3', 'Carol Davis', 'Physics')", "insert into students (student_id, student_name, major) values ('4', 'David Wilson', 'Chemistry')", "insert into students (student_id, student_name, major) values ('5', 'Emma Brown', 'Biology')", "Truncate table study_sessions", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('1', '1', 'Math', '2023-10-01', '2.5')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('2', '1', 'Physics', '2023-10-02', '3.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('3', '1', 'Chemistry', '2023-10-03', '2.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('4', '1', 'Math', '2023-10-04', '2.5')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('5', '1', 'Physics', '2023-10-05', '3.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('6', '1', 'Chemistry', '2023-10-06', '2.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('7', '2', 'Algebra', '2023-10-01', '4.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('8', '2', 'Calculus', '2023-10-02', '3.5')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('9', '2', 'Statistics', '2023-10-03', '2.5')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('10', '2', 'Geometry', '2023-10-04', '3.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('11', '2', 'Algebra', '2023-10-05', '4.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('12', '2', 'Calculus', '2023-10-06', '3.5')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('13', '2', 'Statistics', '2023-10-07', '2.5')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('14', '2', 'Geometry', '2023-10-08', '3.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('15', '3', 'Biology', '2023-10-01', '2.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('16', '3', 'Chemistry', '2023-10-02', '2.5')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('17', '3', 'Biology', '2023-10-03', '2.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('18', '3', 'Chemistry', '2023-10-04', '2.5')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('19', '4', 'Organic', '2023-10-01', '3.0')", "insert into study_sessions (session_id, student_id, subject, session_date, hours_studied) values ('20', '4', 'Physical', '2023-10-05', '2.5')" ], "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\":{\"students\":[\"student_id\",\"student_name\",\"major\"],\"study_sessions\":[\"session_id\",\"student_id\",\"subject\",\"session_date\",\"hours_studied\"]},\"rows\":{\"students\":[[1,\"Alice Chen\",\"Computer Science\"],[2,\"Bob Johnson\",\"Mathematics\"],[3,\"Carol Davis\",\"Physics\"],[4,\"David Wilson\",\"Chemistry\"],[5,\"Emma Brown\",\"Biology\"]],\"study_sessions\":[[1,1,\"Math\",\"2023-10-01\",2.5],[2,1,\"Physics\",\"2023-10-02\",3.0],[3,1,\"Chemistry\",\"2023-10-03\",2.0],[4,1,\"Math\",\"2023-10-04\",2.5],[5,1,\"Physics\",\"2023-10-05\",3.0],[6,1,\"Chemistry\",\"2023-10-06\",2.0],[7,2,\"Algebra\",\"2023-10-01\",4.0],[8,2,\"Calculus\",\"2023-10-02\",3.5],[9,2,\"Statistics\",\"2023-10-03\",2.5],[10,2,\"Geometry\",\"2023-10-04\",3.0],[11,2,\"Algebra\",\"2023-10-05\",4.0],[12,2,\"Calculus\",\"2023-10-06\",3.5],[13,2,\"Statistics\",\"2023-10-07\",2.5],[14,2,\"Geometry\",\"2023-10-08\",3.0],[15,3,\"Biology\",\"2023-10-01\",2.0],[16,3,\"Chemistry\",\"2023-10-02\",2.5],[17,3,\"Biology\",\"2023-10-03\",2.0],[18,3,\"Chemistry\",\"2023-10-04\",2.5],[19,4,\"Organic\",\"2023-10-01\",3.0],[20,4,\"Physical\",\"2023-10-05\",2.5]]}}", "__typename": "QuestionNode" } } }