{ "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| 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| 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\n3
different subjects in a repeating sequence2
complete cycles (minimum 6
study sessions)2
days between sessions3
subjectsReturn the result table ordered by cycle length in descending order, then by total study hours in descending order.
\n\nThe result format is in the following example.
\n\n\n
Example:
\n\nInput:
\n\nstudents 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\nThe result table is ordered by cycle_length in descending order, then by total_study_hours in descending order.
\n表:students
\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| 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\n3
个 不同的学科。2
个完整周期(最少 6
次学习记录)。2
天的 连续日期。3
门学科 的学生。返回结果表按循环长度 降序 排序,然后按总学习时间 降序 排序。
\n\n结果格式如下所示。
\n\n\n\n
示例:
\n\n输入:
\n\nstudents 表:
\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结果表以 cycle_length 降序排序,然后以 total_study_hours 降序排序。
\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\":{\"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"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"