我有一个学习者表,名为learner_lesson
learnerlessonid learnerid lessonid
1 24 42
另一个表为learner_lesson_log
lessonlogid learnerlessonid progress maxprogress. interactionType createdAt
1 1 0 15 Start 2022-11-02 07:50:30
1 1 0 15 Start 2022-11-02 07:51:30
2 1 4 15 Pause 2022-11-02 07:51:34
3 1 4 15 Play 2022-11-02 07:52:20
4 1 14 15 Run 2022-11-02 07:52:30
5 1 15 15 Stop 2022-11-02 07:52:31
我想得到的是学习者观看的秒数。但是,可能发生的情况是,学习者开始了一节课,但没有完成它或暂停它,稍后回来,然后完成课程。例如,在上面的例子中,学习者开始了一节课,并放弃了它,再次回来,并再次开始课程,然后在4秒后暂停它。我希望结果看起来像
Learner ID Length of Interaction Start Timestamp
24 4 2022-11-02 07:51:30
24 11 2022-11-02 07:52:20
但我有一个疑问
Learner ID Length of Interaction Start Timestamp
24 64 2022-11-02 07:50:30
24 4 2022-11-02 07:51:30
24 11 2022-11-02 07:52:20
我希望查询只计算Start -> Pause, Start->Stop, Play -> Pause, Play -> Stop
组合之间的秒数。如何才能实现此结果?这是我的查询
SELECT
c.learnerid AS "Learner ID",
TIMESTAMPDIFF(SECOND, a.createdAt,
(SELECT b.createdAt
FROM learner_lesson_log b
INNER JOIN learner_lessons d
ON b.learnerLessonId = d.learnerLessonId
WHERE b.learnerLessonId = a.learnerLessonId
AND d.learnerId = c.learnerId
AND b.createdAt > a.createdAt
AND b.interactionType IN ('Stop', 'Pause')
ORDER BY b.createdAt ASC LIMIT 1)) AS "Length of Interaction",
a.createdAt AS "Start Timestamp"
FROM learner_lesson_log a
INNER JOIN learner_lessons c
ON c.learnerLessonId = a.learnerLessonId
WHERE a.interactionType IN ('Start', 'Play')
ORDER BY a.createdAt ASC;
这是fiddle
2条答案
按热度按时间dgenwo3n1#
但是版本8解决方案也可以
step-by-step fiddle
jjhzyzn02#
我认为这在5. 7上和预期的一样有效-
db<>fiddle