What is an efficient way to compute how much time someone has watched a movie in Python?
For this SQL version, calculate valid watched time from playback session offsets, including open sessions and offsets outside the movie duration. Exclude sessions without valid viewer or movie matches and sessions with no positive duration.
Output
- One row per viewer and movie with valid watch time.
- Columns:
viewer_id, viewer_name, movie_id, title, total_watched_seconds, and watch_percentage.
- Sort by
viewer_id, then movie_id, both ascending.
Output details
- Clamp
start_second below 0 to 0, and clamp end_second above the movie duration to that duration.
- Return
total_watched_seconds as a bigint.
- Round
watch_percentage to exactly two decimal places.